Saving collectibles that were marked as "picked up"

Discussion and help for Easy Save 3
Post Reply
chrisv007
Posts: 6
Joined: Mon Apr 15, 2019 12:57 pm

Saving collectibles that were marked as "picked up"

Post by chrisv007 »

Hi guys,

I'm building a 2D platformer with multiple scenes and a lot of collectibles for every scene (coins, health pickups and so forth).
Every time an item is picked up it is deactivated and a bool is set to "pickedup". Here is my coin script. This script is on every coin.

Code: Select all

[SerializeField] private int coinsToGive;
    private ScoreManager scoreManager;
    private AudioManager audioManager;
    public bool isPickedUp = false;
    

    // Use this for initialization
    void Start ()
    {
        scoreManager = FindObjectOfType<ScoreManager>();
        audioManager = FindObjectOfType<AudioManager>();
	}
    void OnTriggerEnter2D(Collider2D other)
    {

        if (other.tag == "Player")
        {
            scoreManager.AddCoin(coinsToGive);
            audioManager.PlayCoin();
            isPickedUp = true;
            gameObject.SetActive(false);
        }
    }
I need to save the coins that were marked as "pickedUp" so they don't load when the player loads the game from save.
Any idea on how to achieve this with Easy Save? I don't know where to start. A code snippet as example would be really nice. The rest of the data saves nicely (high score, score, player position etc). Needless to say this part is crucial to avoid cheating by the player.

Thanks in advance
Chris Verner
Chris Verner Studio
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving collectibles that were marked as "picked up"

Post by Joel »

Hi there,

The following script automatically saves the active states of GameObjects which are added to it's gameObjects array whenever the scene is changed or the application is quit/paused, and loads it when the scene is loaded. So if you add each of your coins to that array, it should do what you describe.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SaveActiveStates : MonoBehaviour 
{
	public GameObject[] gameObjects;
	public string guid = System.Guid.NewGuid().ToString();

	void Awake() 
	{
		if (ES3.KeyExists (guid))
		{
			var activeStates = ES3.Load <Dictionary<long, bool>>(guid);
			foreach (var kvp in activeStates) 
			{
				var go = (GameObject)ES3ReferenceMgr.Current.Get(kvp.Key);
				if (go != null)
					go.SetActive (kvp.Value);
			}
		}
	}

	public void OnDestroy()
	{
		Save();
	}

	public void OnApplicationPause(bool paused)
	{
		if (paused)
			Save();
	}

	private void Save()
	{
		var activeStates = new Dictionary<long, bool>();
		foreach (var go in gameObjects)
			activeStates.Add (ES3ReferenceMgr.Current.Get(go), go.activeSelf);
		ES3.Save<Dictionary<long, bool>>(guid, activeStates);
	}
}
All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
chrisv007
Posts: 6
Joined: Mon Apr 15, 2019 12:57 pm

Re: Saving collectibles that were marked as "picked up"

Post by chrisv007 »

Thanks Joel! I'll give that a shot
Chris Verner
Chris Verner Studio
chrisv007
Posts: 6
Joined: Mon Apr 15, 2019 12:57 pm

Re: Saving collectibles that were marked as "picked up"

Post by chrisv007 »

Hi Joel,

I adapted it to my game and it works perfectly! Thanks a million. I've been banging my head against the wall for days trying to figure this out. Easy Save is truly an awesome product!
Chris Verner
Chris Verner Studio
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving collectibles that were marked as "picked up"

Post by Joel »

Glad that's all working for you Chris :)

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply