Hi,

I'm creating an rts game. I'm looking for a way to store unit data. I thought, I'll make a struct for all the required data, and make a multidemensional array of them (one dimension is the different players, the other is the units). The length in both direction is fixed at the start, but not beforehand. The settings define how many players, and the maximum number of units.
The dimensions are known to every script, since they all are in the same class.

int rts::OnExecute()
{

    OnInit();

    while(device->run())
    {

        OnEvent();
        OnAI();
        OnLoop();
        OnRender();
    }

    OnCleanup();
}

OnInit, OnEvent,OnAI and OnLoop do all need to access this data.
device->run() is an command from the irrlicht library, that's not giving any problems.

De definitions of the functions are in main.h, they all have that one included:

#ifndef _rrts_H_
#define _rrts_H_
#include "../rrts/camera.h"
#include <irrlicht.h>
#define FAIL0 "Loading XML failed"
#define filename "settings/config.cfg"
#define meshfilename "saves/save1/descr.xml"

struct unittype
{
    int meshnr;
    irr::core::stringw name;
    int health;
    int defense;
    int speed;
    int rotation;
    int attacktype;
    int attack;
};

class rts
{
private:
    bool            Running;
    /* global variables here */
    irr::IrrlichtDevice *device;
    irr::video::IVideoDriver* driver;
    irr::scene::ISceneManager* smgr;
    irr::gui::IGUIEnvironment* guienv;
    int meshcount;
    RTSCamera* camera;
    irr::core::dimension2d<irr::u32> res;
    int imeshes;
     irr::core::stringw modelpath;
public:
    /* Constructor and main loop */
    rts();
int Punittypes;
    int OnExecute();

public:
    /* all functions and function dependants */
    bool OnInit();
    void initSettings();
    void initEngine();
    void initGame();
    void initCamera();
    void initMod();
    void initMeshes();
    void initUnits();
    void initBuildings();
    void initPlayers();
    void initAI();
    void initSave();
    void loadMeshes();
    int OnAddUnit(int id,int player, irr::core::vector3df(pos), irr::core::vector3df(rot));
int OnRemUnit(int id, int player);

    void OnAI();

    void OnEvent();

    void OnLoop();

    void OnRender();

    void OnCleanup();
};

#endif

If you need other code to help, please ask. It's going to be open source anyway.
At the moment, only unittype is defined, unittype has the same problem as the unit-array, this is an array containing settings of all the possible types of units. This way, I save on loading files to see those things.

Solved it.
I added
unitz * p; to the main.h, unitz beeing a struct, and

try
{
p= new unitz[pl*nu];
}
catch (bad_alloc& ba)
{
cout <<"bad alloc, aborting. Players: "<<players<<", units: "<<numunits<<endl; abort();
}

to rts::OnInit(int pl, int nu);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.