Game programming: duplicating enemy objetcs

Hello everyone,

I wanted to know if anyone could help me out understanding how to do a certain thing for y C++ SDL2 game.

It's a 2D Tile Map Final Fantasy / Zelda style game.

I wanted to know:

If I have an object for a monster like Skeleton.

When I spawn many "Skeleton" objects on the screen at the same time around a spawn point, how do I make it that all those Skeleton objects are actual different instances since they come from the same Object?

My problem is that the Skeleton Object has a Health Point member variable and so
if (healthpoint > 0) then skeleton will remain spawned and else he will die/disapear... My problem is that as soon as one of he skeletons reaches 0 health, they all disapear since they all come from the same object and are actually all related to the same object and health member variable...

So how can I do this? I know I could simply create X number of Skeleton00, Skeleton01 ... to SkeletonX objects, but I'd like to find the right way to do it... Thanks guyz really appreciate it!
when you set the life health point member, did you tell it to decrease all of the objects or, did you tell it to just take a health point off that certain object? like when you have your collision, did you have a function to find out which skeleton it was that was hit and then only take a health point from that one object? hope this helps
Well the collision hits the actual skeleton you are hitting on the screen at those exact coordinates, but as you hit when you retrieve a health point it retrieves a hitpoint from the:

helmhorr = new CSprite(csdl_setup->GetRenderer(), "Enemy00.tga", 550, 250, 100, 100, 50000, 0);

object which is the basic object for all the spawned skeletons...

I can mke different skeletons appear at different locations on the screen with different collisions, but as one gets hit, this objects 50000 heathpoint variable drops and then when it reaches 0 or below all the skeletons disapear...

I know I can make different control structures with variables to have specific skeletons come and go, but it's not what im looking for.

I would like to use this called object and duplicate or copy or clone into different instances that stand on their own but i don't know how... thanks for the help though its really appreciated.
maybe you could create an array out of the object that makes the skeletons and each one will have a different identifier, then I think that would help separating the health points of each.
Yes I think that just might be the way to do it...

You make an array out of the object? Do you mean you create an array and in each position you store a skeleton and you access it through each array position?
yes that is exactly what I was talking about
Great Thanks.

How would you go about doing this...
Are Member variables unique under each array position or do I need to code it up and create a different new external variable per position that would represent skeleton health?
Well, is the function that takes away health from the skeleton called using the this pointer? That might help you out in this case (assuming that the function that calls the function to reduce health is also in the skeleton object).
Yes it's function defined in the class that creates this object...
But I don't See how it could this help though...
Last edited on
I do believe that the member variables are unique, and you can call each skeleton using their number in the array, and you can make a function like getSeletonNo() {return i}; // i being the variable you used in your array
this should work
Wow that would be great...

Excellent, well thanks a lot, i'll work on that and keep you guyz posted!


Thanks!
Wow, you put this on Beginners?
closed account (Dy7SLyTq)
yeah man thats easy
Ok guyz so I'm almost there, it compiles fine but screens stays white, theremust be a problem somewhere here is my code...

If my object is:

helmhorr = new CSprite(csdl_setup->GetRenderer(), "Enemy00.tga", 550, 250, 100, 100, 50000, 0);

and I'm trying to initialize an array filed with that object is it like this?

CSprite* helmhorr00[6] = { helmhorr };

then in my code :

helmhorr00[1]->SetPosition2(i*100+a+ma,j*100+b+mb);
helmhorr00[1]->Draw();

it doesn't work...

I tried also making a function that returns array value, but it won't work:

CSprite* CMain::GetEnemy(CSprite* helmhorr00[6], int u)
{
return helmhorr00[u];
}

can't seem to make my array spit out the object to pass in function...

Did I initialize my array with the right type or what could it be?


Thanks guyz

Last edited on
Ok well I got it to work... Meaning the Array has been initialized and filled with the CSprite Objects...

The only problem I have now is still the same basic one, which is.. my different Skeleton objects are not unique...

I thought that storing an Array with the Same Object made all those objects unique within each of those positions... They don't seem to be as STILL if I kill in-game one of the skeletons they still all disapear at once...


I'll keep working on it, if anyone has an idea or If i'm not getting something, please do tell me.

Thanks again for all your help!
Aight guyz, got it to work all I needed to do was the following:


CSprite* helmhorArray[6];
for(int i = 0; i < 6; ++i)
{
helmhorArray[i] = new CSprite(csdl_setup->GetRenderer(), "Enemy00.tga", 550, 250, 100, 100, 50000, 0);
}

enables you to get a unique instance of the object by calling object thorught array position... Simply Awesome.

I would like to thank everyone who participated in the thread. Thanks!
Topic archived. No new replies allowed.