Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Kerith

Linked Lists and arranging world data

This topic is 5228 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Using linked list how would I arrange specific data. Create a master object that holds either a item class or a npc class within it, or a seperate linked list for npcs and items? I''m sort of lost on how it should be handled.

Share this post


Link to post
Share on other sites
Advertisement
If you want to make a single list that will render all types of objects in your world, you first make a base class, and make certain functions on it virtual. Assuming you're using C++, it would be something like this:

class CObject
{
virtual void Render();
};

class CItem : public CObject
{
void Render();
};

class CNPC : public CObject
{
void Render();
};

in your engine just keep a linked list of CObject pointers and then you can put any of the classes based on CObject in there. Since render is virtual in CObject, when you go through all of them with ->Render(), it will call the render function for the class you created it as.

That was just sort of a simplified explaination, the topics I think you're interested in are inheritence and polymorphism (using virtual functions). You can find plenty on them if you google for it.

[edited by - Subconscious on February 26, 2004 7:40:54 PM]

Share this post


Link to post
Share on other sites
There is not one universal organizing system ... and in fact, for most real world systems, the same object will be in different lists at different times, and even multiple at once ...

the primary things to solve for your system are: WHICH lists OWN the object (who is responsible for delete it) and which lists are just temporary VIEWS of the world ...

of course if you use a ref counted system then there doesn''t have to be a specific owner, or at least no special code for the owner list ... but many people still find it usefull to have one central list to provide lookup access, and also for keeping the object around while it is in a state of flux in other lists ...

I have different ways of thinking for different subsystems of my program ... my game is turn based and non-3d ... so i don''t have a lot of complication with the WORLD part ... but even in my sci-fi game there is a UNIVERSE, which holds an organized system of all the PhysicalObject(s) in my game at a given time ... it isn''t a list though really ... its a heirarchy ... it''s a list of solar systems and fleets, which in turn have planets and ships .. etc ...

but that''s not the only game object list ... there are also ship designs, technologies, players, races, etc ... all of these non-physical items must be around somewhere too ... which is why I have the GameObjectManager which is basically a list/database of all the "game" objects ... things which might be saves or loaded with the game .. things which are not items like bitmaps, etc ...

these items are in other lists, managed by my ScreenManager, SoundManager, etc ... The SoundManager is simple, his job is to load sound files for you, reuse sound buffers, and allow universal operations like MuteSound, Pause/ResumePlayback (not implemented) and such ...

My system isn''t the greatest ... but I thought it might help trigger some ideas of your own ... for your needs ...

There MIGHT also be a general purpose ObjectManager if you use ref counting everywhere in your program ... and it''s just the list of all ref-counted objects, who''s primary purpose is to allow you to profile how many you have at any given time, what kind of ref counts you have, how much memory .. etc .. and mainly to detect if you get to the end of your program and you still have ref couns which don''t go to zero (sure sign of a problem somewhere) ...

Share this post


Link to post
Share on other sites
another usefull tidbit ... at my last job, over 2 years ago .. there was a system in which managed an audio router and it had a list of patch points, ordered by their physical address ... these items also had a logical id, which was used to look them up in many cases ... so there we''re two nearly equivelent ways to organize the data .. both unique for a certain scope ... so what did I do ... I put them all in TWO lists ... each organized by the appropriate criteria .. and I just wrote the class in such a way that the function to add/remove always added and removed for both lists ... no problems at all - and amazingly fast searches for both types of use ...

of course now I use databases, and understand that was just similar to a database creating "indexes" by certain criteria to allow fast lookup .. but still, it was pretty important when I learned that I could model the system as it was, instead of arbitrarily picking one list to use, and writing an extra find() function for getting the data by the secondary method ...

Something else that helped when I was learning to program C++ .. was when I decided nearly all GAME object relationships should work in BOTH directions ... sure when writing a list, you can have a tree of object ownership ... einstein and relativity say, one point of view is as valid as another ... so why should you be able find out what Weapon a Person is using, but not be able to ask what Person is using a Given Weapon? This makes much more sense when you think of Teams (A Team has a list of its Players, and a Player knows its Teeam) .. this dual direction relationship makes life so simple for writing code that actually DOES SOMETHING in the game.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!