Global Timer? and other timery questions
oranjoose

Is there some sort of Timer object/manager in Wade? JavaScript's setTimeout and setInterval are a bit limited by themselves.

I noticed the onAppTimer event and setAppTimerInterval function, but it looks like onAppTimer only works on SceneObjects, and the event can't be made globally, i.e. in the app.js file (unless you perhaps made a blank SceneObject just for the onAppTimer). In addition, the setAppTimerInterval function appears to be designed to change the interval across all onAppTimer events (since the function belongs to the wade object). 

Finally, when trying out the onAppTimer event, it appears to be firing twice at each interval. For example, I logged something to the console in the onAppTimer event for a SceneObject, and it logs twice each interval, and when I cloned the object once, it fired four times each interval. Is this the expected behavior of this event?

Thank you.

All 5 Comments
Gio

I will look at what's happening with onAppTimer. It certainly isn't supposed to fire twice per interval, that may be a bug.

You are right, the onAppTimer interval is the same for every object. It's useful for things such as roguelike games where all objects move at the same time, at set intervals. It should also fire globally as well as pet object. Meaning that if you have a wade.app.onAppTimer function that should be called regularly. If it isn't, it will be fixed in the next release.

For what you are trying to do, it sounds like you may want to use SceneObject.schedule

It works a bit like setTimeout though, not setInterval (i.e. if you want it to execute perodically you would have to call schedule again at the end of your scheduled function). I can see how having something equivalent to setInterval would be handy, I'll see what we can do for the next version.

oranjoose

I took a look at the schedule function, but the problem with it is the same problem with setInterval or setTimeout. The problem is that when the game loses focus, such as when you change tabs, the game pauses, but the setTimeout and therefore schedule function's timer still runs. This is hugely problematic for games that have important mechanics scheduled on a timer, particularly a repeating timer (e.g. tower defense games, among others).

Or am I looking at this incorrectly? =)

oranjoose

Excellent work on the 3.3 update! It looks like it solved the problem with the schedule function's timer running while the game is out of focus. Thank you!

Gio

Thanks and yes, while adding the interval funciton I noticed that schedule was internally using setTimeout, and thought it needed refactoring.

Just something to be aware of: schedule and interval set a timer that is updated while an object is in the scene. If you remove an object from the scene, the timer isn't updated and the scheduled function will not be executed. This is to be consistent with the way schedule was working previously.

However, there is now a difference compared to the old behavior: if you then re-add the object to the scene, the timer will resume, and the scheduled function may be executed.

oranjoose

A couple comments, hopefully not too nit-picky =):

It looks like the unschedule function is the means to clear the timer set in the schedule function, as well as the timer set in the interval function. Using the same logic, I'm curious why you didn't make setting an interval done in the schedule function passing true or false if it is looping (or something similar). And to not be too obnoxious, I noticed that almost all functions in wade are verbs, such as schedule, setPosition, and playAudio, but interval is only a noun. This to me implies it is a non-function property, though I see why you would be hesitant to use the already-occupied setInterval function name.

Anyways, thanks again for adding this!

Post a reply
Add Attachment
Submit Reply
Login to Reply