JS Class and System Events...

I am looking to build for example a Timer class and from what it seems to me, grabbing time information via deltaTime requires an Update. So messing with this a bit I was wondering if I created a Class, was there a way for that Class to get systems events such as Update, Awake, etc. or are those events I would strictly manufacture other ways?

Is there another method, from a Class based approach, that would not use something like deltaTime like a Timer object or something?

Regards,

– Clint

Derive your class from MonoBehavior and you will receive Update, Awake, Start, LateUpdate, and maybe some more. If using C# you have to do this explicitly. With Javascript, you script defaults to deriving from MonoBehavior. Not sure when you use the Class declaration in a Javascript though.

You could probably use the .Net Timer class.

HHHMMM, thanks lfrog. I am using Javascript and my code looks like this for the Timer class right now:

class Timer {

	var time : float = 0.00;
	var timerState : boolean = false;

	function Update () {
		
		if (timerState){
			time += Time.deltaTime;
			this.Output();	
		}
	
	}

	function Start () {
		timerState = true;
	}

	function Stop () {
		timerState = false;
	}

	function Restart () {
		time = 0.00;
	}

	function Output () {
		Debug.Log("TIME: " + time);
	}

}

And I am instantiating it this way from another script:

function Awake(){

	var timer = new Timer();
	timer.Start();
}

Now, I am not getting any output messages, etc. so not really sure what I am doing wrong. Any help would be great!

Thanks!

-- Clint

Now that you memtion it I am not sure of the Unity JS syntax for inheritance. How about

class Timer extends MonoBehavior {
...

:slight_smile:

adding the extends MonoBehavior throws an error reading that:

I sort of recall that JS does inherit automagically from MonoBehavior but why no events? Does it or should it really need to be attached to a GO for that?

Thanks,

– Clint

I think this is a question for the Otee guys. If you don’t do the explicit “Class” declaration then the script inherits from MonoBehavior and the functions get called.

I just noticed on the online documentation that it is MonoBehaviour and not MonoBehavior. You may want to try it with the “u” (i.e. MonoBehaviour).

I tried it with the “u” and it didn’t give an error but didn’t work either. :wink:

Thanks for the help lfrog and I guess I will keep plugging and wait for a response from one of the OTEE guys. :-p

Regards,

– Clint

not sure why you’re not getting output. i tried your scripts and got console output showing the time. so your code’s ok. i added it to a gui text too which worked as well.

i had your Timer class on an empty, then added an update for the gui text on a gui text object.

function Awake(){ 
    var timer = new Timer(); 
    timer.Start();
} 

function Update(){
    guiText.text = Timer.time.ToString ("#,##0.0");
    }

Well one difference is I am not adding the Timer Class to a GO. I didn’t see anything in the console etc. Thanks for looking Pete and I’ll keep plugging.

Regards,

Clint

lol… i can’t read… :smile:

i think you have to. otherwise how do you get it in the hierarchy? but i don’t really get what you’re trying. like you said hear it from otee…

The Update function and Start function is not just magically somehow called.
It is called because you attach it as a component to a game object AND you use a plain javascript or inherit from MonoBehaviour (In either C# or Javascript). And because the behaviour is enabled.

If you want to have a seperate class which isn’t attached to a game object you need to call the functions yourself. Eg. from another script which just calls Update on this class.

But the question really is, why do you want to do this? Why not just write a normal javascript and override the Update function.

Pete,

Thanks for looking into it for me. :slight_smile: I will try to attach to a GO and see what happens… What you are saying does make sense.

Regards,

– Clint

Jo,

I was kidding about the automagically comment. :wink: I know it isn’t magic and that there is a structure etc. associated with how it works.

As far as overriding, that actually didn’t even occur to me and from a JS 2 point of view and what I am trying to do I am not real clear on how I would go about it. Could you provide an example?

Thanks,

– Clint

For implementing a Timer class you should not need to receive any events.

Instead of using Time.deltaTime, you could use Time.time instead and just recod the current time when you start and stop the timer:

class Timer {
    private var start=0.0;
    private var paused=true;
    private var paused_at=0.0;
    
    // Constructor
    function Timer() {
        Reset();
    }

    // Returns the current time
    function ReadTime() {
        if(paused)
            return paused_at;
        else
            return Time.time-start;
    }

    // Can be used to set the timer to a certain value
    function SetTime(i_Time : float) {
        if(paused)
            paused_at=i_Time;
        else
            start = Time.time-i_Time;
    }

    // Pauses the timer
    function Pause() {
        if(paused)
            return;
        else {
            paused_at=ReadTime();
            paused=true;
        }
    }

    // Restarts the timer
    function Start() {
        if(!paused)
            return;
        else {
            paused=false;
            SetTime(paused_at);
        }
    }

    // reset the timer
    function Reset() {
        SetTime(0.0);
        Start();
    }
}

Thanks a lot freyr, I appreciate the help!

Regards,

– Clint

I am not sure if I am doing something wrong but for me Time.time is always return 0.

I tried simply using the Class you provided had it attached and unattached to a GO when instantiated from. I have also just simply added Time.time to an Awake event all returning 0.

What am I doing wrong?

Regards,

– Clint

It’s is not a MonoBehaviour, so it should not be attached to a gameObject.

Just do something like this:

var timer : Timer;

function Awake() {
    timer=new Timer();
}

function Update(){
    print(timer.ReadTime());
}

Thanks freyr!

Still isn’t working like I would expect but I’ll keep hasing it out… It has to be something I am not doing right.

Regards,

– Clint