Jump to content
  • Advertisement
Sign in to follow this  
buggy123

Hypothetical Question

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

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


Link to post
Share on other sites
Advertisement
Quote:
Original post by buggy123
don'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 this post


Link to post
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 command
void removeUnit(unsigned int unitIndex)
{
allUnits.erase(allUnits.begin()+unitIndex);
}



Share this post


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

int NumEle = 3;
Button* MenuElement = new Button[NumEle];

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

Share this post


Link to post
Share on other sites
Quote:
Original post by buggy123
so say I have a class and I allocate some dynamic memory for it

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


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


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


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

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!