Public Group

# Hypothetical Question

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

## Recommended Posts

Say in a rts game and you make a unit in the game. don't you have to create instances of that unit's class? how is that possible during run-time? what exactly happens what you create a soldier? how is it's information (hp, location, attack) stored? what happen's when it "dies"

##### Share on other sites
Quote:
 Original post by buggy123don't you have to create instances of that unit's class?

Yes.

Quote:
 how is that possible during run-time?

Dynamic memory allocation. (Assuming C++)

Quote:
 what exactly happens what you create a soldier?

You dynamically allocate an instance of the unit and put it with others in an array or container.

Quote:
 how is it's information (hp, location, attack) stored?

In a structure:
struct Unit {	Vector2	position;	float	health;	float	power;	float	speed;};

Quote:
 what happen's when it "dies"

It is removed from the array, and the memory used for its allocation is freed.

##### Share on other sites
_fastcall already explained it but here is some code to show how it might work.

struct Unit {        Unit(const Vector2 position,float health,float power,float speed):        position(position),health(health),power(power),speed(speed)        {}	Vector2	position;	float	health;	float	power;	float	speed;};std::vector<Unit> allUnits;void addNewUnitToGame(const Vector2 position,float health,float power,float speed){    Unit newUnit(position,health,power,speed);    _allUnits.push_back(newUnit);}//other functions can iterate over allUnits and draw update etc.void drawUnits(const Vector2 position,float health,float power,float speed){    for(unsigned int i=0; i < allUnits.size(); ++i)    {        drawUnit(allUnits);    }    }//you can remove a unit from the vector by the erase commandvoid removeUnit(unsigned int unitIndex){    allUnits.erase(allUnits.begin()+unitIndex);}

##### Share on other sites
so say I have a class and I allocate some dynamic memory for it

int NumEle = 3;

how can access the members of the class? I don't think MenuElement[NumEle].member; works....

##### Share on other sites
Quote:
 Original post by buggy123so say I have a class and I allocate some dynamic memory for itint NumEle = 3;Button* MenuElement = new Button[NumEle];how can access the members of the class? I don't think MenuElement[NumEle].member; works....

C++ uses 0-based indexing, dude. Indices 0-2 will work in your example.

##### Share on other sites
hmm really? I tried but the intellisense isn't picking up anything

##### Share on other sites
int lolNumber = 10;Unit* lolUnits = new Unit[lolNumber];lolUnits[0]->member;lolUnits[1]->member;lolUnits[2]->member;lolUnits[3]->member;lolUnits[4]->member;lolUnits[5]->member;lolUnits[6]->member;lolUnits[7]->member;lolUnits[8]->member;lolUnits[9]->member;

There is no lolUnits[10]... only 0 - 9

Also remember its a pointer to Unit... you need to use -> not .

##### Share on other sites
just because intellisense doesn't get it doesn't mean it doesn't work.

##### Share on other sites
Yeah sorry forgot to add that Intellisense is extremely slow sometimes. I can write a hundred lines of code and IS wont pick up on all the changes for a good 5 to 10 minutes, sometimes more.

##### Share on other sites
hmm, thanks for the replies. I didn't know before the difference between -> and .
learned something new :D

I find it kind of annoying that intellisense isn't instant but oh well, nothing is perfect.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 31
• 16
• 11
• 10
• ### Forum Statistics

• Total Topics
634118
• Total Posts
3015609
×