Newbie Asks: How To Timers and count time and random time

Non-platform specific questions.

Newbie Asks: How To Timers and count time and random time

Postby ggesterGamePro99 » Tue Apr 17, 2012 1:52 pm

Basically

1. How do i set a timer to start counting when the game starts. And if i decide to display the time, how do i display it on the screen?

2. How do I start counting time at the begininning of the game and keep track of it so can make things happen at random times OR at desired times?

For example: Game starts...then at random time, it starts raining
For example: Game Starts...you pass by a bomb, the bomb is activated...so the timer starts counting down( this timer is not displayed on screen), and once it's 0, the bomb explodes.

*****
So in other words, how do i keep track of a total time of the game. And then how do i keep track of doing other things using timers.
ggesterGamePro99
 
Posts: 44
Joined: Mon Apr 16, 2012 1:53 am
Score: 0 Give a positive score

Re: Newbie Asks: How To Timers and count time and random tim

Postby skydereign » Tue Apr 17, 2012 5:09 pm

You are thinking of timers backwards. Timers countdown events, and trigger code upon the timer reaching 0. So, a timer cannot be used to count how much time has passed. But, gE provides a global variable called frame. It records how many frames have gone by (really the number of the current frame). So if your game is running at 30fps, then when frame is 90, 3 seconds have passed. To display it just use a text actor.
text_actor -> Destroy Actor -> Script Editor
Code: Select all
textNumber=frame;

If you want to format it into seconds you can, using the modulus operator.

Now to make things happen randomly, you can just use the rand function. Since you want random, it isn't really dependent on how much time has passed.
Code: Select all
if(floor(rand(1000))==0) // 1/1000 chance (if this is in draw, then this happens once a frame)
{
    // do something
}
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Newbie Asks: How To Timers and count time and random tim

Postby ggesterGamePro99 » Wed Apr 18, 2012 5:58 pm

So are you saying if

i want an enemy to come out at a random time, and then decide at some point from then on..to execute a move..then i would need two timers, right?
For example: Come out at some random time between 10 seconds and 20 seconds after the game starts... Then 30 seconds after he comes out..decide to OR NOT decide to execute some attack

Would i do this like this?
--->use the game frame counter for the first part( count between 10 to 20 seconds)
--->then use the frame counter or some Timer to count down 30 seconds and then decide to execute an attack
ggesterGamePro99
 
Posts: 44
Joined: Mon Apr 16, 2012 1:53 am
Score: 0 Give a positive score

Re: Newbie Asks: How To Timers and count time and random tim

Postby skydereign » Wed Apr 18, 2012 6:57 pm

If you want something to occur randomly, you still have to define when it can randomly occur. If you know this, you can use a random timer. For instance if you wanted to create an actor 10-30 seconds after an event, then you could create a random timer using that range. I only mentioned frame because you were asking how can you tell how much time has passed. Timers though can be created whenever, so all you need to do is know what causes the random events to happen (and put the CreateTimer there).
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron