timing for game objects

Started by
3 comments, last by andrewk3652 19 years, 4 months ago
hi all! i am thinking how can i make things according to their timings , suppose if game is working and user dont play the game for 10 seconds then character start doing dance etc something like that... i know how can i make this but i dont know how can i store timing for every object? what is the professional way? thanks in advance
Things has been changed but the just the way it is....
Advertisement
Well assuming you just wanted to do this for the player's avatar, you could just have a variable in your game loop - let's call it PlayerIdleTime - and update it for every second that the player is not doing anything. When that variable suggests that ten seconds have passed, then you can change the avatar's state to idle, and then you can have your animation loop render them in an idle pose. Then whenever your input loop detects input, you can resume normal animation on the avatar.

It's pretty simple, really. Take the following pesudo-code example:

while game is running {    read user input {        if (user is doing something) {            // act according to user input            // set animation and player state to whatever            // is appropriate for the input (jumping,             // attacking, using item, etc).        }        else {            if (one second has passed since last loop iteration)                PlayerIdleTime += 1;        }    }    if (PlayerIdleTime >= 10) {        player.state = IDLE;        player.animation = IDLEANIM;    }    render();}


As for storing it for EVERY object, just add a member IdleTime to every object's struct or class (or whatever you use), and increment that in similar fassion for every second that item is not acted on.

Hope that helps.

=========================Buildium. Codium. Fragium.http://www.aklabs.net/=========================
In my opinion, the best way to do this is through states. In general, my code contains 2 pieces of information:

- The current state
- The time the state was changed

This is a great way to work AI and behavior, also commonly referred to as a Finite State Machine.

Say you push up on the joystick. This should make your character walk up, and set the current state to WALK (could be an enum, or constant int, string, or whatever you want). At that point, the time is recorded. I record in absolute time in ticks. Then say the player lets go of the up key. This would set the current state to stand, and again, record the time.

What you then do is have a switch statement of some sort of branching logic that does the appropriate action for each behavior. In your STAND state (whatever you want to call it) you could do something like:

if(currentTime - lastStateChange > 5) // maybe for 5 seconds
// set the state to tapping foot or picking nose or whatever.

You'll find that this kind of setup gives you a lot of freedom. YOu'll know both what the player has been doing something and how long he's been doing it.

--Vic--

If it's not implied, this information should be stored for every game object. So you can have different states for enemies, players, game objects, etc.
Thanks , i think both techniques are fine but finite state machine is a better solution for this ill work on it .. thank you guysssss for your help
Things has been changed but the just the way it is....
Pretty much the exact same thing I said. :-)
=========================Buildium. Codium. Fragium.http://www.aklabs.net/=========================

This topic is closed to new replies.

Advertisement