12

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.

Pablo Claus
  • 5,886
  • 3
  • 29
  • 38
Russel
  • 3,609
  • 8
  • 33
  • 32
  • 1
    If you're a beginner at C++, I [highly recommend that you pick of a good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), and learn at least proper basic C++ before diving headfirst into game programming. In any case, start out small - game programming especially in C++ is not trivial. – In silico Nov 09 '10 at 06:33
  • Hi In silico, thank you for the comment. I do understand this and I've been working though C++ books like crazy. I've been programming awhile, but am slowly progressing to the level of semi-competency. The questions I asked above are really things I was super-curious about and wanted to get a good idea about for future reference. Thanks! – Russel Nov 09 '10 at 06:42

5 Answers5

8

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.

James
  • 1,651
  • 2
  • 18
  • 24
  • PS: It was playing the original final fantasy on the NES that got me into game programming... until the game industry imploded last year... – James Nov 09 '10 at 06:44
  • James, can you elaborate on the game industry imploding? I don't keep up with game industry news, but am interested in the game industry and would like to know what you mean. Sounds scary for future job prospects, considering the market is already over-saturated with applicants. – ShrimpCrackers Nov 09 '10 at 07:02
  • The economic down turn that came about lagged behind with the gaming industry about two years. Around this time last year is when around 600 game developers in the LA area alone were let go and I think the numbers were closer to 800ish within the country... Such is life, but I am expecting a lull in the game releases starting in about 6 months or so given the average time to develop video games :) – James Nov 09 '10 at 16:45
3

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

sum1stolemyname
  • 4,506
  • 3
  • 26
  • 44
3

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.

JimR
  • 15,513
  • 2
  • 20
  • 26
  • Careful with the STL, it is often more than is needed as it is meant to handle Any situation, and the overhead that comes with that ability can be detrimental to video games where memory and speed are or can be critical. They are great for learning, but you will almost always see benefits when you tailor your container objects to your needs. – James Nov 09 '10 at 07:03
  • 1
    @James I do not disagree, however... Beginner... :) – JimR Nov 09 '10 at 07:06
2

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/

Community
  • 1
  • 1
Doc Brown
  • 19,739
  • 7
  • 52
  • 88
  • Hehe, do not spread this around but most games these days are just very very pretty and fast 30+fps graphical interfaces to databases :) And some times this is literally (World of Warcraft anyone? :)) – James Nov 09 '10 at 07:13
  • @James: yes, to use a database seems to be the obvious approach when your data model reaches a certain complexity. It gets even more obvious for multiplayer games, where lots of game data has to be stored on a central server. – Doc Brown Nov 09 '10 at 07:41
1

Regarding Question #1, I concur with James and others on using a database that stores the persistent state of your game objects.

Regarding questions #2 and #3, about variable numbers of objects and objects that need frequent updating: I'd suggest maintaining a registry of objects that need updating for each game "cycle" (most games operate on cycles -- loops, if you like, though a modern game uses many loops spawned as separate threads).

For instance, every time you introduce a new enemy or other object that needs to be updated to respond to the current situation or behave in a certain way, you register that object in a list. Each cycle, you iterate through your current list of updateables (probably based on some priority scheduling mechanism), and update them accordingly.

But the particular data structure you use will depend on your program. Linked lists are a valid foundation structure, but in all likelihood you'll want to use a custom compound structure that meets your particular needs. Your approach may combine any number of classic data structures to achieve the best result in performance and effect.

Considering this, I can't emphasize enough the importance of studying advanced data structures before you tackle any sort of serious programming project. There are scores of great books on the topic and you'd do well to study them. Here's a link to a tolerable overview of the classic data structures: http://randu.org/tutorials/c/ads.php

Brian Lacy
  • 18,785
  • 10
  • 55
  • 73