In the last update on my game JelloStorm! we ran into the issue of defunct jello blobs. I could not figure out why there are some jello blobs that don’t move and can’t be touched or shot with your arrows. The problem for this is that the game can’t continue until you kill all of the alive jello blobs in each round.
I first made a work around, as you can see in the commit on GitLab, where I only counted the non-defunct jellos to see if they were alive, allowing you to continue from level to level, as you only had to fight the non-defunct jellos. This worked, in general, but just isn’t right and caused the game to be fairly random, as one level you only fight 3 non-defunct jellos, and the next level had 17!
Unable to figure out the issue, I turned to Stack Overflow, and asked if my pointer was set up wrong. A user named Jeremy Friesner was kind enough to lend me a hand. More than just answering the problem, he told me what was wrong and why that it is a bad idea to do things that way! Here is a quote from his answer:
Does anything ever set the m_alive field in the Jello class to true? If not, then that field’s value will be indeterminate (i.e. randomly true or false, depending on whatever other data happened to be written to that particular memory location before the Jello object was created). Needless to say, that’s not a good state of affairs. A good practice is to always set all of your classes’ member-variables to a known default state as part of the class’s Constructor-method(s).
Unfortunately, my question was down voted and I can’t select his comment as the answer, but in 15 minutes, this kind user, through the help of Stack Overflow helped me better understand how things work in C++ and fixed my problem! Thanks Jeremy!
So, I edited my code. You can see the full commit on GitLab, but here is the important part:
class Jello {
private:
// How fast is each jello type?
const float RED_SPEED = 40; //Bloater in original game.
const float BLUE_SPEED = 80; //Chaser in original game.
const float GREEN_SPEED = 20; //Crawler in original game.// How tough is each jello type
const float RED_HEALTH = 5;
const float BLUE_HEALTH = 1;
const float GREEN_HEALTH = 3;// Make each jello vary its speed slightly
const int MAX_VARRIANCE = 30;
const int OFFSET = 101 – MAX_VARRIANCE;// Where is this jello?
Vector2f m_Position;// A sprite for the jello
Sprite m_Sprite;// How fast can this one run/crawl?
float m_Speed;// How much health has it got?
float m_Health;// Is it still alive?
bool m_Alive = true;
Sweet! By declaring the variable specifically at creation, the jello blobs don’t end up defunct. I guess, from Jeremy’s answer, that if I don’t declare it, it will randomly become something, which could be false. And if it is false, then it will not be harmful to the player, not be kill-able, and can’t move, because all of those functions check if(_is_Alive)!
Be sure to check out the game, now that it works properly!
Linux – keep it simple.