In my post yesterday I forgot to mention a few things which would be even more helpful when trying to make your game run smoother on low end machines.
Firstly I forgot to mention states.
Probably the biggest improvement you can make to your code if you're not already using them is to add behavior states.
When your agents are doing an action, that action should be a separate state. They should concentrate only on that state, all other calculation should be avoided.
Lets' start with a sleeping state:
Agents in a sleeping state should only be checking if they should wake up. That's it, nothing else. If your agent is off-screen it's probably best off in a sleeping state. This will keep calculations for that agent to a minimum.
Next up is an active state:
Running, fleeing, flying, waiting, hiding etc... these are open ended states that can be ongoing until something happens to change them. They need to do some calculation, but it should be kept to things relevant to their state. A running dog would need collision detection, movement and navigation. A hiding dog would need line of sight calculations to see if it has been detected, but not navigation.
Another state is the action state:
This state is not open ended, it has a start and a finish and care should be taken to track its progress. By tracking the progress (0.0 - 1.0) you can often avoid continuous calculation, instead only calculating start and finish values and interpolation them.
An example in my game is using rays to detect ground height and the angle of the floor. Casting a ray 60 times a second for every agent can be costly. Doing it twice a second is not.
If the action state is a state like shooting or casting a spell, again calculations should focus on that. There's no need for an agent in the middle of shooting to be making navigation calculations.
Finally the dead state:
Like the sleep state a dead agent should not be doing anything. My very first game I forgot about this and was confused as to why dead entities were still eating Logic calculation time. It's probably worth having a dying state, where it plays a death animation and drops loot etc... and then a separate death state where it just lays there doing nothing.
Something else I forgot to mention yesterday is that it's a good idea to add your own profiler.
Your game's Logic can be broken down in to several areas, it can be handy to list these separately in your profiler. If, like me, you're running a central game loop it's easy to record times, just use the python time module:
Just record the time before the process and the time after.
Add it to a central dictionary and print it on screen.
A final thing I wanted to talk about was my comment about being off-screen.
There are different degrees of off-screen, depending on the type of game.
There are basically four cases:
Both the player and the agent can see and interact with each other. (on-screen)
The agent can't be seen, but they can see the player. (partially on-screen)
The agent can't see the player, but they can be seen. (partially on-screen)
Neither the player nor the agent can detect each other. (off screen)
It's safest to treat partially on-screen as on-screen and do all calculations normally. But if an agent is off screen you can save a lot of calculations by putting them in to a sleep state.
Firstly I forgot to mention states.
Probably the biggest improvement you can make to your code if you're not already using them is to add behavior states.
When your agents are doing an action, that action should be a separate state. They should concentrate only on that state, all other calculation should be avoided.
Lets' start with a sleeping state:
Agents in a sleeping state should only be checking if they should wake up. That's it, nothing else. If your agent is off-screen it's probably best off in a sleeping state. This will keep calculations for that agent to a minimum.
Next up is an active state:
Running, fleeing, flying, waiting, hiding etc... these are open ended states that can be ongoing until something happens to change them. They need to do some calculation, but it should be kept to things relevant to their state. A running dog would need collision detection, movement and navigation. A hiding dog would need line of sight calculations to see if it has been detected, but not navigation.
Another state is the action state:
This state is not open ended, it has a start and a finish and care should be taken to track its progress. By tracking the progress (0.0 - 1.0) you can often avoid continuous calculation, instead only calculating start and finish values and interpolation them.
An example in my game is using rays to detect ground height and the angle of the floor. Casting a ray 60 times a second for every agent can be costly. Doing it twice a second is not.
If the action state is a state like shooting or casting a spell, again calculations should focus on that. There's no need for an agent in the middle of shooting to be making navigation calculations.
Finally the dead state:
[Dem bones] |
Like the sleep state a dead agent should not be doing anything. My very first game I forgot about this and was confused as to why dead entities were still eating Logic calculation time. It's probably worth having a dying state, where it plays a death animation and drops loot etc... and then a separate death state where it just lays there doing nothing.
Something else I forgot to mention yesterday is that it's a good idea to add your own profiler.
[Yes, it's not great that particles are eating more logic than agents, but also not unusual.] |
Your game's Logic can be broken down in to several areas, it can be handy to list these separately in your profiler. If, like me, you're running a central game loop it's easy to record times, just use the python time module:
agent_time = time.clock() self.manager.agents_update() agent_time = time.clock() - agent_time
Just record the time before the process and the time after.
Add it to a central dictionary and print it on screen.
A final thing I wanted to talk about was my comment about being off-screen.
There are different degrees of off-screen, depending on the type of game.
There are basically four cases:
Both the player and the agent can see and interact with each other. (on-screen)
The agent can't be seen, but they can see the player. (partially on-screen)
The agent can't see the player, but they can be seen. (partially on-screen)
Neither the player nor the agent can detect each other. (off screen)
It's safest to treat partially on-screen as on-screen and do all calculations normally. But if an agent is off screen you can save a lot of calculations by putting them in to a sleep state.
Comments
Post a Comment