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

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 how alive the subject look…

How to... build a strong art concept.

So you want to make some art assets for your game. The first on the list is a Steampunk Revolver for your main character to shoot up Cthulhu with. Quickly opening your internet browser you start with a Google image search. Ah, there is is!

It might be a good idea to find a few influences so you don't accidentally end up copying a famous design.

Just mash them up and you're ready to go! Off to your favorite modeling program.
But wait! isn't there more to building a strong design concept than that?

Of course there is.
One of the diseases of modern design is that of recursion. Everything is a copy of a copy of a copy. This is especially a problem with "historical" concepts. Over the course of that recursive process the concept becomes infected with modern design elements, and ends up looking very similar to everything else that anyone else has ever made.
If you want to come up with a really fresh idea, you have to get beyond secondary references and go look at real …


Ok, so it's not exactly skynet, but I have got my first AI state working, kind of.

The first state is "HOLD" in which case the agent stays in place where they are and shoots at any unit that comes in range. When I started writing this module, I found that the existing method of triggering actions wasn't good enough to allow the AI to choose the best weapon or target. It worked by simply sending a command to the unit to trigger the currently selected action.

If the action is valid, it triggered, if not it didn't.
That's fine for play controlled units, as that's all they need to do. But AI needs to know in advance if the action is valid. The player can get that info from UI feedback, but that wasn't available to the AI player.

There were three problems:

1. The UI feedback duplicated code in the action trigger function. These  two sets of code could get out of phase so that UI feedback was wrong.

2. The action trigger didn't give enough feedback for …