Hi and welcome back! This time I'm going to talk about a trick I used on the old PS2 game '24 The Game' to save over 30% of a frame by hacking the game's monolithic entity system.
The monolithic entity system
You've probably all seen this design before:
Entity manager, stores a list of all entities
Each entity implements an Update() function
Every frame, the manager loops through all entities and calls Update() on each one
The primary problem with this design is it's polling nature; each entity is polled to do work, even when that entity may not have any work to do. This doesn't start out being a problem at the beginning of the development of a game when there are only a handful of entities, but it sure is when the game is full of 1000s of them!
In '24 The Game', entities were any object which you could interact with, which included: doors, characters, cars, guns, pick-ups, boxes and any other physics objects. They soon started stacking up in numbers, especially when you consider the game's draw distance.
In an ideal world, the system wouldn't have been designed like this in the first place. Something event driven would have been more appropriate for the majority of entities - so entities just sleep until they are acted upon by an incoming event, such as being shot, or collided with. While asleep an entity would have done no work and therefore taken no CPU time.
However, this was not to be since the game was in beta and rewriting the entire entity system seemed like a bad idea(!)
Hacking the monolithic entity system
So a more achievable method was needed, one which could be plugged right into the existing system without breaking everything.
The solution was discovered after making a key observation about the nature of the variable time-step we were already using in game.
In a game where the time-step varies from frame to frame (like nearly every game in the world), entities must adjust their behaviour to cope with a differing frame rate - for example, animations move forward a differing number of frames, characters move across the ground differing amounts per frame and so on.
This being the case, calling Update() on 'unimportant' entities every 2nd or 4th frame wouldn't break anything and would save a bunch of CPU time.
So, what is an unimportant entity? From the point of view of this system, an unimportant entity is one which is not currently interacting with the player, or is very small on screen, or completely off-screen.
Mitigating edge cases
Unimportant entities could temporarily have their importance increased after receiving an event, such as being shot, or collided with. This mitigated the problem of 'dumb entities' which would take a long time to react to events in this new system.
Screen-space size should be used rather than just plain distance when calculating importance, otherwise zooming in on a far away moving character would reveal jerky animation, as the animation controller only got updated every other frame.
Importance overrides will always be needed in some cases, like if you have a bunch of AI companion team-mates who are following behind the player.
Very unimportant entities could volunteer themselves to be excluded from Update() completely if they did no work there, such as most physics objects.
In the end this system saved 30% of a frame in general across all levels, sometimes a lot more which was a definite result. However, if possible don't design a monolithic system in the first place!
That's all folks
Thanks for reading, hope this post gives you some ideas! For more ideas check out my blog at http://www.wildbunny.co.uk/blog/.
Article Update Log
10 Aprl 2014: Initial release