Applying Save Data on Player Spawn

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
DrewThomasArt
Posts: 60
Joined: Thu Mar 24, 2022 12:07 am

Applying Save Data on Player Spawn

Post 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!
Last edited by DrewThomasArt on Fri Dec 16, 2022 4:51 pm, edited 1 time in total.
User avatar
Tony Li
Posts: 21962
Joined: Thu Jul 18, 2013 1:27 pm

Re: Applying Save Data on Player Spawn

Post 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);
DrewThomasArt
Posts: 60
Joined: Thu Mar 24, 2022 12:07 am

Re: Applying Save Data on Player Spawn

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

Re: Applying Save Data on Player Spawn

Post by Tony Li »

Glad to help!
Post Reply