Auto Set vs Manual Decrease (How to do both?)
This may be a bit complex to understand at first, but please bear with me. I am making a city simulation game, and what I want to achieve is this:
1. Look at the healthStat of the city
2. If it's within a range, set the daysRemaining (meaning the city's lifespan)
3. Every day that passes, the daysRemaining will be decreased.
Guideline:
healthStat | default daysRemaining
-10 10
-25 8
-35 5
-40 2
I have managed to somewhat do a half-solution to this, however, a problem came up. I can only do either the auto setting of the daysRemaining, or the manual decrease - I cannot do both.
Here's my code:
if (statsHealth <= -10f && statsHealth > -25f)
{
//Enable Health Hazard
isHealthHazard = true;
//Set Days Remaining
daysRemaining = 10;
}
else if (statsHealth <= -25f && statsHealth > -35f)
{
//Enable Health Hazard
isHealthHazard = true;
//Set Days Remaining
if (daysRemaining > 8 || daysRemaining <= 5)
{
daysRemaining = 8;
}
}
else if (statsHealth <= -35f && statsHealth > -40f)
{
//Enable Health Hazard
isHealthHazard = true;
//Set Days Remaining
if (daysRemaining > 5 || daysRemaining <= 2)
{
daysRemaining = 5;
}
}
else if (statsHealth <= -40f && statsHealth > -50f)
{
//Enable Health Hazard
isHealthHazard = true;
//Set Days Remaining
if (daysRemaining > 2 || daysRemaining < 0)
{
daysRemaining = 2;
}
}
else if(statsHealth >= -50f && hasIssues)
{
//GAME OVER
Debug.Log("GAME OVER!");
daysRemaining = 0;
}
else if(statsHealth >= 0)
{
//No More Health Issues
isHealthHazard = false;
}
The problem is - whenever a day goes by in my game, if it is within the range of the healthStat (ex: -25's healthStat range is between 8 - 6 daysRemaining), it would decrease. However, if the daysRemaining reached 5, it would reset back to 8 since it is out of range (based on the healthStat).
On the other side, if I disable the autoSetting of the daysRemaining based on the healthStat's range, it would work for the manual decreasing. But how can I set the daysRemaining in the first place whenever the player reaches a certain healthStat?
How can I do both auto set the daysRemaining, and at the same time can be able to manually adjust it?
PS: I know this might be a little confusing, so if you have any further inquiries, please feel free to comment it out and I will surely respond. Thanks!
Answer by Bunny83 · Jul 13, 2019 at 11:36 AM
You should to rethink your setup. This doesn't make much sense. You can not use a single variable to track two different values over time. Just split it into two seperate variables. A city has a certain lifespan based on the "statsHealth" (at least that's how I understood your concept). Do not subtract 1 every day from this variable. Instead count up how many days have passed already as an absolute value.
That would mean if your city has a current lifespan of 10 and 2 days have already passed, the current remaining days are 8 (10-2). If your conditions change change so the lifespan drops to 8 the actual remaining days would be 6 (8-2). Though that also means if the lifespan is for example 10 and 9 days have passed, only 1 day is remaining. When the conditions drop the lifespan would drop to "8" and 9 days have already passed, so the city is instant dead.
That's a general issue when you do instant changes to any kind of stats. In reality there is no instant change. Everything happens over time. I think the concept of lowering the lifespawn of the city afterwards just makes no sense. It would make more sense to give a city not exact days but some sort of life "energy". This is a fix amount. If the conditions are bad you just increase the speed in which this energy is drained.
It's not quite clear what you had in mind with your current setup. We don't even know if you're intended to do a realtime game or a turn based game.
Thank you so much! This makes much sense. I was trying to make a turn-based game and each day is like a turn. I stick with the auto changing of stats and variables to avoid players exploiting bugs and have a never-ending city lifespan - but I guess I'm doing it wrong. Thank you for the advice in regards of an "energy" like feature! :)