Public Group

# Actions that span several iterations of the game loop

This topic is 4706 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

##### Share on other sites
Quote:
 Original post by J306When the player is "coming to a stop", things will be different. I know that I could just have a flag on the player object, called isSlowingDown or something...

Yes, you could ...

Quote:
 So my guess is that the proper thing to do would be to create a "GradualAction" type, and just keep a data structure of all GradualAction's that are currently happening. Then, every iteration, just call the actions in this list. Is my assumption correct?

Well, you COULD use that method...

Quote:

Not like that.

Normally for handling this kind of thing, you'd work by modelling intertia and friction of the player. This handles things like continuing after the player lets go of the forward key, not instantly reaching top speed when the player hits the forward key in the first place...

In a car, if you release the break and instantly floor the accelerator, does it immediately reach top speed? No. This is because of inertia, i.e. it takes a finite amount of time to change speed. This is acceleration.

I turns out, that modelling this is actually incredibly simple. There are two main methods of doing so, called "Euler integration" and "Verlet integration", but I used the former for a long time before realising that's what it was.

---

In the case of something like a FPS, friction doesn't just model friction, but also the player actively slowing down - but the engine doesn't care, we probably just consider it a stronger type of friction.

---

Needless to say, this is not a general mechanism, Euler integration (or Verlet) will only help you model intertia, not generalised timer tasks.

You were right in your assessment that a lot of tasks in games often need to be delayed or take more than one tick to complete.

Consider your FPS again - your character has a gun. He can fire it when the player hits the fire key. But it doesn't fire instantly, in actual fact there is a short delay while the character's finger gets on the trigger.

So you'd have to remember that the player had pressed fire, and remember how long ago that was. After the requisite time period, you'd then call the routine which actually creates the bullet and/or kills the monster as necessary (Some guns in FPS fire instantly, others have projectiles which fly at finite speed).

After the gun has been fired, the player can't fire it again instantly, he has to wait while the character reloads it (even if it's fully auto etc, just the delay is smaller). This of course, is a second delay, independent of the first.

What you'll want for the gun, is a finite state machine for the gun, say there are three states for the gun: ready, firing and reloading.

You'd need a timer which would normally be something like an int. This would store how much time is left before the current action is finished (in the case of ready, it isn't used).

Each tick, you'd check the gun state, decrement the counter if necessary, and update the state and/or call the fire routine. It's that simple.

For most other things, there's something like that too. A simple counter which is decremented and tested each tick usually suffices.

If you feel it's necessary, you could make a generalised state-machine handler. Alternatively, you could create a priority-queue scheduler which would know which things need to be actioned at which tick. This is more complicated, particularly given the fact that often actions might get cancelled (if player dies while the gun is reloading, the gun never gets reloaded).

If you have a very large number of timers running concurrently, a priority queue may be more efficient. But don't overcomplicate your code.

Mark

##### Share on other sites

ya, the player coming to a stop was a bad example. Thankfully you caught my drift with timed tasks.

1. 1
2. 2
3. 3
Rutin
20
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633655
• Total Posts
3013179
×