Pickup Spawner Saving

Hi!

I’m having trouble understanding the logic behind the Restore State method for the Pickup Spawner.

void ISaveable.RestoreState(object state)
        {
            bool shouldBeCollected = (bool)state;

            if (shouldBeCollected && !isCollected())
            {
                DestroyPickup();
            }

            if (!shouldBeCollected && isCollected())
            {
                SpawnPickup();
            }
        }

My problem specifically is that as far as I can tell, IsCollected is always going to return false, because RestoreState is always called after Awake, where the pickup is spawned regardless. Is there a situation that I’m not getting where this isn’t the case?

Press the “L” key. If the SavingWrapper is unchanged, Load() will RestoreState on the scene without reloading it, in whcih case IsCollected could be true.

Ah ok that solves that problem, then :grinning:

But there never seems to be a situation where the pickup wasn’t collected, and now on RestoreState is null. I’ve put a Debug.Log inside the second if statement, and it never logs in the console when I reload the scene. Tested on a saved file with collected and uncollected pickups.

So what situation does the second if statement cover?

With the item spawned, Save (!shouldBeCollected). Pick the item up and then Load.(isCollected()).

Of course all of these corner cases can be resolved more simply (and in fact all the potential bugs and corner cases we have to solve when L)oading by pressing L in the scene) can be solved by going to SavingWrapper.cs into the Update code and changing the method call in Update from

        if (Input.GetKeyDown(KeyCode.L))
        {
           Load();
        }

to

        if (Input.GetKeyDown(KeyCode.L))
        {
            StartCoroutine(LoadLastScene());
        }

This will perform a complete reload of the scene.

Then RestoreState() in PickupSpawner becomes:

void ISaveable.RestoreState(object state)
{
      if((bool)state)
      {
            DestroyPickup();
      }
}

Ah very cool thank you!

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

Privacy & Terms