Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Scene Progress Saving (through c#) Unclear

Discussion in 'Editor & General Support' started by Simplisticated_Development, Oct 7, 2023.

  1. Simplisticated_Development

    Simplisticated_Development

    Joined:
    Jun 26, 2023
    Posts:
    61
    Hello everyone-

    So in a game I have been making I am attempting to make a system that saves the entire scene. I know one way to do this is through saving variables. I could, in theory, attach a script to every moving part of the scene. This script would detect changes in position/rotation, and then save. Is there an easier way without attaching a script to EVERY object?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Saving everything is by far the hardest most-fraught way to go. I highly recommend rethinking this approach.

    If you insist on doing this,
    Find*()
    and
    GetComponent*()
    are probably where you will need to start the adventure.

    There are assets on the asset store such as EasySave. I have not used them. They may even do this.

    Whatever you do, this is the basic process:

    Load/Save steps:

    https://forum.unity.com/threads/save-system-questions.930366/#post-6087384

    An excellent discussion of loading/saving in Unity3D by Xarbrough:

    https://forum.unity.com/threads/save-system.1232301/#post-7872586

    Loading/Saving ScriptableObjects by a proxy identifier such as name:

    https://forum.unity.com/threads/use...lds-in-editor-and-build.1327059/#post-8394573

    When loading, you can never re-create a MonoBehaviour or ScriptableObject instance directly from JSON. The reason is they are hybrid C# and native engine objects, and when the JSON package calls
    new
    to make one, it cannot make the native engine portion of the object.

    Instead you must first create the MonoBehaviour using AddComponent<T>() on a GameObject instance, or use ScriptableObject.CreateInstance<T>() to make your SO, then use the appropriate JSON "populate object" call to fill in its public fields.

    If you want to use PlayerPrefs to save your game, it's always better to use a JSON-based wrapper such as this one I forked from a fellow named Brett M Johnson on github:

    https://gist.github.com/kurtdekker/7db0500da01c3eb2a7ac8040198ce7f6

    Do not use the binary formatter/serializer: it is insecure, it cannot be made secure, and it makes debugging very difficult, plus it actually will NOT prevent people from modifying your save data on their computers.

    https://docs.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide
     
  3. Simplisticated_Development

    Simplisticated_Development

    Joined:
    Jun 26, 2023
    Posts:
    61

    Thanks for the reply. So what exactly does the "JSON-based wrapper" save? Does it save strings, floats, ints or scenes or what? And I don't really understand how to implement the "Jason-based wrapper" that you "forked".
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    No, its function is far below that of a save system. It is just to replace PlayerPrefs.

    When you write a
    PlayerPrefs
    entry of any kind, how it lands on the user's computer is dependent on what platform. On Windows it is especially piggish because each entry ends up in the multi-megabytes system-wide Windows Registry, at least as far as I understand it.

    The above JSON-based wrapper makes it "feel like" you're still using PlayerPrefs (go look at the API) but you're actually making and using an object that wraps up all your items and puts them into a Dictionary and writes them out that way.

    The only reason I forked it is so that I can fearlessly paste in references to it without the references being owned by someone else and potentially going away.
     
  5. Simplisticated_Development

    Simplisticated_Development

    Joined:
    Jun 26, 2023
    Posts:
    61
    Okay, good to know!

    I started using a Unity asset store package to achieve save positions. I got stuck while scripting. I need to be able to save the X,Y, and Z in a script. This script doesn't work...can I get some recommendations...thanks.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using BayatGames.SaveGameFree;
    4. using UnityEngine;
    5. using UnityEngine.SocialPlatforms.Impl;
    6.  
    7. public class PositionSaver : MonoBehaviour
    8. {
    9.     private Vector3 V3Pos;
    10.     public int posX = 0;
    11.     public int posY = 0;
    12.     public int posZ = 0;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.        
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         V3Pos = new Vector3(transform.position.x, transform.position.y, transform.position.z);
    24.  
    25.         posX == transform.position.x;
    26.  
    27.         SaveGame.Save("position x", posX);
    28.     }
    29. }
    30.  
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Double equals is comparison.

    Single equal sign is assignment.

    EDIT: you cannot directly assign a float to an int. You must cast it first.

    I'm not sure what purpose the code above serves, just making lots of copies of what is already globally available via
    transform.position
    ...
     
    Last edited: Oct 7, 2023