Skip to main content

General Principles for Reducing Logic Overhead.

Today I was reading Blender Artist's Game Engine Forum when I came across a post about very high logic overheads. Of course slow game performance is a real problem with a lot of indie games, and low frame rate can easily be enough to turn someone off your game before they even really get started. It's an important issue for indie game-devs and hobbyists alike.

[LAG=game over]
So I decided to write a blog post about it.

First of all I should point out there are lots of areas of overhead in a game. One area that often causes lag is graphics, a game engine like Blender is still using quite old methods of rendering and lighting so you can't expect too much from it. There are easy ways to reduce rasterizer overhead which I think I've talked about before many times over at Blender Artists.

The problem the BA poster was having though was specifically with Logic, i.e python scripting and the Game engine logic bricks.

With games developed by hobbyists it's common to find code which is not well optimized or not well planned. Often tasks are repeated even though they are not needed, or bottlenecks occur which can cause lag spikes.

[All the AI characters trying to change a lightbulb at the same time]

Lag can be manageable, but severe spikes can be a problem. There are 1000 milliseconds in a second, and the Blender game engine runs 60 frames of logic every second. That's 16.666ms every frame. If a frame takes more than that to calculate you start losing frames. 

My first finished game sometimes spiked up to 20ms logic which meant a lot of lost frames.
[Why is this my most popular game?]
I couldn't do anything now to improve that though, it's an old game and I didn't know much about project management so I made a lot of mistakes. I couldn't just go back and fix them, since it would mean a 99% re-write of the code.

As time's gone by I've found ways to improve the logic overhead on my games, nowadays I expect about 2-4ms for a finished game, and during development I usually see less than 1ms. I got to this point by adopting a number of design principles which help me to keep calculations to a minimum.

Here are a few of the principles I try to stick to:

[Can you even tell how many frames per second?]
1. I Don't do AI calculations every logic tic.
Most of my games are tile based, so moving from one tile to another is a single action. That one action takes about 0.5 to 2.0 seconds. Some events can interrupt that action (like the actor dying) but for the most part the actor only needs to calculate behavior once every action, instead of 60 times per second. Many older games were turn based and tile based, I remember sometimes waiting up to 15 minutes for a turn to finish and the AI to do all its calculations. You don't have to go this far, but having a scheduler or set duration actions is better than just continuous calculation. Most humans can't notice a half second delay in an AI's reactions.

[What's happening off screen? Who cares?]

2. I Don't calculate things off screen.
If an enemy is off screen you don't have to do anything with it. Put it to sleep until it gets close enough to interact with the player. Better yet, only spawn enemies when the player gets near. If you're using a sprite animation, you don't need to update it when the agent is off screen, only when you can see the enemy. You can use frustum culling or ray casting to check if an agent is off screen.

[for agent in agent_list:]

3. I Work with lists of different kinds of entities and manage them in different but standardized ways.
Each of my projects has a main loop which shepherds all the different aspects of the game. Lots of calculations are done centrally in the main loop so they don't have to be repeated for each element further down the line. It should be easy to get a reference to the nearest enemy, or the distance to the player from any enemy without doing the calculation again for every single entity. Agents, particles, lights etc... each have their own sub process. Particles don't need to do agent things and agents don't need to do particle things. I keep types of things separate.

[How can you make a game without A*, is it even possible?]
4. There are other, ways of doing things which are not the latest or the best but they are better for performance, it pays to research old games.
Most modern games use a variant of A* for AI navigation. If you're using the nav meshes in Blender you're using something similar. But in Python this can be slow, cause massive Lag spikes or require multi-threading to manage the overhead required. I found an old "dumb" AI trick that only checks the tiles surrounding the agent to see which is nearest the target and then keeps a list of those already visited while moving only clearing the list if it can't go any further. The result is much, much faster than even the most optimized version of A* in python and gives a result that can be described as "good enough". Most players won't know the difference, but it allows dozens of agents in the game at the same time with almost no overhead. (here's a video of an old project with around 100 enemies running at about 4ms, notice that when only the player is in the screen Logic is at around 0ms!). It's also easily adapted to allow fleeing or patrolling behavior. Knowing how programmers did something when they only had 128k of ram can help us to find ways to reduce overheads in our own games to a level that approaches zero. (Here's an excellent article on the AI of Pacman for starters).

[bullet time, collision sensor=True]

5. I consider multiple ways of doing something and decide which is likely to have the lowest overhead.
An example is bullets. One way to do things is to have a collision sensor on the bullet and detect when it hits an enemy. Another is to have a collision sensor on the enemies and detect when a bullet hits them. If there are 7 enemies but 700 bullets on screen it's easy to guess which way will save the most calculations. Sometimes this requires a bit of lateral thinking to fix holes in the design, in this case the level also needs collision sensors for when bullets hit walls.

[You've got mail!]

6. I (often) use messages and callbacks rather than continuous polling.
Rather than making any calculations or changes to the main scene, my UI scene just records button presses or mouse movements and turns them in to messages. If I click the menu button it sends a message "menu_clicked" to the main controller in the main scene and the calculations are done there. My UI is therefore mostly inactive, just waiting to receive input or return messages from the main controller. My agents receive messages from the main controller too, instead of checking every frame to see if one of a dozen things have happened (keys pressed? mouse moved? buttons clicked? object collided?) they just check to see if they have received a message. If they have, then they have to parse the message and act accordingly, but if not they get to sit back and do noting (or continue doing their current action). I have to admit though this is one area where I'm still trying to develop. It's often easier and safer to just poll something rather than trust that nothing changed since the last message was received.

These are design decisions not a roadmap, not all of them will be useful for every type of game, but if you're having trouble with Logic spikes they might be helpful. The point is to try different ways of doing things and see which has the best result for you.
There's no silver bullet for fixing the problem of working with a slow scripting language like Python, you just have to take care to work within the limits that imposes on you. There's no point blaming the game engine or the Language for not being fast enough, without good project management and planning it's unlikely that moving to a faster language or a better game engine would result in a better, smoother running game.


  1. There were a few things I missed such as the use of states and I needed to talk about the different definitions of "off screen". But it's too late at night now so I'll write a part 2 tomorrow, or when I have time. :)


Post a Comment

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.


I finished working on the code for adding foliage and having some extra time I decided to experiment with the code for rockets.

The original idea I had was that rockets would be large vehicle components that can be fired very quickly, regardless of how much manpower is used for reloading.

They would use up a lot of ammo, so they would run dry after a short but devastating barrage.
The problem here is that it's easy to take advantage of this by adding a lot of ammo, which is much smaller than in bulk than the rockets.

There's also the problem of firing large caliber rockets. In real life rockets of up to 30cm were used, but I think that will be too powerful for the scale of combat in this game.

lol. Somehow that one trooper survived the mother of all explosions...

A 30cm rocket could contain nearly 30KG of explosive. That would be a very large explosion.

I've tried to balance the game by using a simple equation to make bigger guns more powerful, but hopefully not too powerf…

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…