Search Unity

Scriptable Objects Unity video question

Discussion in 'Editor & General Support' started by roseportalgames, Aug 24, 2021.

  1. roseportalgames

    roseportalgames

    Joined:
    Aug 9, 2017
    Posts:
    173
    Hi everyone,

    I have a quick question about this video from Unity:



    So in it they have a Deck ScriptableObject with a currentCard integer. They change it in a function in the Scriptable Object class.

    But changing this value would be "remembered" in the editor and in future playthroughs, right?
    So isn't it bad to have this value carry over?
    Because then the next time you play the game, the current card will not be the first card, unless you initialize it and set it to 0 again. But then why not just put it in a MonoBehaviour class anyway?

    I've used Scriptable Objects a LOT but I still don't know a good use case for adding functions to them and for changing the values within the Scriptable Object like they do in the video.
     
  2. No. Only in editor. SOs reset between sessions in build. This is why many people use SOs as configs (as opposed to settings which should be remain after changing runtime).

    Depends. If it is something tied to the current scene, then a MonoBehaviour is better. But keeping MBs around and make sure you always have it when it is needed is complex and prone to error.
    Sometimes a game-wide place is better which doesn't get unloaded when you switch scenes.

    It's a matter of taste, up to you.

    This is the ultimate SO video:
     
    roseportalgames likes this.
  3. roseportalgames

    roseportalgames

    Joined:
    Aug 9, 2017
    Posts:
    173
    Wait, you're saying that if an SO is changed by a script in a playthrough, that value is not "remembered"?
    But if I change it directly in the Editor, it's "remembered"?

    That's so strange. I thought a few years ago when I first started working with Scriptable Objects, I made one for an enemy. And then I also put "current HP" in it, next to "max HP". Then I changed "current HP" when it received damage. And I thought after I stopped the Playing, the current HP was still at that lower value. Since then I have never used them in the way of changing values on playthrough.

    If they actually reset somehow when you stop Playing that would be pretty cool, and then it would make sense to keep track of the current card id as it would go back to 0 anyway after the Play stops.

    But I'm curious how this data class "remembers" the original value? Does it keep an original instance of it somewhere in memory? Or a duplicate of each value at start in memory?

    Will watch the video, thanks for the link. Lol Firefly
     
  4. No. If you change a value in the Editor (regardless if it is a play-through or editor action), it will be saved and serialized. If you change it in a build, the value won't be saved (since Unity serializes nothing in build).
    Serialization. It changes the value in editor no matter if it is play mode or edit mode, then when you build the game it gets serialized and saved in a special binary file (resources - not the folder, but similar). When the game starts, that file gets read into memory and gets used. When you exit, the memory will be released without saving it onto disk. When you run it again, the original value gets read back from the resource file.
     
    roseportalgames likes this.
  5. roseportalgames

    roseportalgames

    Joined:
    Aug 9, 2017
    Posts:
    173
    Ah, thanks! Doesn't that make it difficult to play test the game in the unity editor if changing values is permanent? You'd have to change them back each time.