- Home /
Variables not changing correctly
Hello, first off sorry for asking so many questions lately. Now on with my question. My variables are changing far less than they should. Some change by one per 5 seconds, some by 0. Here is my code.
var population = 100; var food = 100.0; var happiness = 60.0; var farms = 5; var harvest = farms * 5; var hunger = population * 0.25; var starvation = 0.0; InvokeRepeating ("FoodCycle", 0, 5);
function FoodCycle () {
var populationgrowth = Random.Range(population / 10, population / 100);
population += populationgrowth * Time.deltaTime;
food += harvest * Time.deltaTime;
food -= hunger * Time.deltaTime;
if (food < population / 10)
happiness -= 1 * Time.deltaTime;
if (food < 1)
happiness -= 5 * Time.deltaTime;
if (1 > food)
starvation += 1 * Time.deltaTime;
if (1 < starvation)
population -= 1;
if (1 < starvation)
starvation -= 1;
}
EDIT:
Now that my variables are updating at the right time there is another problem. My variables that depend on each other don't update in relation to each other. For example as population increases hunger does not. Here's my current code:
var population = 100.0; var food = 100.0; var happiness = 60.0; var farms = 5.0; var harvest = farms * 5.0; var hunger = population * 0.25; var starvation = 0.0; InvokeRepeating ("FoodCycle", 0.0, 120.0); var populationgrowth = Random.Range(population / 100.0, population / 50.0);
function FoodCycle () {
population += populationgrowth;
food += harvest;
food -= hunger;
if (food < population / 10.0)
happiness -= 5.0;
if (food < 1.0)
happiness -= 10.0;
if (1 > food)
starvation += 1.0;
if (1 < starvation)
population -= 1.0;
if (1 < starvation)
starvation -= 1.0;
if (food < 0.0)
food = 0.0;
if (population < 0.0)
population = 0.0;
if (food < population)
populationgrowth = 0.0;
if (food > population * 2)
happiness += 1.0;
if (happiness > 100)
happiness = 100;
if (happiness < -100)
happiness = -100;
}
Umh, but please don't add another question to already existing questions using "edit". Simply create a new question for it, or use the forum at http://forum.unity3d.com/ which is better suited for discussions.
If you look at your code, you make only one assignment to "hunger" - in the initialization. So it never changes, even if the population changes. Ins$$anonymous$$d, you need to update hunger accordingly. Don't write everything linearly one after the other, split your method into smaller functions. You could write a function GetHunger() which computes a new hunger value, etc. You will run into similar problems with harvest and populationgrowth.
Answer by Mike 3 · Aug 06, 2010 at 07:07 PM
Looks like the same issue as last time - population is an integer, so using integer/integer is a bad idea
Either make population a float, or divide by floats each time (i.e. 10.0 and 100.0)
Also - you're invoking the function every 5 seconds, but multiplying by Time.deltaTime, which is the last frame time. My guess is that you should remove all the Time.deltaTime parts, and if you must, multiply by 5 instead if you want it in value per second
Thanks! This solves the problem I had here. However it creates a bunch more...prepare for more questions...
Answer by Wolfram · Aug 07, 2010 at 12:04 AM
Everything Mike said is perfectly valid. However, another problem might be your use of Random.Range(). According to the documentation, the first parameter is the minimum, and the 2nd is the maximum. population/10 is 10, population/100 is 1, so you should reverse it (and take care of floating points):
var populationgrowth = Random.Range(population / 100.0, population / 10.0);
Note the maximum is not included in the range, so this call will return values from 1 to 9 for a population of 100.