Page 1 of 1

Applying Save Data on Player Spawn

Posted: Fri Dec 16, 2022 12:33 am
by DrewThomasArt
I've been restructuring my game into multiplayer, and players must be spawned as prefabs when they enter servers.
So the issue is the spawned prefab does not have the recorded data, and they can't retrieve it immediately upon scene load because there is loading time for finding the server.

Is there a way to call the ApplyData method from the Saver script in a Start function on the player?

I try to use "GetComponent<PlayerStats>().ApplyData(s)" but it tells me s doesn't exist in the current context. What else would be the parameter? Can methods be called from Saver scripts?

Here is a short portion of the Apply Data function in my Saver script (just without every single int that's being saved as a string),

Code: Select all

public override void ApplyData(string s)
    {
       if (string.IsNullOrEmpty(s)) return; // If we didn't receive any save data, exit immediately.
        var data = SaveSystem.Deserialize<SaveData>(s);
        if (data == null) return; 
         GetComponent<PlayerAttack>().damage = data.damage;
Thank you!

Re: Applying Save Data on Player Spawn

Posted: Fri Dec 16, 2022 9:57 pm
by Tony Li
Hi,

Yes. You can apply save data manually. Example:

Code: Select all

var saver = GetComponent<PlayerStatsSaver>();
var data = PixelCrushers.SaveSystem.currentSavedGameData.GetData(saver.key);
saver.ApplyData(data);

Re: Applying Save Data on Player Spawn

Posted: Sat Dec 17, 2022 12:29 am
by DrewThomasArt
Tony Li wrote: Fri Dec 16, 2022 9:57 pm Hi,

Yes. You can apply save data manually. Example:

Code: Select all

var saver = GetComponent<PlayerStatsSaver>();
var data = PixelCrushers.SaveSystem.currentSavedGameData.GetData(saver.key);
saver.ApplyData(data);
Perfect, thank you

Re: Applying Save Data on Player Spawn

Posted: Sat Dec 17, 2022 8:22 am
by Tony Li
Glad to help!