5 Replies - 729 Views - Last Post: 03 October 2012 - 09:59 AM Rate Topic: -----

#1 RollaJ  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-July 11

Active game objects and Update()

Posted 03 October 2012 - 08:10 AM

So I posted a while back coming back to game development after a few years developing tools for autocad professionally. After your help I decided to go back to XNA and so far so good.

Im experimenting with the best way to update game objects both player controlled, ai controlled and background objects.

My current method is all objects have an isActive variable and then I loop through all objects calling their Update() function if its set to true.

Looping through lots of different lists of objects just to find a few active ones seems like a bit of a "waste" though. Are there any other recommended methods? Or is it really ok to do it this way?
Just realised the range of potential active objects might help in your advise so a rough estimate is 100-120 unique game objects on screen at once, of which 60-70% will be isActive = true, with 90% of those jumping between isActive = true and false.

Maybe when an object is set to active adding a pointer to it in a game manager collection of active objects and removing from it when it is no longer active therefore eliminating the need to search?

Is This A Good Question/Topic? 0
  • +

Replies To: Active game objects and Update()

#2 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 581
  • View blog
  • Posts: 1,292
  • Joined: 24-April 12

Re: Active game objects and Update()

Posted 03 October 2012 - 08:52 AM

I don't know if there is an absolute best way. But rather than iterating through a huge list to discover who's active and who's not, you could have a list of who's active. You could remove them from the list when they de-activate and add them to the list when they are activated.

You could use several different data structures to store that including a simple array. In XNA you would probably use the List<> object.
Was This Post Helpful? 0
  • +
  • -

#3 RollaJ  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-July 11

Re: Active game objects and Update()

Posted 03 October 2012 - 09:06 AM

View PostBBeck, on 03 October 2012 - 08:52 AM, said:

I don't know if there is an absolute best way. But rather than iterating through a huge list to discover who's active and who's not, you could have a list of who's active. You could remove them from the list when they de-activate and add them to the list when they are activated.

You could use several different data structures to store that including a simple array. In XNA you would probably use the List<> object.


Yeah that was the second method I thought of. Purely for discussions sake, would it make a difference if there were 200/500/1000/X objects in the game to look through. Or is the process so insignificant at run time that its pointless putting much thought into it.
Was This Post Helpful? 0
  • +
  • -

#4 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 581
  • View blog
  • Posts: 1,292
  • Joined: 24-April 12

Re: Active game objects and Update()

Posted 03 October 2012 - 09:50 AM

Well, numbers always make a difference. I mean, would you go through this kind of trouble for only two objects? But in general, I would either not worry about it because it's too few objects, or do something along the lines of what I suggested, knowing that it will get even better as it scales up.

As a general rule, the less time you waste, the faster things will run. If you check something that doesn't need to be checked that's a waste of time (and computer resources). So, if you can figure out a way to not test something that doesn't need to be tested, then you save time and everything runs faster, sometimes exponentially faster.

In this case, by maintaining a list of only the items that are in the state that need to be worked with, you eliminate a huge number of tests that would have just been a waste of time.

If that's just two or three items it may be more trouble than it's worth.

Basically, you want to look for "waste": waste of time, waste of memory, waste of disk space, etc. The fastest way to do anything is to not do it. You can't get faster than not doing it. And if it's time spent wasting, then you definately want to not be doing it.

For a game, pretty much all that matters is "Frames Per Second". "Basically", if it runs at 60 FPS or faster it doesn't matter what it's doing. And if it runs slower than that it doesn't work, and you need to come up with a design that does work. But as a general rule, you don't want to shoot for 60 FPS but instead shoot for "as fast as possible". Then hopefully you wont be ever running that slow.

How you figure out what to process and what not to process really is specific to your program. And even if you have a great way of doing it, that doesn't mean that there isn't a better way to do that no one has ever thought of.

Especially for 3D there are various methods for deciding what to process and what not to process. Culling is pretty common. There's no need to draw anything that can't be seen. So one thing they use is view frustrum culling to determine what's in front of the camera and what can be cut out before any attempt is made to draw it.
Was This Post Helpful? 0
  • +
  • -

#5 RollaJ  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-July 11

Re: Active game objects and Update()

Posted 03 October 2012 - 09:57 AM

Some good points to keep in mind, I appricate the time taken to write it out.

Yeah I've dealt with culling before in my autocad work. Our C++ programmer gave me a crash course with our in house software. Something along the lines of clockwise and anti clockwise vertex drawing order, if its one way its facing you if its the other its behind. Among some other stuff.
Was This Post Helpful? 0
  • +
  • -

#6 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 581
  • View blog
  • Posts: 1,292
  • Joined: 24-April 12

Re: Active game objects and Update()

Posted 03 October 2012 - 09:59 AM

This is not the XNA forum (we have a specific sub-forum for XNA questions), so I've about half purposefully tried to avoid talking about XNA here much. But since you asked:

You should seriously consider "game components". In XNA you have two different types of game components: update-able and draw-able. Updating game components don't draw on the screen. Draw-able game components update and draw.

They are almost exactly like regular classes in XNA, except they have specific funtionality built into them. Specifically, they seem to inheriet basically from the Game class. So, their update gets called "automatically" every time the Update gets called in Game1. Likewise the Draw and Initialize also get called "automatically" every time they are called in the Game1 object.

So, you just add them as a component at startup, and then their update is called for you every frame. You can control what order they get called in though. And you can also disable them so that they go "inactive".

Adding a game component is pretty much as simple as initializing and adding it like this:
            Character = new PlayerCharacter(new Vector3(0f, 0f, 0f), new Vector2(0f, -1f), this);
            Components.Add(Character);




In my holodeck tutorial, the very last thing I do is turn the character/camera into a game component where it had previously been a class.


http://xna-3d-101.co...kPartSeven.html

If you go through that tutorial, I would recomend starting it over from the beginning or just downloading the source code at the end and running it.

There's an Enable and Visable property that determine whether the game component's Update method gets called and whether it's Draw method get's called. So, that's similar to what you've been doing.

http://msdn.microsof...y/bb196401.aspx

You can Google "XNA Game Component Tutorial" or something like that to get even more info.

Personally, I use game component's at a fairly high level. Like if I had 30 different types of enemies, I would not create a game component for each, but one game component for all enemies. I "might" create a class for every enemy, and possibly inherit them from a single game component. I've been using a camera class lately where I did exactly that; I created a template class from a game component and then both of my camera classes were children of that template class.

But I would not necessarily make every class a game component.

This post has been edited by BBeck: 03 October 2012 - 10:14 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1