Enemies don't look dead after reloading scene

After a bit of debugging, I discovered that all of the Grunts were starting with their health at Zero…
The reason for this is that Progression, there are actually two entries for Health under Grunt…
image

That still leaves your characters getting back up after dying, saving, and restarting…

The cause of that is that RestoreState is called before Start()… so there is a trick for this (in a later lecture, we’ll introduce the LazyValue which will make it so you won’t need this trick…

Set healthPoints to -1 (You’ll actually have to do this both in the code and in the inspector because for most of these the healthPoints is serialized.

Now in start, check to see if the healthPoints are -1… if it is, then get the healthPoints from BaseStats. This is a sort of Lazy Evaluation trap (not quite true Lazy evaluation). It just says “Oh, nothing has initialized Health, so I should do it now”. If RestoreState loads, it will have the correct value and then in start it will say “Oh, I don’t need to go to BaseStats”…

That leaves the final issue, the death…
You’re absolutely right, all of the characters with overrides fail to “die” when they’re clearly dead after a
RestoreState… It looks like the issue is a race condition with Animator…

Ordinarily, the Animator should be all set with it’s Override controller in place by the time RestoreState happens. Even when we’re changing the controller with Fighter, it should still work. This is the first time I’ve worked with 2020.1.8, and I’m questioning if there may have been a change in order of operations…

So here was my solution, though it’s a bandage, not a cure…
In RestoreState(), replace Die() with Invoke(nameof(Die), .01f);

1 Like

For the first part - yes, when I wrote the thread I didn’t even had the progression… didn’t notice this problem when I sent the project… fixed it yesterday (exactly like the way you described :slight_smile: ), sorry about that… haha
also had to change the Start() in health to Awake() so I won’t die when moving a scene (keeping the healthpoints I had in last scene now)

And the Invoked worked! thank you very much!
I hope this won’t be something I’ll have to encounter again…

After finishing 4 Gamedev.TV courses I’ll tell you: you will get constantly back to the courses to understand the code you previously typed, so you can expand the code to your game’s needs. I strongly suggest you to finish your course and then start creating your own game. The courses have refactoring, you learn different ways to do the same thing, so something you make now can break in the future when refactoring something.

From what I’ve seen by now you’re definitely right… haha
I’m really by the end of the course by now btw, and I’m pretty sure I’ll need to go back to some lessons for some stuff
So yes, I’ve decided to wait before I implement some game designs to finish the course first:)

1 Like

Yeah! Even the challenges were complicating things for me. I took different approaches in the first challenges, and a few lessons later my code was breaking the game. So I stopped taking the challenges to avoid losing time. Now I understand why they do that way and why their code is much much better than the code you find on You Tube lessons or other places.

If you wait you’ll have less work later because you’ll have everything implemented. :wink:

haha I’m just the same! sometimes I do the challenges just to check my understanding than deleting it all and following copy paste from the lecture haha
that’s exactly what Im planning (now :wink: ), finishing with the game development -> than putting my game designer hat. with their automatic approach for the code I think it will be very fast to design!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms