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?
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.
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
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?
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).
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.
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.
I was kidding about the automagically comment. 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?
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();
}
}
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.