x


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;

      }
    }

}
more ▼

asked Jan 14 '12 at 09:42 PM

hdy gravatar image

hdy
1 2 3 3

Please edit your code, select it, hit the 101010 button to format it please.

Jan 17 '12 at 02:52 AM DaveA

can you please say what the errors are?? it is clueless for us... btw is is compiler error or logical error ?!?!

Jan 17 '12 at 06:19 AM flamy

edited it hope it is easier to read now, and added the compiler error

Jan 17 '12 at 06:47 AM hdy

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 - instead of -=

Jan 17 '12 at 09:55 AM flamy

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.

Jan 17 '12 at 04:34 PM hdy
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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

more ▼

answered Jan 17 '12 at 06:54 AM

Default117 gravatar image

Default117
331 26 29 33

(comments are locked)
10|3000 characters needed characters left

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?

more ▼

answered Jan 17 '12 at 07:04 AM

BiG gravatar image

BiG
4.7k 4 13 49

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x570
x52
x27
x8

asked: Jan 14 '12 at 09:42 PM

Seen: 1318 times

Last Updated: Jan 17 '12 at 04:35 PM