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

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
HaiiroMukuro
Posts: 80
Joined: Sun Jul 17, 2016 3:38 pm

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

Post 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
User avatar
Tony Li
Posts: 21069
Joined: Thu Jul 18, 2013 1:27 pm

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

Post 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.
HaiiroMukuro
Posts: 80
Joined: Sun Jul 17, 2016 3:38 pm

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

Post by HaiiroMukuro »

It work! Thank you, have a good day! :D
HaiiroMukuro
Posts: 80
Joined: Sun Jul 17, 2016 3:38 pm

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

Post 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
User avatar
Tony Li
Posts: 21069
Joined: Thu Jul 18, 2013 1:27 pm

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

Post 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.
HaiiroMukuro
Posts: 80
Joined: Sun Jul 17, 2016 3:38 pm

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

Post 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
User avatar
Tony Li
Posts: 21069
Joined: Thu Jul 18, 2013 1:27 pm

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

Post by Tony Li »

Hi,

Try this:

Code: Select all

FindObjectOfType<GameSaver>().RestartGame();
It will not clear your PlayerPrefs.
HaiiroMukuro
Posts: 80
Joined: Sun Jul 17, 2016 3:38 pm

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

Post by HaiiroMukuro »

Still not working. What if I use this one?

Code: Select all

public void ResetData()
    {
        FindObjectOfType<DialogueManager>().ResetDatabase();
    }
User avatar
Tony Li
Posts: 21069
Joined: Thu Jul 18, 2013 1:27 pm

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

Post by Tony Li »

Try this:

Code: Select all

public void ResetData()
{
    DialogueManager.ResetDatabase(DatabaseResetOptions.RevertToDefault);
} 
HaiiroMukuro
Posts: 80
Joined: Sun Jul 17, 2016 3:38 pm

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

Post by HaiiroMukuro »

It's working! :D Thanks for the help. I appreciate it. Have a great day! :D
Post Reply