Search Unity

Save scene before closing server

Discussion in 'Multiplayer' started by BrunoPuccio, Dec 21, 2017.

  1. BrunoPuccio

    BrunoPuccio

    Joined:
    Dec 13, 2017
    Posts:
    22
    hi, i am trying to make a simple multiplayer proyect in which each player haves an inventory and can modify the scene, my question is how can i save the state of the scene(also saving each player's inventory) so next time the same host opens the server everything is there?
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    There's no way to directly save the "scene" as if you're in the editor from a standalone build. What you can do is record all changes to the scene that you want to preserve to disk, as well as the inventory, and then read that back when the game is reloaded. So you'd record the location and rotation of objects, what type of prefab, etc. Using either JSON format or writing to a database is probably your best bet.
     
    BrunoPuccio likes this.
  3. Bertlapp

    Bertlapp

    Joined:
    Sep 7, 2015
    Posts:
    44
    I wrote a class where you can specify your datatype and save it to the device (should work on all platforms)
    Use it as follow:
    Code (CSharp):
    1.  
    2. WriteLoad.SaveData<YOUR_TYPE>(REFERENCE);
    3. WriteLoad.LoadData<YOUR_TYPE>();
    4.  
    YOUR_TYPE can be anything you define. A string, List of data, Dict, custom class, etc...

    Write Load class:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Runtime.Serialization.Formatters.Binary;
    3. using System;
    4. using System.IO;
    5. using UnityEngine;
    6.  
    7. public class WriteLoad : MonoBehaviour {
    8.  
    9.     //Location of file on windows
    10.     //C:\Users\your name\AppData\LocalLow\DefaultCompany\project_name
    11.  
    12.     public static void SaveData<T>(T data)
    13.     {
    14.         BinaryFormatter bf = new BinaryFormatter ();
    15.         FileStream file = File.Create (Application.persistentDataPath + Parameters.dataName);
    16.  
    17.         bf.Serialize (file, data);
    18.         file.Close ();
    19.     }
    20.  
    21.     public static T LoadData<T>()
    22.     {
    23.         if (File.Exists (Application.persistentDataPath + Parameters.dataName)) {
    24.  
    25.             BinaryFormatter bf = new BinaryFormatter ();
    26.             FileStream file = File.Open (Application.persistentDataPath + Parameters.dataName, FileMode.Open);
    27.             T data = (T)bf.Deserialize (file);
    28.             file.Close ();
    29.  
    30.             return data;
    31.         }
    32.  
    33.         T dataEmpty = default(T);
    34.         return dataEmpty;
    35.     }
    36.  
    37. }
    I encourage you to make a Class with constant values to set common data such as dataname

    Example
    Code (CSharp):
    1. public class Parameters
    2. {
    3.     public static const string dataName = "myDataName";
    4.  
    5. }
    Enjoy
     
    Last edited: Dec 22, 2017
    BrunoPuccio and bertenernie like this.