Jump to content
  • Advertisement
Sign in to follow this  
namingway

setting up an array in C++

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

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 array
player_stats creatures[5];
creatures[0].heath = 5;
//or as a vector
vector<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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by KulSeran
Rabbit? 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 this post


Link to post
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 this post


Link to post
Share on other sites

#include <iostream>
using namespace std;

#define MAX_CHARACTERS 32
#define MAX_STATS 8

class 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!