Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Beginner game programming: Keeping track of objects, inventory lists, arrays of different amnts of object/characters, etc

Tags:

c++

I was just playing an old SNES RPG (Secret of Mana, if anyone cares) and was wondering a few general things about game programming.

Sorry for some of the brain-dead questions, I'm really a beginner. :)

These questions are quite general, but use SNES-style RPGs as a "template" to get an idea of what I mean:

  1. How do games keep track of all the objects, triggered events, etc in its "world"? For example, how does it keep track of which treasure chests have already been opened, which doors are locked, which story events have already triggered?

    Does it basically create an array of elements each corresponding to a chest/door/event/etc and "mark" each (change its value from 0 to 1) when it has been opened/triggered? If there are multiple approaches, what are they?

  2. How are "variable lists" handled? Ie, if you have a game when you can have a huge inventory of objects (ie: armor, swords) and have X of each object, how is this done?

    My guess: have a struct that has a big array with a spot for every possible object (an array of X ints, where X is number of possible objects to own) where each element's value represent how many of that object you have, and then have a giant enum of every object so that an object is matched to a corresponding index, and access it, like: numberOfSwords = inventory[SWORDS] where SWORDS is part of an enum and has some integer number associated with it. How close am I?

  3. How about the case where the number of objects can vary? Ie, if I have a game where I have some amount of enemies on the screen and they can get killed / give birth to new enemies at any point, it would seem to me like I would need an array of "enemy" objects to loop through and process, but that the number of elements would vary at any one time. What is the usual approach to this? Linked lists?

Any help / hint / pointers are really appreciated.

like image 285
Russel Avatar asked Nov 09 '10 06:11

Russel


4 Answers

In a very basic manner your answers are not too far off, things could be done the way that you mention them. However space and processing power can come into play so instead of an array of bools to track which treasure chests or how far along the chain of events you are you may want to slim it down to bits being on and off and use the bitwise operators for masking to see where you are in a storyline or whether or not to show the treasure chest you are about to display as opened or close.

For inventories, instead of tracking how many of each item a player has it may be better to have a base item for everything a player can pick up; weapons, armor and even money. Then you could make a linked list of just the items the player has. Use the Enum for the item as you mentioned and then the quantity of that item. This would allow for sorting of things and would also only keep in memory the items the player's character(s) actually have/has. You could extend this data structure to also track if the item is equipped. You could likely keep more generic what the item does sort of information in an items table.

The enemies would likely be a bit more complicated as you need to do a few more things with them. A linked list here though is still likely your best bet. That way removal of an object form the list would be a mite bit easier (can simply remove the link and the like when a player kills them or add in a new enemy wherever needed in the list.)

Honestly there is no one answer and it can depend on quite a few things. The best way is really to simply try it out.. For a simple 'what if I do this for this' it really does not end up taking all that long to give it a whirl and see how far you get. If you start running into issues you can start to consider other options :)

Hope this helps.

Edit: Just wanted to add in a link to www.codesampler.com. Generally more DirectX oriented tutorial sites but as a beginner it can start you thinking or give you a set of places to start. As an added bonus alot of the DirectX SDK examples/samples started to be formatted very much like how this site's tutorials are done. Can help ease you into the whole thing.

like image 63
James Avatar answered Oct 31 '22 10:10

James


This is a pretty advanced question for a beginner.

I'd like to echo In Silico's response that you should learn C++ language basics before you tackle this subject.

To give you a place to start, you should know about container classes (Linked Lists, Vectors, HashTables/Dictionaries, Queues etc) and how they work. Since the Standard Template Library (STL) is pretty standardized, it would be a good place for a beginner to start.

You should also know about inheritance and how to build a hierarchy of classes.

For example, you asked about inventory in a role playing game: I'd start by defining an InventoryItem class that defines or sets up an interface for all of the code necessary for an item to participate in your inventory system. Something like:

class InventoryItem
{
private:
    std::string description; // A description of the item
    bool inInventory;  // True if in the players inventory, false if on the ground etc...
    int weight;        // How much the item weighs
    int size;          // How much space the item takes in inventory
    // etc...
};

In the InventoryItem class you'd also define the member functions and data needed for InventoryItem to be placed in your container class of choice.

The same sort of thing holds true for triggered items, things on the ground etc. They're typically kept in a container class of some sort.

The STL containers will take care of the variable sizes of the containers mentioned in the last part of your question(s).

vector is a good place to start for a general list of items. HashTables/Dictionaries are good for looking things up with a key.

I hope this is enough to get you started. Good luck.

like image 23
JimR Avatar answered Oct 31 '22 11:10

JimR


In addition to James' excellent post, some keywords for you to google for

  • Data structures
    • linked list
    • doubly linked list
    • queue

for the theory of dynamic memory management.

Also, let me share my standard recommended links for people asking for aid on basic c++:

Full scale tutorial on c++

C++ Language Reference (including STL)

ANSI C Language reference for all those pesky C stuff that C++ keeps using

like image 3
sum1stolemyname Avatar answered Oct 31 '22 09:10

sum1stolemyname


Your question is not specific for "games programming". You are asking how arbitrary data is organized and stored in bigger programs. There is no definite answer to this, there are lots of different approaches. A common general approach is to make a data model of all the things you want to store. In C++ (or any other languages with object oriented capabilities), one can create an object oriented class model for this purpose. This introduction to C++ contains a complete tutorial on object oriented modeling in C++.

Lots of applications in general use a layered approach - the data model is one layer in your application, separated from other layers like a presentation layer or application ("game") logic.

Your data modeling approach will have to deal with persistency (that means, you want to store all your data on disk and reload it later). This question was asked earlier here on SO. This fact will give you some restrictions, for example, on the use of pointers.

EDIT: if your data model reaches a certain complexity, you might consider using a (lightweight) database, like SQLlite, which has a C/C++ api.

Finally, here is a link that might give you a good start, seems to fit exactly on your question:

http://www.dreamincode.net/forums/topic/26590-data-modeling-for-games-in-c-part-i/

like image 2
Doc Brown Avatar answered Oct 31 '22 10:10

Doc Brown