Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are good techniques to handle state and state change in game development?

I'm an experienced developer new to game development, and I have made a reasonably simple but working 3D platformer game and am looking for ways to make the architecture more scalable.

What is a good way to handle state in game development?
A very naive approach is to have multiple booleans like:

time_stopped = True
time_slow_motion = False
level_loaded = True
level_paused = True
level_completed = False

This is very inflexible since a developer error could lead to time_stopped = True and time_slow_motion = True at the same time, which would destroy game logic.

I'm looking for a better approach in the direction of:

class TimeState:
    STOPPED = 0
    SLOW_MOTION = 1
    NORMAL = 2
    current_state = 0

    def isStopped(self):
        if current_state == self.STOPPED: return True

But how would I handle multiple groups of state in a good way? I could create a LevelState which had LOADED, PAUSED and COMPLETED states. But how do I handle overlapping groups like when LevelState and TimeState PAUSED should always be the same? Is there a good design pattern or a good practice on how to solve state management?

What is a good way to handle state change?
My thoughts are to either give the state object eg. LevelState callback functions to execute on any given state change - or to poll the state object each game loop like if level_state.changedTo() == LevelState.COMPLETED: doStuff(). Are there better ways to handle this? Message queues/events? I don't want to embed logic into the state objects.
I'm also interested in if one should handle both changedFrom and changedTo events, and what would happen if logic triggered by changedFrom changes the state of the object before the changedTo logic is triggered - who wins?

I don't want implementation/language specific answers but tips on a good way to think architecturally wise.

like image 693
Anton B Avatar asked Sep 07 '11 18:09

Anton B


People also ask

What are the 5 stages of game development?

Sometimes game development is a challenging, painful process — but nothing compares to seeing the pure elation of someone experiencing your game for the first time. We spoke to game developers and boiled down the game development process to the following five stages: Prototyping, progress, ship, watch, and repeat.

Which process model is best for game development?

Though it comprises a few drawbacks, yet the spiral model is considered the best gaming development technique so far.

What sort of tools are typically used during the game development process?

Photoshop, 3ds Max, Maya, and Blender are all popular examples of graphics tools. Like game engines, graphics tools are often integrated with other tools used in game development. One of those tools is version control.


1 Answers

This is such a common thing that there is even a pattern: The State Pattern

At the start, it seems like this approach has some overhead, but as the logic gets more complicated you will see the benefits.

like image 125
fabrizioM Avatar answered Nov 15 '22 03:11

fabrizioM