• Advertisement
Sign in to follow this  

Hypothetical Question

This topic is 2995 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
Quote:
Original post by phear-
*** Source Snippet Removed ***

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

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


Actually, not in that example. While the array allocation returns a pointer on the first element, the C++ default [] operator performs a dereferenciation. Its an array of object, not an array of pointers. If it was an array of pointers, you would need to dynamically allocate each element of the array.

Share this post


Link to post
Share on other sites
Hm, tried the suggestions


int GraphicElements = 2
class ImageClass
{
public:

sf::Image image;
sf::Sprite sprite;

int x;
int y;
};

ImageClass* MenuPicEle = new ImageClass [GraphicElements];

MenuPicEle[1]->x = 1;

delete[] MenuPicEle;




and it's giving me 4 errors:

1.error C2143: syntax error : missing ';' before '->'
//referring to MenuPicEle[1]->x = 1;
//It's not recognizing MenuPicEle as a class?

2.error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
//referring to MenuPicEle[1]->x = 1;
//what? why? I specified it to me a pointer to ImageClass

3.error C2372: 'MenuPicEle' : redefinition; different types of indirection
//referring to MenuPicEle[1]->x = 1;
//Eh? how is that a redefinition?

4.error C2059: syntax error : 'delete'
//referring to delete[] MenuPicEle;
//that's a syntax error? then how am I suppose to de-allocate it?

Share this post


Link to post
Share on other sites
Steadtler is right my apologies I wasn't thinking in my post.


Imageclass *newimage = new Imageclass();

Imageclass *images = new Imageclass[20];

images[1] = newimage;



Look into standard library containers like vectors, lists, etc instead of using raw arrays.

Share this post


Link to post
Share on other sites
sry for being a noob but I have no idea what you just did there. Can you explain the code? also why are vectors and lists better than arrays in this case? lastly would I have to de-allocate both newimage and image?

Share this post


Link to post
Share on other sites
learn arrays first then worry about vectors.

it's hard enough gettin the basics down without the added complexity of STL :P

keep on goin with arrays (:

Share this post


Link to post
Share on other sites
<! EDITED !> See my other post below

[Edited by - phear- on December 6, 2009 10:47:06 PM]

Share this post


Link to post
Share on other sites
The code given by phear- in his previous post does not compile:
Imageclass *newimage = new Imageclass();

Imageclass *images = new Imageclass[20];

images[1] = newimage;

because of the last line. The [] operator already gives you access to the object itself, there's no dereferencing needed (you're working with a pointer that points to a number of contiguous objects). So, you should use the . operator rather than the -> operator. This also means that you're trying to assign a pointer-to-Imageclass to an Imageclass instance. Ain't gonna work. What you can do, however, is this:
images[1] = *newimage;
That dereferences newimage, so it calls the = operator. Which copies the content of one object into another.

However, phear-'s latest post uses an array of pointers, and there you will need to use the -> operator.

Quote:
Original post by Atrix256
learn arrays first then worry about vectors.

it's hard enough gettin the basics down without the added complexity of STL :P

keep on goin with arrays (:

True, learn about arrays first. But after that, learn about std::vector, std::string, other container classes and algorithms, and use them. If anything, the standard library makes doing actual work easier, although I do agree it looks somewhat intimidating at first. C++ just isn't the easiest or nicest language to start with...

Share this post


Link to post
Share on other sites
For some reason I cannot post the source code that I had above within source tags on the forum without it crashing, so I posted the source on my site:.
I cant stand looking at source code without source tags.
@Captain P: as I said, my post was incorrect, my apologies. This does compile now =D

[Edited by - phear- on December 6, 2009 11:59:41 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by phear-
For some reason I cannot post the source code

If that should happen again, I recommend pastebin.

Share this post


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

  • Advertisement