# setting up an array in C++

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

## Recommended Posts

Well I'm wondering how I would setup some global arrays to store character data for my game. When I did this in game maker it was easy because I could just put; global.character[0,0]= (name) global.character[0,1]= (health) etc. They way it was set out was the first number was the character id and the second number defined the stat. What I'm wondering is how would I go about setting up something similiar in C++ for my rpg game?

##### Share on other sites
Heres an example,

type name[number of items]

heres an acual example:
int weapons[5];weapons[0] = "USP";weapons[1] = "M4";weapons[2] = "AK47";weapons[3] = "AWP";weapons[4] = "GLOCK";

Hope that helped [smile]

##### Share on other sites
I know hwo to do that but is what I could do in game maker possible?

##### Share on other sites
Rabbit? that should probably read "string weapon_name[5]" considering how you used it.

OP,
you can do something more like this
struct player_stats{  int health;  int magic;};player_stats me;...me.health = 5;//or as an arrayplayer_stats creatures[5];creatures[0].heath = 5;//or as a vectorvector<player_stats> creatures;creatures.resize(1);creatures[0].health = 5;

But, since there is no standard variable that takes all types, you can't make a homogenious array charactor[number][stat].
But, structures should make your life easier too, since you now have a named stat ( .health ) instead of a number ( 1 )

##### Share on other sites
Quote:
 Original post by KulSeranBut, structures should make your life easier too, since you now have a named stat ( .health ) instead of a number ( 1 )

Or a class, even! Either of these are most definately the way to go if you're making something bigger than Pong, or even then.

##### Share on other sites
There are several issues wrong with the above code sets.

* Indecent Exposure
* Magic Numbers
* Fixed-length buffers
* Unneccesary use of globals

First, there is the Indecent Exposure problem. The internals of the class are exposed. You should use accessors and modifiers for your values, rather than direct access to the data.

Example:
// Accessor
int myHealth = character.Health();
// Mutator
character.Health( 14 );

Next, magic numbers. Those are always a bad thing. As others expressed, you should use some other way. If the elements are of differing types, you may want to use a structure. Otherwise you might want to change to a std::vector and use an enumerated value (enum) for the index, or you might want to use a std::set with some other key value.

Fixed length buffers. Those are one of the biggest sources of bugs in computer prgramming. Don't use them! C++ has container classes that can manage the work for you.

Unneccesary global. You don't need to store all your world and characters inside a single global object (or a singleton object which is just a fancy global). You can use a regular object and pass around the pointer as needed. Or you can use the global/singleton if you really want, but it's really not necessary.

So a simple example:

class Character {  /* Your innards go here */  public:  // Default constructor.  You might omit it if you always need to have  // specified values.  Character( );  // Construct a character with all the values you REQUIRE for it  Character( string name, int maxHealth, int health, etc. );  // Make it virtual because you will probably be deriving from this class  virtual ~Character()    // Accessors and mutators  string Name();  string Name( string newName );  int Health();  int Health( int newHealth );   ...};class Monster : public Character {  /* Monster specific stuff */}...

##### Share on other sites
Quote:
 Original post by KulSeranRabbit? that should probably read "string weapon_name[5]" considering how you used it.OP,you can do something more like this*** Source Snippet Removed ***But, since there is no standard variable that takes all types, you can't make a homogenious array charactor[number][stat].But, structures should make your life easier too, since you now have a named stat ( .health ) instead of a number ( 1 )

Oh yeah [lol]

I wasnt thinking, i was in a hurry. [smile]

##### Share on other sites
Well friend, there's no easy way to go about this. I think you're trying to do the automagically changing variable system, like that in PHP. In C++, it's strong on type safety, so this isn't going to be possible without a whole lot of casting. Could you be anymore specific on what you need changed, what's the range of change. This will determine how hard it will be to make this work. First off, use std::string for strings, don't use char*!!!

##### Share on other sites
#include <iostream>using namespace std;#define MAX_CHARACTERS      32#define MAX_STATS           8class global_type{    static int characters[MAX_CHARACTERS][MAX_STATS];public:    class character_type    {    public:        int& operator () (int id, int stat) { return global_type::characters[id][stat]; }    };    character_type character;};int global_type::characters[MAX_CHARACTERS][MAX_STATS];global_type global;int main(){    global.character(0, 0) = 100;    cout << global.character(0, 0) << endl;    return 0;}

This was the closest I could get without setting my PC on fire.

If you want to re-create the game maker syntax, you probably have to write your own language interpreter. My guess is that is what they did.