- Home /
TimeSpan why are you being difficult?
Question updated, 1/17/12 11:30 am EST.
What i am trying to do is to make the pet's hunger(or food) decrease every 12 hours
so the user/player can feed it every now and then.
any help would be much appreciated.
var monsterCurrentHunger: int = 100;
var monsterMaxHunger: int = 100;
var hungerTimerStarted: boolean = false;
var monsterAlive: boolean = true;
var saveTime: System.DateTime;
var passedTime: System.TimeSpan;
var hungryTime: System.TimeSpan; // need to make this into a constant of 12 hours
function Update () {
// recording the timer or start point
if (hungerTimerStarted == false){
saveTime = System.DateTime.Now;
hungerTimerStarted = true;
}
// once timer started
if (hungerTimerStarted == true){
passedTime = System.DateTime.Now - saveTime;
// every 12 hours hunger lose 10 point.
/*error: This code here is what i want to do and don't know how to do it.
1, how to create a constant timespan of 12 hours? (the hungryTime var on top)
2, how to do the 3 line below:
//error msg end
if (passedTime => hungryTime) { monsterCurrentHunger -= (10 * (passedTime/hungryTime)); hungerTimerStarted = false; }
}
}
Please edit your code, select it, hit the 101010 button to format it please.
can you please say what the errors are?? it is clueless for us... btw is is compiler error or logical error ?!?!
edited it hope it is easier to read now, and added the compiler error
passed time is of type System.DateTime you have to change it to timespan because supraction of 2 times returns a time span not another time!! and also the other error is because u used - ins$$anonymous$$d of -=
Thanks for the help, i really appreciated it.
when i changed the passedTime to TimeSpan, it took care of the error, but now i am getting more road block towards what i want to do. the Question has been edited.
Answer by Default117 · Jan 17, 2012 at 06:54 AM
I answered a very similar question a while ago regarding real time integration with games and affecting "hunger" =P http://answers.unity3d.com/questions/171302/save-current-time-on-quit-then-on-relaunch-compare.html
Answer by BiG · Jan 17, 2012 at 07:04 AM
Dewald117's solution seems a great one. This would solve the first error. About the second error of yours,
monsterCurrentHunger - (passedTime).Day * 20;
doesn't seem a legal statement (that's not an assignment). Would you say
monsterCurrentHunger -= (passedTime).Day * 20;
, maybe?
Your answer
Follow this Question
Related Questions
slowly decrease value when in trigger 1 Answer
See if a time span is bigger than X 4 Answers
Increase/Decrease Over Time, Not Frames 1 Answer
Accumulating deltaTime oddity 1 Answer