Vector of enemies, problems with a surface?

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
SamCam
Posts: 20
Joined: January 8th, 2020, 3:24 pm

Vector of enemies, problems with a surface?

Post by SamCam » May 28th, 2020, 6:53 pm

Hey guys,

I was bimbling along quite happily trying to create this Space shooter type game, lots of inspiration taken from Project Twin.

I have managed to make a vector of animated 'laser' sprites but despite the code being pretty darn similar for the actual enemies, I keep crashing hard. I think this is something to do with the surface, being reset or copied incorrectly but can't quite put my finger on it.

The idea (for now) is to spawn 10 random enemies which bounce around the screen or other rect area (poo style) and then you can shoot them, I plan later to try and add their own attacks and tidy up their flight paths and whatnot.

https://github.com/SamuelLouisCampbell/ ... T14_samDev

Any ideas welcome, any suggestions welcome!

SamCam
Posts: 20
Joined: January 8th, 2020, 3:24 pm

Re: Vector of enemies, problems with a surface?

Post by SamCam » May 28th, 2020, 7:13 pm

Also, I thought I might try posting a question on Stack Overflow about this, better described for all the peoples who aren't working with the Chili Framework. But people there seem sooo harsh and a little esoteric if not harsh, any thoughts?

For example people will say "DUPLICATE QUESTION" and then just link to a question that does fulfil the answer if you aren't a beginner maybe. But also is maybe a touch hard to follow, if you're not quite grounded in the language yet.

Anyway, that's defo why I like coming here first, happy friendly people. Thanks all.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Vector of enemies, problems with a surface?

Post by albinopapa » May 29th, 2020, 8:00 am

This particular problem gets me every once in a while.

When you place objects in a vector it's address may change and probably will when the vector grows. What this means to you is you are assigning the address to a sprite to an animation object, then when you add another enemy or laser or whatever uses animations to the vector it has to grow which creates a new vector, copies over the old data and destroys the old vector thus freeing up the memory and the address of the Surface object stored in Animation.

This is one of the issues with sharing raw pointers, especially when it's a pointer to a unstable source such as a vector or address of an object allocated on the stack.

My advice is this, instantiate each Surface object in Game. Pass a pointer to each Surface to each entity that needs that surface. For instance, Enemy( pos, reboundRect, &enemy_surface ); This way, the surface object is stable and should never move. The pointer will be safe even when the enemies vector is resized. This will help with resources as well, since you won't be loading the sprite for each enemy you save on time and memory.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Vector of enemies, problems with a surface?

Post by albinopapa » May 29th, 2020, 8:32 am

Image
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

SamCam
Posts: 20
Joined: January 8th, 2020, 3:24 pm

Re: Vector of enemies, problems with a surface?

Post by SamCam » May 30th, 2020, 11:26 am

Many thanks, just getting an understanding of the issue is really helpful. I'll be trying to take this little project forward using some of the techniques described in the later intermediate series videos. For example, I will want some different enemies to inherit and extend the enemies functionality.

Maybe I'll make a 'SpriteDump' class that's stable and just pass out all the sprites from there. Thanks again!

Post Reply