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

Vinland 1936

What have I been up to this month?

Well you can see it in a couple of development blog videos, here, here and here.

Vinland 1936 is a game I've been working on (on and off) for about 3 years. It is somewhat based on the old Nirval interactive game, Blitzkrieg;

I hope you've played it since it is one of the best games ever!!! (IMHO)
Blitzkrieg was a real time tactics game. You didn't build a base, or spawn units. It wasn't about rushing the enemy. You got a small number of troops and vehicles that could be replenished or repaired if you had access to a supply base and the right supply trucks, but couldn't be replaced if lost. Once your vehicles were destroyed and your infantry killed you were finished. You couldn't just churn out some more from your factory and have another go at rushing the enemy guns. This made you invest a lot in each of your units. They really mattered.

It was also procedurally generated. Each mission (except for the historical missions) was…

Reboot / Remake / Restart

Although the roguelike project was going well I had a few issues with some parts of the code, and the sheer size of the project was something I could see stretching away in front of me for years with no guarantee that people would actually want to play it when it's finished.

It's time to try something a little less ambitious.
I'm going full rogueLITE!

Using a lot of the code from the roguelike project, I started making a more limited game.
There will be a single character, combat will be more arcade like, there will still be a chance to upgrade and develop your character's stats, but they represent only a single class and have fixed equipment.

I've got a fun character, an interesting setting and an exciting story lined up. It still utilizes my low poly style, but things are going to be a little more cartoony.

Game play involves mostly chucking bombs at the enemy.

But there's also a lot of platforming, jumping from multiple levels is part of the game. And you ca…

Telling a story; Creating a Compelling Narrative.

Telling a story; Creating a Compelling Narrative. In this blog I will talk about my own recent brush with story telling and go on to talk about how tools from creative wring can help you to better author the narrative in your games, whether they have a traditional linear narrative or a procedurally generated interactive narrative.

Narrative and structure in traditional fiction  last week I started writing a story set in the world I'm developing for my game Vinland: 1936.

I hope the story will help me to flesh out my game world and develop my own expanded universe which will be a good place to set my games in the future.

After about a week of work, on and off I've progressed the story to outline stage. For each character thread I have half a dozen chapters which plot a course through the events of the story. Each thread is told from the perspective of a different character.

Actually I started writing as soon as I had my outline, but I've since gone back and deleted what …