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

Advice needed on tilesets...

I need some advice on which is the best way to handle building the dungeon. Right now I'm using prefabs for my dungeon, they have a north south east and west section for each "room": The basic tileset. This has several advantages, and also several disadvantages. Firstly I can have curved rooms, I can have tunnels and other interesting shapes. The tilesets can look quite nice with a little work. On the other hand I can't easily get the navigation data before building the map and once the map has been built I can't make changes to the layout, like having active pit traps or believable secret doors. Although the rooms are interesting, they are quite repetitive, and it takes a lot of effort to make even a few different variations. Also rooms are constrained to one size. A newer version of the tileset with a lot of variant parts for making more interesting rooms. To create a tile set is a real headache too. Planning how to lay out the UVs, trying to cra...

Upstairs / Downstairs.

I've decided to make my prefabs multilevel. Later this should allow me to add pit traps and other great stuff. It also makes it easier to line up stairs so that you can exit them on the same co-ordinates where you entered them. The prefab editor is pretty much finished, it just needs some code for loading up prefabs from a saved dictionary, so that they can be checked or edited. The entries will need to be forwards compatible, so I'll be loading each tile and then translating the indexes to a new array, that way if I add extra indexes or extra info (like traps or puzzles) I'll be able to update existing prefabs to work with the new standard. Click for a video.

Video Diary 8

Things are moving along well, there's been a lot of progress on the action manager side of things. Actions have finally moved to the UI, so you can initiate actions by clicking the appropriate button. I've set up some dummy actions to show what happens visually when actions are taken, but the actual dice rolls and such are yet to be integrated. The UI objects are also being added, though some are non functional or empty at the moment. Click on the image to see this week's development video. Every time I add something big I also add about a dozen small things. Like the selection box visualization. Previously this was using render.drawline, and old fashioned Blender function which can be impossible to see at certain resolutions, or at certain frequencies. I replaced it with a function that adds planes of the right size and scale in the right location. I also made all characters a little bigger. I still need to do some work with vectors and final target locations t...