Hacking the monolithic entity system

24_Snipe_4_3
Submit to StumbleUpon

Hi and welcome back to my blog!

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.

24_Snipe_4_3

Event driven

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.

Unimportant entities

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.

Results

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!

Until next time, have fun!

Cheers, Paul.

Submit to StumbleUpon

About Paul Firth

A games industry veteran of ten years, seven of which spent at Sony Computer Entertainment Europe, he has had key technical roles on triple-A titles like the Bafta Award Winning Little Big Planet (PSP), 24: The Game (PS2), special effects work on Heavenly Sword (PS3), some in-show graphics on the BBC’s version of Robot Wars, the TV show, as well as a few more obscure projects.   Now joint CEO of Wildbunny, he is able to give himself hiccups simply by coughing.   1NobNQ88UoYePFi5QbibuRJP3TtLhh65Jp
This entry was posted in Entity systems, Optimisation, Technical and tagged , , , . Bookmark the permalink.

3 Responses to Hacking the monolithic entity system

  1. Mehdi says:

    Awsome, really.
    I have read most of your posts and a really enjoy them, as well as very use full, i’m a big fan of you, please keep the greate work.
    I have a question,, even that it’s off the topic, i have start programming games since a year, and i have just started 3d, the probleme is that i have used always Tiles in 2d, but i can’t really find a reasonable way create maps, i would appreciate your answer if it’s possible.

    • Paul Firth says:

      Personally I would stick to 2d as it’s much easier to work with – 3d games are probably 4x as complex as 2d ones to produce. If you really want to, though – have you considered using unity?

  2. Mehdi says:

    Thanks for your reply, :)
    the reason why i don’t want try Unity is because you don’t create games on your way, but you doit on unity way, don’t mention the cost, the actual reason why i love game programming is because you learn a lot from it, and you enjoy what you do.
    as for 2d i can’t say that i get pro and i want to move on, cause i havnt even yet implement a good 2d physics engine, but the reason is that 2d is limited to the style of games i like, (Adventure, Arcade, heavy combat stuff ,:) ), and i have create a 2d game for Android that i’m happy with (i don’t know if i can put the link to the video of it), so i just want to try something new. the idea that i have now for 3d maps is a little bit similar to 2d tile maps, and it by creating a level in Blender for exemple and then load it as cubes, spheres and planes, but this will make every thing gameObjects, and i don’t think it’s good for performance. heightmaps are a good choise but i found them a unsuitable for some games, fps for example.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

WP-SpamFree by Pole Position Marketing