Page 1 of 2

Saving a bool state from a custom script

Posted: Tue Jan 25, 2022 6:22 am
by baxta86
Hi Tony,

Curious to know if there is a way in which I can save a bool state (or an int, which holds the players health) from one of my custom scripts, using the in-built saver?

Cheers

Re: Saving a bool state from a custom script

Posted: Tue Jan 25, 2022 8:51 am
by Tony Li
Hi,

Please see: How To: Write Custom Savers.

The first example demonstrates how to save a bool in a script.

The second example demonstrates how to save a set of ints in a script.

Re: Saving a bool state from a custom script

Posted: Tue Jan 25, 2022 5:50 pm
by baxta86
Thanks Tony!

Re: Saving a bool state from a custom script

Posted: Tue Jan 25, 2022 6:08 pm
by Tony Li
Happy to help! If you have any questions about it, just let me know.

Re: Saving a bool state from a custom script

Posted: Tue Jan 25, 2022 7:33 pm
by baxta86
Although that all works perfectly, again thank you! I have stumbled on another minor issue.

If I assign a Save System Method to one of my buttons and then assign the SaveSystemMethods.SaveSlot to the OnClick(), the game data saves, however the data from the PositionSaver on my player gameObject doesn't save?

Yet, if I save using the Save Game button from DemoMenu menu, the Position Saver data is saved.

Aren't these two calling the same function?

Re: Saving a bool state from a custom script

Posted: Tue Jan 25, 2022 7:38 pm
by Tony Li
Hi,

Yes, they call the same method. Are you saving to the same slot? The DemoMenu saves to slot 1.

Does your player's PositionSaver have a unique Key value?

Re: Saving a bool state from a custom script

Posted: Tue Jan 25, 2022 8:15 pm
by baxta86
I've cleared all the saves to be safe
.
I have tried saving to both slot 1 and 2, but neither are recording the player position. When I look at the saved data from the show button, I can see it saves everything but the player position.

I'm not using a unique key either.

SAVING FROM DEMO MENU

Code: Select all

Save1: {
    "m_sceneName": "IntroLevelOne",
    "m_list": [
        {
            "key": "Dialogue Manager",
            "sceneIndex": -1,
            "data": "Variable={Alert=\"\", HasTalkedNPC=false, HasTalkedActor3=false, CellAmount=0, [\"BlueGenerator1.1\"]=false, [\"BlueGenerator1.2\"]=false, InteractionGuide=false, MeleeGuide=false, SpringGuide=false, ShootingGuide=false, GameSaved=false, Actor=\"\", Conversant=\"\", ActorIndex=\"\", ConversantIndex=\"\"}; Actor={Player={Name=\"Player\", Pictures=\"[]\", Description=\"\", IsPlayer=true}, Ally={Name=\"Ally\", Pictures=\"[]\", Description=\"\", IsPlayer=false}, NPC={Name=\"NPC\", Pictures=\"[]\", Description=\"\", IsPlayer=false}, Charger={Name=\"Charger\", Pictures=\"[]\", Description=\"\", IsPlayer=false}, ExclamationMarkOne={Name=\"ExclamationMarkOne\", Pictures=\"[]\", Description=\"\", IsPlayer=false}, ExclamationMarkTwo={Name=\"ExclamationMarkTwo\", Pictures=\"[]\", Description=\"\", IsPlayer=false}}; StatusTable = \"\"; RelationshipTable = \"\"; "
        },
        {
            "key": "ActivationButton (1)",
            "sceneIndex": -1,
            "data": "0"
        },
        {
            "key": "ActivationButton",
            "sceneIndex": -1,
            "data": "0"
        },
        {
            "key": "Player",
            "sceneIndex": -1,
            "data": "{\n    \"scene\": 0,\n    \"position\": {\n        \"x\": -83.49055480957031,\n        \"y\": -4.186470031738281,\n        \"z\": 0.0\n    },\n    \"rotation\": {\n        \"x\": 0.0,\n        \"y\": 0.0,\n        \"z\": 0.0,\n        \"w\": 1.0\n    }\n}"
        }
    ]
}
UnityEngine.Debug:Log (object)
PixelCrushers.PlayerPrefsSavedGameDataStorerEditor:OnDrawElement (UnityEngine.Rect,int,bool,bool) (at Assets/Plugins/Pixel Crushers/Common/Scripts/Editor/Save System/PlayerPrefsSavedGameDataStorerEditor.cs:81)
UnityEditorInternal.ReorderableList:DoListElements (UnityEngine.Rect,UnityEngine.Rect)
UnityEditorInternal.ReorderableList:DoLayoutList ()
PixelCrushers.PlayerPrefsSavedGameDataStorerEditor:DrawSavedGameList () (at Assets/Plugins/Pixel Crushers/Common/Scripts/Editor/Save System/PlayerPrefsSavedGameDataStorerEditor.cs:53)
PixelCrushers.PlayerPrefsSavedGameDataStorerEditor:OnInspectorGUI () (at Assets/Plugins/Pixel Crushers/Common/Scripts/Editor/Save System/PlayerPrefsSavedGameDataStorerEditor.cs:48)
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)
SAVING FROM BUTTON

Code: Select all

Save2: {
    "m_sceneName": "IntroLevelOne",
    "m_list": [
        {
            "key": "Dialogue Manager",
            "sceneIndex": -1,
            "data": "Variable={Alert=\"\", HasTalkedNPC=false, HasTalkedActor3=false, CellAmount=0, [\"BlueGenerator1.1\"]=false, [\"BlueGenerator1.2\"]=false, InteractionGuide=false, MeleeGuide=false, SpringGuide=false, ShootingGuide=false, GameSaved=false, Actor=\"\", Conversant=\"\", ActorIndex=\"\", ConversantIndex=\"\"}; Actor={Player={Name=\"Player\", Pictures=\"[]\", Description=\"\", IsPlayer=true}, Ally={Name=\"Ally\", Pictures=\"[]\", Description=\"\", IsPlayer=false}, NPC={Name=\"NPC\", Pictures=\"[]\", Description=\"\", IsPlayer=false}, Charger={Name=\"Charger\", Pictures=\"[]\", Description=\"\", IsPlayer=false}, ExclamationMarkOne={Name=\"ExclamationMarkOne\", Pictures=\"[]\", Description=\"\", IsPlayer=false}, ExclamationMarkTwo={Name=\"ExclamationMarkTwo\", Pictures=\"[]\", Description=\"\", IsPlayer=false}}; StatusTable = \"\"; RelationshipTable = \"\"; "
        },
        {
            "key": "ActivationButton (1)",
            "sceneIndex": -1,
            "data": "0"
        },
        {
            "key": "ActivationButton",
            "sceneIndex": -1,
            "data": "1"
        }
    ]
}
UnityEngine.Debug:Log (object)
PixelCrushers.PlayerPrefsSavedGameDataStorerEditor:OnDrawElement (UnityEngine.Rect,int,bool,bool) (at Assets/Plugins/Pixel Crushers/Common/Scripts/Editor/Save System/PlayerPrefsSavedGameDataStorerEditor.cs:81)
UnityEditorInternal.ReorderableList:DoListElements (UnityEngine.Rect,UnityEngine.Rect)
UnityEditorInternal.ReorderableList:DoLayoutList ()
PixelCrushers.PlayerPrefsSavedGameDataStorerEditor:DrawSavedGameList () (at Assets/Plugins/Pixel Crushers/Common/Scripts/Editor/Save System/PlayerPrefsSavedGameDataStorerEditor.cs:53)
PixelCrushers.PlayerPrefsSavedGameDataStorerEditor:OnInspectorGUI () (at Assets/Plugins/Pixel Crushers/Common/Scripts/Editor/Save System/PlayerPrefsSavedGameDataStorerEditor.cs:48)
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

Re: Saving a bool state from a custom script

Posted: Tue Jan 25, 2022 8:17 pm
by Tony Li
Try assigning a unique key to the PositionSaver such as "playerPosition".

Re: Saving a bool state from a custom script

Posted: Tue Jan 25, 2022 8:20 pm
by baxta86
Still no luck after changing the key. When i save through the Demo Menu, it picks up the key into slot 1 - if i save through the button which is pointing to slot 2, no player position data still.

I'm thinking i must have some code in my scene that's setting the player back to its starting position. But, it's odd that when i save and load through the Demo Menu, it works perfectly?

DEMO MENU

Code: Select all

Save1: {
    "m_sceneName": "IntroLevelOne",
    "m_list": [
        {
            "key": "Dialogue Manager",
            "sceneIndex": -1,
            "data": "Variable={Alert=\"\", HasTalkedNPC=false, HasTalkedActor3=false, CellAmount=0, [\"BlueGenerator1.1\"]=false, [\"BlueGenerator1.2\"]=false, InteractionGuide=false, MeleeGuide=false, SpringGuide=false, ShootingGuide=false, GameSaved=false, Actor=\"\", Conversant=\"\", ActorIndex=\"\", ConversantIndex=\"\"}; Actor={Player={Name=\"Player\", Pictures=\"[]\", Description=\"\", IsPlayer=true}, Ally={Name=\"Ally\", Pictures=\"[]\", Description=\"\", IsPlayer=false}, NPC={Name=\"NPC\", Pictures=\"[]\", Description=\"\", IsPlayer=false}, Charger={Name=\"Charger\", Pictures=\"[]\", Description=\"\", IsPlayer=false}, ExclamationMarkOne={Name=\"ExclamationMarkOne\", Pictures=\"[]\", Description=\"\", IsPlayer=false}, ExclamationMarkTwo={Name=\"ExclamationMarkTwo\", Pictures=\"[]\", Description=\"\", IsPlayer=false}}; StatusTable = \"\"; RelationshipTable = \"\"; "
        },
        {
            "key": "ActivationButton (1)",
            "sceneIndex": -1,
            "data": "1"
        },
        {
            "key": "ActivationButton",
            "sceneIndex": -1,
            "data": "1"
        },
        {
            "key": "playerPosition",
            "sceneIndex": -1,
            "data": "{\n    \"scene\": 0,\n    \"position\": {\n        \"x\": -64.79705810546875,\n        \"y\": -4.186966896057129,\n        \"z\": 0.0\n    },\n    \"rotation\": {\n        \"x\": 0.0,\n        \"y\": -1.0,\n        \"z\": 0.0,\n        \"w\": 4.371138828673793e-8\n    }\n}"
        }
    ]
}
UnityEngine.Debug:Log (object)
PixelCrushers.PlayerPrefsSavedGameDataStorerEditor:OnDrawElement (UnityEngine.Rect,int,bool,bool) (at Assets/Plugins/Pixel Crushers/Common/Scripts/Editor/Save System/PlayerPrefsSavedGameDataStorerEditor.cs:81)
UnityEditorInternal.ReorderableList:DoListElements (UnityEngine.Rect,UnityEngine.Rect)
UnityEditorInternal.ReorderableList:DoLayoutList ()
PixelCrushers.PlayerPrefsSavedGameDataStorerEditor:DrawSavedGameList () (at Assets/Plugins/Pixel Crushers/Common/Scripts/Editor/Save System/PlayerPrefsSavedGameDataStorerEditor.cs:53)
PixelCrushers.PlayerPrefsSavedGameDataStorerEditor:OnInspectorGUI () (at Assets/Plugins/Pixel Crushers/Common/Scripts/Editor/Save System/PlayerPrefsSavedGameDataStorerEditor.cs:48)
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)
BUTTON

Code: Select all

Save2: {
    "m_sceneName": "IntroLevelOne",
    "m_list": [
        {
            "key": "Dialogue Manager",
            "sceneIndex": -1,
            "data": "Variable={Alert=\"\", HasTalkedNPC=false, HasTalkedActor3=false, CellAmount=0, [\"BlueGenerator1.1\"]=false, [\"BlueGenerator1.2\"]=false, InteractionGuide=false, MeleeGuide=false, SpringGuide=false, ShootingGuide=false, GameSaved=false, Actor=\"\", Conversant=\"\", ActorIndex=\"\", ConversantIndex=\"\"}; Actor={Player={Name=\"Player\", Pictures=\"[]\", Description=\"\", IsPlayer=true}, Ally={Name=\"Ally\", Pictures=\"[]\", Description=\"\", IsPlayer=false}, NPC={Name=\"NPC\", Pictures=\"[]\", Description=\"\", IsPlayer=false}, Charger={Name=\"Charger\", Pictures=\"[]\", Description=\"\", IsPlayer=false}, ExclamationMarkOne={Name=\"ExclamationMarkOne\", Pictures=\"[]\", Description=\"\", IsPlayer=false}, ExclamationMarkTwo={Name=\"ExclamationMarkTwo\", Pictures=\"[]\", Description=\"\", IsPlayer=false}}; StatusTable = \"\"; RelationshipTable = \"\"; "
        },
        {
            "key": "ActivationButton (1)",
            "sceneIndex": -1,
            "data": "0"
        },
        {
            "key": "ActivationButton",
            "sceneIndex": -1,
            "data": "1"
        }
    ]
}
UnityEngine.Debug:Log (object)
PixelCrushers.PlayerPrefsSavedGameDataStorerEditor:OnDrawElement (UnityEngine.Rect,int,bool,bool) (at Assets/Plugins/Pixel Crushers/Common/Scripts/Editor/Save System/PlayerPrefsSavedGameDataStorerEditor.cs:81)
UnityEditorInternal.ReorderableList:DoListElements (UnityEngine.Rect,UnityEngine.Rect)
UnityEditorInternal.ReorderableList:DoLayoutList ()
PixelCrushers.PlayerPrefsSavedGameDataStorerEditor:DrawSavedGameList () (at Assets/Plugins/Pixel Crushers/Common/Scripts/Editor/Save System/PlayerPrefsSavedGameDataStorerEditor.cs:53)
PixelCrushers.PlayerPrefsSavedGameDataStorerEditor:OnInspectorGUI () (at Assets/Plugins/Pixel Crushers/Common/Scripts/Editor/Save System/PlayerPrefsSavedGameDataStorerEditor.cs:48)
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

Re: Saving a bool state from a custom script

Posted: Tue Jan 25, 2022 8:34 pm
by Tony Li
At the point that you click the button, is the Position Saver component enabled and on an active GameObject?