Saving and Loading Scores for each level.

Discussion and help for Easy Save 3
Post Reply
GumballFight
Posts: 5
Joined: Tue Jul 14, 2020 7:13 pm

Saving and Loading Scores for each level.

Post by GumballFight »

Hello,

I've got a simple set up here. Each level is represented by a GameObject that hold information about the level. It also holds the Score that the player has achieved if they've played the level before but it doesn't seem to be working. How is this script?

Code: Select all

    public GameObject[] levels; //An Array of all the levels.
    private List<int> scores = new List<int>(); // A list of the scores from each level.
    private List<int> loadedScores = new List<int>(); // A list of the scores once they're loaded. Shouldn't really be needed.

    public void Start()
    {
        LoadGame(); // Loads the game when the game starts.
        
        //This is here if the player has just finished a level and has come back to the level select scene. This is working.
        
        if (scoreKeeper.levelFinished == true) 
        {
            GameObject.Find(scoreKeeper.currentLevel.ToString()).GetComponent<levelManager>().currentScore = scoreKeeper.lastScore;
            scoreKeeper.currentLevel = 0;
            scoreKeeper.lastScore = 0;
            scoreKeeper.levelFinished = false;
        }
        
        //This is then saved.
        
        SaveGame();
    }

    public void SaveGame() 
    { 
    
    	//Gets each level object from all the levels and adds the score to the list.
        foreach(GameObject level in levels) 
        {
            scores.Add(level.GetComponent<levelManager>().currentScore);
        }
	//Saves the new list.
        ES3.Save("scoreSave", scores);
    }

    public void LoadGame() 
    {
    	
    	//This loads the scores into a list
        loadedScores = ES3.Load("scoreSave", loadedScores);
	
	//This gets each level object from the list
	
        foreach (GameObject level in levels)
        {
        
        //This gets each score from the loaded score list
        
            foreach(int scoreNum in loadedScores) 
            {
            
            //Adds the score to each level. I think this is where theres an issue.
            
                level.GetComponent<levelManager>().currentScore = scoreNum;
            }
        }
    }

    private void OnApplicationQuit()
    {
        SaveGame();
    }
Thank you for any help. I think theres an issue when the game loads the data.

Thanks again!
User avatar
Joel
Moodkie Staff
Posts: 4848
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving and Loading Scores for each level.

Post by Joel »

Hi there, and thanks for getting in contact.

It's not possible for me to replicate this from what you've sent. Please could you create a new, basic project which replicates the issue and private message it to me with instructions?

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
GumballFight
Posts: 5
Joined: Tue Jul 14, 2020 7:13 pm

Re: Saving and Loading Scores for each level.

Post by GumballFight »

Thank you for replying. Yes I can do that.
So the script is working for you then?

I should also note that the loaded score isn't been given to any of the levels.
User avatar
Joel
Moodkie Staff
Posts: 4848
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving and Loading Scores for each level.

Post by Joel »

Hi there,

just to clarify, it's not possible for me to replicate it because your script relies on their being specific objects in your scene, including a GameObject containing a levelManager class.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
GumballFight
Posts: 5
Joined: Tue Jul 14, 2020 7:13 pm

Re: Saving and Loading Scores for each level.

Post by GumballFight »

Yes of course, sorry. I should of thought about it a bit more.
Post Reply