FrameListener vs Entity Manager Update

Anything and everything that's related to OGRE or the wider graphics field that doesn't fit into the other forums.
Post Reply
Wraithdrit
Halfling
Posts: 64
Joined: Thu Jul 29, 2004 6:30 pm

FrameListener vs Entity Manager Update

Post by Wraithdrit » Wed Apr 20, 2005 8:49 pm

No, its not a help question... more of a general which would you use, and why?

From what little I've come to learn in game programming, most entities are controlled by finite state machines that have at their core an update function which executes the current_state's programming of 'what to do'. Also most entitys are generally controlled by an entity manager that holds a data structure of some sort containing the entities. Thus you could call an update function on the entity manager which calls each individual entity's FSM's current state execute function.

Then I read about the Ogre FrameListener and how you could add a framelistener for each object or a single listen that is inherited by all objects (see the big HOWTO).

So which is better, in a general frameListener, call a entity manager's update function, or on entity creation register a framelistener and remove it upon entity destruction?

Or is it just a preference thing? If so, what would your preference be?

- Wraithdrit
0 x

User avatar
DWORD
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 1365
Joined: Tue Sep 07, 2004 12:43 pm
Location: Aalborg, Denmark
Contact:

Post by DWORD » Wed Apr 20, 2005 9:11 pm

I would prefer a single frame listener calling the manager's update function. It just seems cleaner to me, but I'm not sure there will be much or any difference in performance. You'd probably have to use some kind of entity manager anyway if you register a frame listener for every entity.
0 x

Van
Hobgoblin
Posts: 512
Joined: Fri Nov 19, 2004 3:56 am
Contact:

Re: FrameListener vs Entity Manager Update

Post by Van » Wed Apr 20, 2005 9:55 pm

Wraithdrit wrote:Then I read about the Ogre FrameListener and how you could add a framelistener for each object or a single listen that is inherited by all objects (see the big HOWTO).

So which is better, in a general frameListener, call a entity manager's update function, or on entity creation register a framelistener and remove it upon entity destruction?
It is my experience that having more than one framelistener is not good. First, you are not guaranteed the order of framelistener execution therefore dependancies could be a problem; Second, performance suffers - at least in our MMO implementation performance suffered. Giving every object their own framelistener produced horrible results for us.

There is a thread on this forum where sinbad (I think it was sinbad) recommended avoiding using more than one frame listener and this was echoed by more experienced people than me. We followed their advice and we use only one frame listener which we call our Event Manager.

Our event manger class is an abstract of the framelistener. Objects requiring events inherit from an event listener class and register themselves with the event manager. During registration they inform the event manager of their priority and their dependencies. This provided us with stricter control over the order in which events get fired off and performace and expected results increased dramatically. Example of our event listeners are: UserIO, NetworkIO, PlayerObjects, NonPlayerObjects, SceneManager (our implementation), GUI's, PhysicsEngine, etc.

For example:
In the begining we were trying to manage FrameListeners for each subsystem. Our physics engine was a framelistner until itself. One of the biggest problems we had was the physics engine was never in frame (or sync) with the rest of the application. The "time between frames" was so radically different between all framelisteners that just trying to keep the application in sync was a disaster.

I won't even get into the disasters we faced when we were starting and stopping all of these framelisteners through out the application. The end result was that certain objects were running before their dependancies ran and the results were unexpected at best, crashing at worst - and the longer you played, the worse it got.

I highly recommend the single framelistener approach for an MMO (or any game really). Although, I do see other instances where having multiple frame listeners really doesn't matter.
0 x

User avatar
Kencho
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 4011
Joined: Fri Sep 19, 2003 6:28 pm
Location: Burgos, Spain
x 1
Contact:

Post by Kencho » Wed Apr 20, 2005 10:01 pm

Yep, there is indeed a performance hit. Consider you want to manage 2000 game objects:
1st case: A FrameListener per game object

Code: Select all

YourFrameListener::frameStarted (...) {
  mAssignedGameObject->update ();
}
This will be called by the manager 2000 times. So 2000 frameStarted's + 1 update per frameStarted = 2000 frameStarted's + 2000 update's = 4000 function calls.

2nd case: A FrameListener that manages all the game objects

Code: Select all

YourFrameListener::frameStarted (...) {
  for (int i = 0; i < 2000; i++)
    mAssignedGameObject[i]->update ();
}
This will be called by the manager just one time. So 1 frameStarted + 2000 update per frameStarted = 1 frameStarted's + 2000 update's = 2001 function calls.

I've always been told that function calls are performance expensive, so this is a performance improvement ;)

*Please, check my calculations :P I think I've done them ok, but I might have missed something :oops:

[EDIT]You replied faster :P[/EDIT]
0 x
Image

User avatar
:wumpus:
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 3067
Joined: Tue Feb 10, 2004 12:53 pm
Location: The Netherlands

Post by :wumpus: » Wed Apr 20, 2005 11:47 pm

I do use multiple frame listeners in a current project (like about 4), all are independent so that no dependency problems arise in this case. But I agree a framelistener for each object (or calling every object every frame in whatever way) is a very bad idea.
0 x

Wraithdrit
Halfling
Posts: 64
Joined: Thu Jul 29, 2004 6:30 pm

Post by Wraithdrit » Thu Apr 21, 2005 12:36 am

Thanks for all the replies everyone. I appreciate the feedback for this relative newb when it comes to Ogre (and c++ for that matter). Now if I could just get my managers implemented right, I'd be in business. That's tomorrow's task. :P

- Wraithdrit
0 x

Post Reply