Attached is a very small demo of my alternative to timers..
why not use timers ?
well they are handy for many things, but unfortunately they can get messed up with game lags.
They depend on REAL time.. not in game time... which means if the game lags, or "windows" decides at a critical moment to do something in the background.. then the timers will be messed with..
This is ok if they are not critical to game play.. for example setting a timer to destroy a "killed" actor after a period of time..
but if your game requires careful timing... for example player only has 2 minutes to get to a safe zone, or player has only 10ms to cross a hot river of lava ... well... then timers can be a problem..
I was going to write a 'function' to solve this, but then thought of a much easier way, one that also has other advantages..
in the attached demo i have created a "actor" variable called fcount. ( short for "frame count")
HOW TO FOR NEW USERS
use the variables button in any script window and add the variable as
name: fcount
as: integer
type: Actor variable
This creates a new variable called fcount for EVERY actor and it can be accessed in the same way as the actors x, y positions or actors 'sprite' width... eg: player.x player.y pl;ayer.width OR player.fcount
what I do then is, on every actor I need a timer for, I add to the draw function
fcount=fcount+1;
This means every frame that is drawn adds one to fcount FOR THAT ACTOR. and only adds it when the frame is drawn.. so it does not depend on real time but actual frame rate of game, if the game lags, the count lags..
in the attached example I have the enemy actor changing direction after every 20 fcounts
fcount=fcount+1
if (fcount>20)
{
xvelocity=-xvelocity;
fcount=0 //reset counter to 0
}
Which means the actor will always change directions at the right place, at the right timing, even if game lags..
NOTE: EACH ACTOR HAS ITS OWN FCOUNT
to show how all actors can then use each others fcounts, the demo example shows the player actor updating the text actor called "time" but doing so in the 'draw actor' of the player
the player also accesses the enemies fcount to move itself 10 pixels each time the enemys count=10..
This means you could for example freeze a enemy for 10 fcounts, and the player could access the enemies fcount to find out when he could be shot at again.. etc that is, any actor can access any other actors timer for whatever purpose and can reset it to 0, jump it forward, etc etc
note: if you want to use it as a Game clock simply divide the frame count by the number of frames your game is running at and you will get a clock that depends on the game and not realtime, so it will not be effected by lag..
in the demo example the player actor updates the text actor with fcount/30 to give a 1 second timer.. (thirty frames a second)
this means ....if a player has 10 seconds (300 fcounts) to cross the lava, he will always get 10 game seconds..
In other words fcount can be used for anything timers do, but easier to set up, and more accurate.
hope that makes sense and is useful to somebody.. if not let me know and I will try to explain better or do a bigger demo
feral
note: if you need more times... just create more variables.. eg: sleeptime.... or holdbreathcount etc