Search Unity

Scriptable Objects in My Text Adventure Don't Revert After Changes in Play Mode

Discussion in 'Scripting' started by samdaman93, Mar 20, 2021.

  1. samdaman93

    samdaman93

    Joined:
    Oct 3, 2015
    Posts:
    37
    Hello,

    I'm pretty basic at unity, I want to make a text adventure to start and loosely followed the below tutorial series.



    I've changed the setup though so that I can click buttons to progress between rooms not type "go north" etc. That works fine, I can go back and forth between rooms, but I'm stuck on my next problem. I want to have different prompts when I return to a room or I want the "exits" in my scriptable object to change as the story progresses. So I added a function that sets the room text to change when you re-enter the room. This way in the inspector I can write what happens when you first go to a room and then a different description when you go back. This overwrites the initial description in the scriptable object and the boolean changes as well so when you next play its like you've already visited the room.

    Is this just how scriptable objects work in unity? Feels like a recipe for disaster.

    Is there a better way to handle all this data as well? Like a database that my game controller script could draw from instead of having scriptable objects for the rooms and the exits and having all the data in there? Would be good to setup a spreadsheet of sorts for all the items and decision trees that would effect all the options that you could choose from rather than creating copies of the scriptable objects, especially if they're just going to get overwritten at play time.

    Let me know if you want me to post something!

    Cheers,
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    Worse:

    It's how ScriptableObjects work only in the editor.

    I prefer to treat ScriptableObjects as a static database of sorts. If you have data that you want to create and change at runtime, just make some plain old C# classes and objects.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Yes, this is precisely by design. This is how you write Editor scripts that do interesting things to preset data in your game.

    To proceed successfully with RUNTIME use of your ScriptableObject approach, I recommend a step where you actually make a copy each ScriptableObject at the beginning of the play of a game. This would make temporary in-memory-only copies of the predefined ScriptableObjects for you to use during this game run, and when you press STOP, all of those things are gone and you're back to defaults.

    To copy a ScriptableObject, use Instantiate:

    Code (csharp):
    1. var MyRuntimeCopy = Instantiate<MyScriptableObject( OriginalScriptableObjectAssetOnDisk);
    And then your runtime code would only use MyRuntimeCopy, never touching the original.

    ALTERNATELY, you can make private fields in those ScriptableObjects and mark them with
    [NonSerialized]
    and then your data will not persist from run to run, but you would have to copy the predefined data over to those private temporary datas. This may get very messy.