Page 1 of 1

Creating c# script for persistence data and the feature demo.

Posted: Thu Feb 23, 2017 2:20 pm
by HaiiroMukuro
Good day! :)
How to create a persistence data script for variables?
The variables are:
Money as int
Mathematics as bool
Achievement as bool.
Also for the feature demoscript.
I'm using the feature demo script because I'm trying to create my own prefab for the main menu. Now, it's working, but there's i wanted to try. Making the menu open by the button. My question is how to make the feature demo script to be open using the button?
Thanks for the help in advance. :D

Re: Creating c# script for persistence data and the feature demo.

Posted: Thu Feb 23, 2017 3:33 pm
by Tony Li
HaiiroMukuro wrote:Good day! :)
How to create a persistence data script for variables?
The variables are:
Money as int
Mathematics as bool
Achievement as bool.
You can either start with a copy of PersistentDataTemplate.cs or just add OnRecordPersistentData() and OnApplyPersistentData() methods to your existing script. For example:

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class MyScript : MonoBehaviour {
    public int Money;
    public bool Mathematics;
    public bool Achievement;
    
    void OnRecordPersistentData() {
        // Before saving game, record variables into Dialogue System's Lua environment:
        DialogueLua.SetVariable("Money", Money);
        DialogueLua.SetVariable("Mathematics", Mathematics);
        DialogueLua.SetVariable("Achievement", Achievement);
    }
    
    void OnApplyPersistentData() {
        // After loading game, retrieve variable values from Lua:
        Money = DialogueLua.GetVariable("Money").AsInt;
        Mathematics = DialogueLua.GetVariable("Mathematics").AsBool;
        Achievement = DialogueLua.GetVariable("Achievement").AsBool;
    }
} 
The PersistentDataTemplate.cs file has a lot of comments explaining how it works.

HaiiroMukuro wrote:Also for the feature demoscript.
I'm using the feature demo script because I'm trying to create my own prefab for the main menu. Now, it's working, but there's i wanted to try. Making the menu open by the button. My question is how to make the feature demo script to be open using the button?
Thanks for the help in advance. :D
Edit FeatureDemo.cs, and change line 70 from:

Code: Select all

private void SetMenuStatus(bool open) {
to:

Code: Select all

public void SetMenuStatus(bool open) {
Then point your button to call FeatureDemo.SetMenuStatus, and tick the checkbox.

Re: Creating c# script for persistence data and the feature demo.

Posted: Fri Feb 24, 2017 2:12 pm
by HaiiroMukuro
It work! Thank you, have a good day! :D

Re: Creating c# script for persistence data and the feature demo.

Posted: Mon Mar 06, 2017 4:10 pm
by HaiiroMukuro
Good day! :D
How to reset variables?
For example, I play the game and manage to get 50 coins. Then after getting some coins, I decided to click main menu and start the game again.
But the problem is everytime I start the game. The 50 coins that I collected previously still there.
My problem is how to reset the variables?
Thank you and have a great day! :D

Re: Creating c# script for persistence data and the feature demo.

Posted: Mon Mar 06, 2017 4:16 pm
by Tony Li
Hi,

If you're using a GameSaver component, you can use GameSaver.RestartGame(). If your scene also has a LevelManager, it will call LevelManager.RestartGame() (see below). Otherwise it will just reset the database.

If you're using a LevelManager component without GameSaver, you can use LevelManager.RestartGame() to reset the database and load the default starting level set in the LevelManager component.

If you're using neither GameSaver nor LevelManager, use DialogueManager.ResetDatabase() to reset the variables.

Re: Creating c# script for persistence data and the feature demo.

Posted: Tue Mar 07, 2017 1:29 pm
by HaiiroMukuro
Hello, am I doing right?

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class ResetGame : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update ()
    {
       
	
	}
    public void ResetData()
    {
        GameSaver.RestartGame();
	
    }
}
I'm using the game saver right now also I have a question. It will reset the playerprefs? I hope not. :lol:
Thanks for the help! :D

Re: Creating c# script for persistence data and the feature demo.

Posted: Tue Mar 07, 2017 1:35 pm
by Tony Li
Hi,

Try this:

Code: Select all

FindObjectOfType<GameSaver>().RestartGame();
It will not clear your PlayerPrefs.

Re: Creating c# script for persistence data and the feature demo.

Posted: Tue Mar 07, 2017 2:53 pm
by HaiiroMukuro
Still not working. What if I use this one?

Code: Select all

public void ResetData()
    {
        FindObjectOfType<DialogueManager>().ResetDatabase();
    }

Re: Creating c# script for persistence data and the feature demo.

Posted: Tue Mar 07, 2017 3:13 pm
by Tony Li
Try this:

Code: Select all

public void ResetData()
{
    DialogueManager.ResetDatabase(DatabaseResetOptions.RevertToDefault);
} 

Re: Creating c# script for persistence data and the feature demo.

Posted: Tue Mar 07, 2017 4:09 pm
by HaiiroMukuro
It's working! :D Thanks for the help. I appreciate it. Have a great day! :D