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()
self.manager.agents_update()
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.  

Comments

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 …