Thread: Updating game objects...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    11

    Updating game objects...

    Hello, there. I'll start off with letting you all know that I'm new to C++, although I have prior experience with other languages (Delphi, BASIC, Pascal, and some scripting langauges). I've always been a game developer at heart, and now I've decided to dwell into the realm of C++ programming. I could go on, but this thread has been created for a purpose other than stating my dull programming history.

    In my planned game, it will (of course) feature objects of sorts that will be displayed on the map. This is true of pretty much any game.

    I'm probably over simplifying this, so I'll get to the point: What is the most efficient way to store/access object data that can be read and updated with the most ease possible? The two options I was considering were arrays or linked lists. My experience in the latter is significantly lower. Basically, all I need to do is be able to efficiently run through every object in the game and update it accordingly.

    Thanks in advance.

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    There is no one best way. It depends on the situation...
    Away.

  3. #3
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    >all I need to do is be able to efficiently run through every object in the game and update it accordingly.

    With that purpose in mind I would suggest an array. It is easier to run through them than it would be to cycle through a linked list.

    However, if you plan on adding elements to the array/linked list DURING game play....its up for grabs....

    Linked Lists can add fast because all you do is switch pointers...the problem is finding WHERE to add it to in the list...

    Arrays are a bit more cumbersome, because if you exceed your length, you must make a new array of a larger size to add elements to, however the search time is probably much less in an array than in a linked list.

    So, like confuted said before me, it all depends on the situation...
    My Website

    "Circular logic is good because it is."

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    11
    You've both made a valid point: I was rather vague as to what I'll be doing with these objects.

    Let's say the objects were projectiles, such as bullets or laser beams or holographic cheese wheels. I would need to be constantly updating their movement and collisions, as well as be able to add new projectiles whenever the player arbitrarly whacks the spacebar.

    I have the most experience with arrays, although if linked lists would work faster, than I'm more than willing to give it a shot.

  5. #5
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    As long as there are an (practically) unlimited number of bullets which will be (potentially) displayed, use a linked list. Especially since it doesn't matter if you have to update each bullet in a certain order.
    Away.

  6. #6
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    what confuted said is very true....and also, it really helps if you keep a pointer to the end of your linked list so you dont have to cycle through it every single time you want to add something...
    My Website

    "Circular logic is good because it is."

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    11
    What if there are multiple object types (classes) that need to be constantly updated?

    I know that I could make a dozen different linked lists, but is there an easier way to contain everything into one linked list?

    I'm not really experienced on the matter, so I'm assuming that I sound reasonably logical. :P

  8. #8
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    You could use a linked list with void pointers.
    Away.

  9. #9
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    I'm currently developing a game engine too, I chose to use linked lists to store game object data, however, instead of using void pointers, all of my game objects inherit from one class called Object, which has member functions like draw and update...
    and each linked list node has a pointer to Object.
    This works great for me.

  10. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I suggest not having all objects in the same list. What if you only want to traverse the bullets (and not the enemies)? If you have everything in the same list, you spend unneccessary time traversing objects you don't need to.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  11. #11
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    I go through the list once every frame to update all the objects, Idon't need to do it more than once, this maybe different for your engine though.

  12. #12
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361

    Hmm...

    Maybe I didn't read hard enough, but why hasn't anyone mentioned the use of vectors?
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

  13. #13
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    vectors are essentially the same as arrays with extra little nice features
    My Website

    "Circular logic is good because it is."

  14. #14
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by glUser3f
    I go through the list once every frame to update all the objects, Idon't need to do it more than once, this maybe different for your engine though.
    In my version, I test all the objects vs each others (like if they overlap each other). This would require n^2 checks if all objects are in the same list. If you split the list into several, you can dramatically lower that number. For example, you have no need of testing if two bullets overlap each other. At least not in my game ^^.

    It sounds like you don't test the objects vs each oteher. In thta case, you can as well go with 1 list.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D Game project requires extra C++ programmers, new or experienced
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 05-16-2007, 10:46 AM
  2. Open Source / Semi Open source game idea. Help needed
    By CaptainPatent in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 05-16-2007, 10:44 AM
  3. So you want to be a game programmer?
    By dxfoo in forum Game Programming
    Replies: 23
    Last Post: 09-26-2006, 08:38 AM
  4. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  5. Game Programmer's AIM Circle: Join Today
    By KingZoolerius66 in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 12-20-2003, 12:12 PM