Archived

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

Linked list

This topic is 5654 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

I am writing a simple tile based game. My "hero" is encapsulated in class CCharacter while baddies will be encapsulated in class CVillan. From my reading around this topic I see that storing in game characters in a linked list is a good idea. Would it be good practice to create a class, CUnitBase, that CVillan and CCharater can inherit from? This base class could then be used as the data type for the linked list pointer as well as storing other data that is generic to the two classes (position, for instance). Your thoughts please comrades. Garvin.

Share this post


Link to post
Share on other sites
Yes, it is a valid solution for storage.

Consider the use of std::list<UnitBase*>.

See also that discussion of Generic Programming before you get too much carried away with your class hierarchy.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites
when designing class hierarchies(or solving any sort of problem), it is often useful to sit down with a piece of paper (a nice big one), some sharp pencils, and oh yeah... dont go anywhere near your computer. the design of a solution has nothing to do with a computer, so don''t use one. it is the implementation of that design that requires a computer.

another useful tool is a nice big dry erase board or a chalk board, and get plenty of colors of colored marker or chalk to work with. you can get an 8''x4'' chunk of the same stuff used in dry erase boards for pretty cheap.

on this, you write down every single class that you think you will need for your entire game, not just your hero and villan, but also the map class, and whatever else. i mean EVERYTHING. while you are doing this, draw relationships between the classes, use a different colors for "is-a" and "has-a" relationship, and if there are other relationship types, like "contains a collection of" or something, use a different color for that, too.

and don''t forget to put down the ultra-trivial stuff, either. no, you won''t "remember" it later. write that shit down, now.

after you have done this (and it *WILL* be messy), consolidate it into a more understandable and organized form on paper. again, stay away from your computer. during this translation from chalk/dry-erase board to paper, you will likely wind up revising things a bit as you get more organized. you can start adding members to your classes during the paper phase and otherwise flesh out the ideas for the classes a bit. the more you do BEFORE you sit down at your computer, the less time you will spend at your computer.

now that i''ve done my public service announcement about the value of designing things beforehand, i''ll get to your specific problem. the value of the methods outlined above is that it will typically answer just these sorts of questions for you.

unless i miss my guess, CVillain and CCharacter are almost exactly identical, with perhaps a few capabilities on the part of CCharacter than your typical CVillain won''t have. If this is so, then CCharacter and CVillain may not really need to have a common base class. CVillain may be the base class you are looking for to make CCharacter (naturally, you may wish to change the name of the classes then).

Share this post


Link to post
Share on other sites