0
\$\begingroup\$

I'm working on a dungeons and dragons first person game and I've been finishing up the character creation section. I've gotten to the point that you can choose your character's stats / ability scores and save them on your computer but now I need a way to load them when they join a server. Right now, every time someone joins the server I'm hosting, they load the stats saved on my computer and I have no idea why. I'd really appreciate any help :)

This is my SaveSystem

using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Mirror;

public class SaveSystem {

public static void SavePlayer(CharacterSheet player)
{
    BinaryFormatter formatter = new BinaryFormatter();
    string path = Application.persistentDataPath + "/player.data";
    FileStream stream = new FileStream(path, FileMode.Create);

    PlayerData data = new PlayerData(player);

    formatter.Serialize(stream, data);
    stream.Close();
}


public static PlayerData LoadPlayer()
{
    string path = Application.persistentDataPath + "/player.data";
    if (File.Exists(path))
    {
        BinaryFormatter formatter = new BinaryFormatter();
        FileStream stream = new FileStream(path, FileMode.Open);

        PlayerData data = formatter.Deserialize(stream) as PlayerData;
        stream.Close();

        return data;
    }
    else
    {
        Debug.LogError("Save file not found in " + path);
        return null;
    }
}

public static void DeletePlayer()
{
    string path = Application.persistentDataPath + "/player.data";


    File.Delete(path);

}

[ClientRpc]
public static void RpcLoadCharacterSheet(NetworkConnection conn)
{
    conn.identity.GetComponent<CharacterSheet>().LoadPlayer();
}
}

This is my Custom NetworkManager

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
using UnityEngine.SceneManagement;
using System;
using System.Linq;

public class NetworkManagerLobby : NetworkManager
{

public List<CharacterSheet> Players;

[Scene] [SerializeField] private string menuScene = string.Empty;

/*[Header("Room")]
[SerializeField] private NetworkRoomPlayerLobby PlayerPrefab = null;*/

public static event Action OnClientConnected;
public static event Action OnClientDisconnected;

public override void OnStartServer() => spawnPrefabs = Resources.LoadAll<GameObject> 
("SpawnablePrefabs").ToList();


public override void OnStartClient()
{
    var spawnablePrefabs = Resources.LoadAll<GameObject>("SpawnablePrefabs");

    foreach (var prefab in spawnablePrefabs)
    {
        ClientScene.RegisterPrefab(prefab);

    }
}


public override void OnClientConnect(NetworkConnection conn)
{
    base.OnClientConnect(conn);

    //conn.identity.gameObject.GetComponent<CharacterSheet>().LoadPlayer();

    OnClientConnected?.Invoke();
}



public override void OnClientDisconnect(NetworkConnection conn)
{
    base.OnClientDisconnect(conn);

    OnClientDisconnected?.Invoke();
}



public override void OnServerConnect(NetworkConnection conn)
{

    base.OnServerConnect(conn);



}


public override void OnServerAddPlayer(NetworkConnection conn)
{
    base.OnServerAddPlayer(conn);
    Players.Add(conn.identity.gameObject.GetComponent<CharacterSheet>());

    SaveSystem.RpcLoadCharacterSheet(conn);
}



}

This is the LoadPlayer() function called in the RPC

public void LoadPlayer()
{

    PlayerData data = SaveSystem.LoadPlayer();


    strMod = data.strMod;
    dexMod = data.dexMod;
    conMod = data.conMod;
    intMod = data.intMod;
    wisMod = data.wisMod; 
    chaMod = data.chaMod;
 strScore = data.strScore;
    dexScore = data.dexScore;
    conScore = data.conScore;
    intScore = data.intScore;
    wisScore = data.wisScore;
    chaScore = data.chaScore;
 humanChoice = data.humanChoice;


 AC = data.AC;
    FlatFootedAC = data.FlatFootedAC;
    TouchAC = data.TouchAC;
    CMD = data.CMD;
    CMB = data.CMB;
    dodge = data.dodge;
    deflect = data.deflect;



movementSpeed = data.movementSpeed;
healthPoints = data.healthPoints;
Initiative = data.Initiative;


CharacterName = data.CharacterName;
    GoodEvilAlignment = data.GoodEvilAlignment;
    ChaoticLawfulAlignment = data.ChaoticLawfulAlignment;
    Deity = data.Deity;
    Size = data.Size;
    Age = data.Age;
    Height = data.Height;
    Weight = data.Weight;
    HairColour = data.HairColour;
    EyeColour = data.EyeColour;
    Class = data.Class;
    Race = data.Race;
learnedLanguages = data.learnedLanguages;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.