How To Reference Savable Integers
-
- Posts: 60
- Joined: Thu Mar 24, 2022 12:07 am
How To Reference Savable Integers
I'm following this tutorial:
https://pixelcrushers.com/phpbb/viewtop ... f=9&t=4763
My question is, how does it know what ints to save, like currentHealth and coins? It won't let me call anything from within the script. When I try to use GetComponent or .Find() it says they don't exist or it must be in reference to an object, even though the Saver script is on my player.
I don't think the Saver scripts are allowed to call other components, so how exactly will it know what to save?
(we can ignore the record data() code, forgot to fix that before the screenshot) Or is there another way to save ints like currentHealth and coins. Thanks for any help
https://pixelcrushers.com/phpbb/viewtop ... f=9&t=4763
My question is, how does it know what ints to save, like currentHealth and coins? It won't let me call anything from within the script. When I try to use GetComponent or .Find() it says they don't exist or it must be in reference to an object, even though the Saver script is on my player.
I don't think the Saver scripts are allowed to call other components, so how exactly will it know what to save?
(we can ignore the record data() code, forgot to fix that before the screenshot) Or is there another way to save ints like currentHealth and coins. Thanks for any help
Re: How To Reference Savable Integers
Hi,
In general, you'll only want to put serializable variables in your SaveData class. Try something like this:
In general, you'll only want to put serializable variables in your SaveData class. Try something like this:
Code: Select all
// Assumes this script is on a GameObject that also has PlayerHealth & CoinCollecter scripts.
public class PlayerStats : Saver
{
[Serializable]
public class SaveData
{
public int currentHealth;
public int coins;
}
public override string RecordData()
{
var data = new SaveData();
data.currentHealth = GetComponent<PlayerHealth>().currentHealth;
data.coins = GetComponent<CoinCollector>().coins;
return SaveSystem.Serialize(data);
}
public override ApplyData(string s)
{
if (string.IsNullOrEmpty(s)) return;
var data = SaveSystem.Deserialize<SaveData>(s);
if (data == null) return;
GetComponent<PlayerHealth>().currentHealth = data.currentHealth;
GetComponent<CoinCollector>().coins = data.coins;
// NOTE: You might want to update visual representation of health & coins here
}
}
-
- Posts: 60
- Joined: Thu Mar 24, 2022 12:07 am
Re: How To Reference Savable Integers
Thank you for writing that, that fixed all of the errors.Tony Li wrote: ↑Tue Nov 01, 2022 10:06 pm Hi,
In general, you'll only want to put serializable variables in your SaveData class. Try something like this:
Code: Select all
// Assumes this script is on a GameObject that also has PlayerHealth & CoinCollecter scripts. public class PlayerStats : Saver { [Serializable] public class SaveData { public int currentHealth; public int coins; } public override string RecordData() { var data = new SaveData(); data.currentHealth = GetComponent<PlayerHealth>().currentHealth; data.coins = GetComponent<CoinCollector>().coins; return SaveSystem.Serialize(data); } public override ApplyData(string s) { if (string.IsNullOrEmpty(s)) return; var data = SaveSystem.Deserialize<SaveData>(s); if (data == null) return; GetComponent<PlayerHealth>().currentHealth = data.currentHealth; GetComponent<CoinCollector>().coins = data.coins; // NOTE: You might want to update visual representation of health & coins here } }
It seems now that when I record my health, it's being applied as 0 and instantly killing my character upon load.
To test this, I told the health to be 1000hp on load but the healthbar to use the data.currentHealth. The Healthbar shows 0 HP while my character has 1000hp Even though I'm recording the data at 1000hp, as shown in this screenshot Is it possibly because I'm not recording the data correctly? I haven't done anything except what's in the script. However, there is no option to call the "RecordData" method from this script when I try to call it as an event in the conversation manager It resets the coins to 0 as well
Re: How To Reference Savable Integers
Hi,
Are there any errors or warnings in the Console window?
There's no need to call RecordData() yourself. Since it's a Saver script, it just needs to be on the same GameObject as your PlayerHealth + CoinCollector scripts. The Save System will know to call RecordData() and ApplyData() at the correct times. You should also assign a unique Key value such as "playerStats".
As a test, after the "if (data == null) return" line in ApplyData(), add this line:
Also temporarily tick the Debug checkboxes on the SaveSystem and PlayerPrefsSavedGameDataStorer components. When you save the game, the Console will show the save data in JSON format. It will be a little hard to read, but look for something like:
That will tell you what values it has saved.
When you load the saved game, the Debug.Log() line above will show something like:
Are there any errors or warnings in the Console window?
There's no need to call RecordData() yourself. Since it's a Saver script, it just needs to be on the same GameObject as your PlayerHealth + CoinCollector scripts. The Save System will know to call RecordData() and ApplyData() at the correct times. You should also assign a unique Key value such as "playerStats".
As a test, after the "if (data == null) return" line in ApplyData(), add this line:
Code: Select all
if (data == null) return;
Debug.Log($"Restoring currentHealth={data.currentHealth}, coins={data.coins}"); //<-- ADD THIS
Code: Select all
{key:"playerStats"}, data:{ currentHealth:1000, coins=99 }}
When you load the saved game, the Debug.Log() line above will show something like:
Code: Select all
Restoring currentHealth=123, coins=456
-
- Posts: 60
- Joined: Thu Mar 24, 2022 12:07 am
Re: How To Reference Savable Integers
Thank you, I can see the values now, that has fixed the issue as well as makes the player spawn in the correct place for whatever reason. It also made me realize to put the starting currentHealth not to 0 in the inspector as well. Apparently that was registering before the script was. Either way, everything is working absolutely perfectly now thanks to you
Re: How To Reference Savable Integers
Glad to help!
-
- Posts: 60
- Joined: Thu Mar 24, 2022 12:07 am
Re: How To Reference Savable Integers
Somewhere along the way, things got buggy and now there's no proximity selector UI as shown below: As you can see, I have the proximity selector. But, when I press play the "selector use standard UI elements" turns inactive.
It was working fine until I started playing around with saves and spamming it, but it's very possible I did something to the Dialogue Manager. I made a new one (in the screenshot above) and the issue persists with all NPCS and Usable game objects. I haven't changed anything on any of them. I rewatched the tutorial that demonstrates it, made sure my collider was a trigger, etc.
EDIT: After getting this error:
"Dialogue System: SelectorUseStandardUIElements can't find a StandardUISelectorElements component in the scene."
I added the component to my player and am now getting this error: Also my professor and his friends said my game was brilliant and at some point I said I lot of it is due to this system and getting help from a guy named Tony Li. So thank you
Re: How To Reference Savable Integers
Hi,
Remove the Standard UI Selector Elements component from your player. It should be on the selector UI instead. To do this, inspect the Dialogue Manager GameObject's Instantiate Prefabs component. Assign the "Basic Standard UI Selector Elements" prefab (or a customized copy if you prefer) to the Prefabs list. Or, if you don't want to add it to Instantiate Prefabs, just add it directly to a Canvas that's a child of the Dialogue Manager.DrewThomasArt wrote: ↑Wed Nov 02, 2022 6:13 pm EDIT: After getting this error:
"Dialogue System: SelectorUseStandardUIElements can't find a StandardUISelectorElements component in the scene."
I added the component to my player and am now getting this error:
Great job! And glad to help.DrewThomasArt wrote: ↑Wed Nov 02, 2022 6:13 pmAlso my professor and his friends said my game was brilliant and at some point I said I lot of it is due to this system and getting help from a guy named Tony Li. So thank you
-
- Posts: 60
- Joined: Thu Mar 24, 2022 12:07 am
Re: How To Reference Savable Integers
I've tried this, (as well as just putting it on the Canvas that's on the Dialogue Manager) It then gives me this error, And the "Selector Use Standard UI Elements" component on the player becomes inactive on game start. Clicking it active doesn't do anythingTony Li wrote: ↑Wed Nov 02, 2022 7:51 pm Hi,Remove the Standard UI Selector Elements component from your player. It should be on the selector UI instead. To do this, inspect the Dialogue Manager GameObject's Instantiate Prefabs component. Assign the "Basic Standard UI Selector Elements" prefab (or a customized copy if you prefer) to the Prefabs list. Or, if you don't want to add it to Instantiate Prefabs, just add it directly to a Canvas that's a child of the Dialogue Manager.DrewThomasArt wrote: ↑Wed Nov 02, 2022 6:13 pm EDIT: After getting this error:
"Dialogue System: SelectorUseStandardUIElements can't find a StandardUISelectorElements component in the scene."
I added the component to my player and am now getting this error:
Screenshot (17).png
Great job! And glad to help.DrewThomasArt wrote: ↑Wed Nov 02, 2022 6:13 pmAlso my professor and his friends said my game was brilliant and at some point I said I lot of it is due to this system and getting help from a guy named Tony Li. So thank you
Re: How To Reference Savable Integers
Hi,
Can you compare the setup to DemoScene1? Make a note of the components that are on the Player GameObject and the Dialogue Manager GameObject. In DemoScene1, the Player has a Selector instead of a Proximity Selector, so just pretend it's a Proximity Selector for the point of comparison.
Can you compare the setup to DemoScene1? Make a note of the components that are on the Player GameObject and the Dialogue Manager GameObject. In DemoScene1, the Player has a Selector instead of a Proximity Selector, so just pretend it's a Proximity Selector for the point of comparison.