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, 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
    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;
    /* Constructor and main loop */
int Punittypes;
    int OnExecute();

    /* 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();


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

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, learning, and sharing knowledge.