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.


Conclusion:
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.

Comments

  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. :)

    ReplyDelete

Post a Comment

Popular posts from this blog

Automating Level imports from Blender to Godot

  Recently I've been making some levels in Blender an importing them into Godot. There are only about 7 or 8 shaders for each level, not counting dynamic objects which will be added later. But to improve rendering performance, it can be a good idea to split the meshes up into sections. At that point you might be faced with a list like this: Or it might be even more chaotic, if you didn't use simple names for the objects in your level. So it can take a long time to sort out all the meshes, make them unique and add textures and so on. Blender imports with simple Blender textures, or with placeholder materials. This is sometimes OK, but if your Godot shaders are very different to those used by Blender, it means applying new materials to every mesh object in the level when you import the scene. I found that during the design process, I was importing and readying a level several times before I was happy with the final layout. So at first I was wasting a lot of time. In Blender, I us

Dynamic terrain in GODOT

Long time no posts. I haven't been keeping up with the projects I started. At first it seems fun and exciting, but I always run in to limitations in the setup, plus the grind of just making stuff without any real challenges... It ends up being something that I don't want to commit to. So right now I'm just messing around with some ideas and see what comes out. No commitment to a bigger project, just some time to try new things. This week I've been working on procedurally generated terrain.  In the past, there were some big limitations on how I approached this, because the game world had to have the whole map, from the micro to the macro. I had to choose a scale somewhere between, which meant I couldn't have really large maps, or really small details. I think I've found a way around that. Below you can see two types of map data coexisting on top of each other. The wireframe is the collision data, used for physics and for clicking on the map, to move characters ar

Make your game models POP with fake rim lighting.

I was watching one of my son's cartoons today and I noticed they models were using serious amounts of simulated rim lighting . Even though it wasn't a dark scene where you'd usually see such an effect, the result was actually quite effective. The white edge highlighting and ambient occluded creases give a kind of high contrast that is similar to, but different from traditional comic book ink work. I'll be honest, I don't know if there's a specific term for this effect in 3d design, since my major at university was in traditional art. I learned it as part of photography. You can find plenty of tutorials on "what is rim lighting" for photography. It basically means putting your main sources of light behind your subject so that they are lit around the edges. It can produce very arresting photographs, either with an obvious effect when used on a dark subject ... ..,or as part of a fully lit scene to add some subtle highlights. See ho