Skip to main content

General Principles for Reducing Logic Overhead (Part 2)

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:

[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()
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.  


Popular posts from this blog

Back to Vinland.

I'm going back to my real time tactics project, Vinland 1936.
While working on the other project I overcame the problems which were stopping me from saving/loading the game and also cleaned up the base code a lot.

After a few weeks I'm getting near the the state I was in before.

Infantry are back to their previous state, and vehicles are running OK.
This time I'm going to push ahead with mocking up the combat system though before I work any more on the vehicle builder or graphical aspects of the game.

Map screen designs

I've been working some more on the map window. Right now you can only see the base, it doesn't show items, enemies or even doors on the map yet. These would be decals.

In the top window you can see the modified result of last night's tile based map. It looks good but there are some visual artifacts related to the problems I encountered yesterday, and as well it takes much more code and time to calculate.

The second window uses a cheap trick to fake an beveled look from a smoothed version of the 32x32 map. It uses black to mask unexplored areas.

Finally the third version is meant to look like a had drawn map. I'm using a cross hatching texture to distort it and unexplored areas are shown as blank map paper.

There's going to be a mechanic in game where you need to use some paper every level in order to activate the map for that level. From there it will fill it in automatically. Paper will be pretty rare so it might be worth keeping it safe for the more complex level…

Infantry combat and entering buildings.

I've been working a lot on the game recently and I've nearly rebuilt it to the level it was before. Past that maybe, since now I have the beginning of a working combat system and the ability to save and load the game.

Infantry can now occupy a building. It's quite an abstract representation, since they stay at the door and turn invisible. But they can then fire from one of the windows and take damage from shots at the windows too. I think I've set it up well so that when building damage and destruction is working then the system should continue to work.

For combat I tried some new ideas, but they didn't work out that well. It seems that it's important that viewing range should be further than shooting range. Now shooting range is pegged at 18 units of distance, while viewing range can extend out past that.

In the above image one unit has an officer, so has further viewing range. The other can only see as far as they can shoot, a dangerous situation since the en…