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 Initialize ScriptableSingleton<T0>

Discussion in 'Scripting' started by Hazneliel, Oct 1, 2022.

  1. Hazneliel

    Hazneliel

    Joined:
    Nov 14, 2013
    Posts:
    303
    Hello

    Anybody knows what is the way to initialize ScriptableSingleton<T0>?

    It seems the instance is already initialized when I call it but I want to overwrite to init my own variables.

    Any help is really appreciated.

    Regards
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    Did you look in the documentation? It appears to indicate that these things receive certain callbacks not unlike those sent to MonoBehaviours.
     
  3. Hazneliel

    Hazneliel

    Joined:
    Nov 14, 2013
    Posts:
    303
    I did look thanks, it says instance is created automatically the first time you access that property so I want to do some initialization when that happens.
    You can override the Awake method but that one never gets called I guess because the ScriptableObject is not actually created in any scene
     
  4. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    326
    ScriptableObjects don't have Awake/Start methods. They do have OnEnable/Disable. Check the documentation per @Kurt-Dekker suggestion.
     
  5. Hazneliel

    Hazneliel

    Joined:
    Nov 14, 2013
    Posts:
    303
    Last edited: Oct 2, 2022
  6. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,114
    So Awake does get called when ScriptableSingleton<T>.instance is accessed for the first time and the instance is created. However, this does not get called again each time you exit and enter play mode again; the instance remains loaded in memory even if you exit play mode. You have to restart the Editor before Awake triggers again.

    If you need to re-run some initialization code each time when entering play mode you can subscribe to EditorApplication.playModeStateChanged in the Awake function.
     
    orionsyndrome likes this.
  7. Hazneliel

    Hazneliel

    Joined:
    Nov 14, 2013
    Posts:
    303
    Thanks, I will try this
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    In my experience, since about Unity2019 does reliably call OnEnable/OnDisable at start/stop of editor/play, which definitely is NOT the case in older Unity.

    Nowadays I just do stuff on OnEnable
     
    orionsyndrome and Hazneliel like this.
  9. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,114
    Could be that it works differently based on how a ScriptableObject instance is being initialized - or even based on one's Enter Play Mode Options.

    I did a quick test and at least in Unity 2020.3 with my project calling ScriptableSingleton<T>.instance did not result in Awake being executed again when entering play mode a second time.
     
    orionsyndrome likes this.
  10. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    326
    I stand corrected. Thank you for pointing this out. I never used Awake on SO, and very rarely OnEnable/Disable, I prefer to have an Init() method that I call when needed. Also I never disabled/destroyed an SO during runtime.
     
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    On a few games we implemented an
    IInitializable
    interface on all of our ScriptableObjects and then had a script that essentially listed them in the order necessary.

    The
    IInitializable
    interface we made supported a call-on-finished delegate, which just naturally chained onto the next items init, and we had a little "init runner" in our first scene.

    It proved remarkably robust, because you could make sub-chains if you wanted, such as start up systems A,B,C in linear step-after-step order, but then simultaneously kick off D, E and F because none of them were dependent on each other after the C task completed.

    And they could be fairly easily reordered, as they were just supplied to runner objects via a list constructor.
     
    SisusCo and Nad_B like this.
  12. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    326
    Similar to the "Bootstrapper" class I'm using on my current project, it lists all services/systems/data SOs via reflection + attributes, and initialize them in the necessary order. Like you said it's super robust, and it works very well for me since I'm more of a programmer than a game designer, I rarely use the Unity Editor/Hierarchy (my main scene is just a floor, a camera and three empty GameObjects containing basic systems and the Bootstrapper).
     
    Kurt-Dekker likes this.
  13. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    This resonates with me... my first X games using Unity were basically an empty scene with a single GameObject called
    0scripts
    (because back then the hierarchy was alphabetical, putting that at the top) and a single script (or two) on that GameObject.

    And that script did everything, such as loading and creating other scripts that did other scripts, etc.

    Obviously as a professional Unity developer today I no longer work that way, as I do more in-Editor work than actual coding most days, as my team needs that level of support granularity from engineers.

    One of the handy bridges that got me to full editor usage was to start making lots of small additively loaded scenes. Check it out:

    Additive scene loading notes:

    https://forum.unity.com/threads/right-way-for-performance-divide-scene.1023673/#post-6630961
    https://forum.unity.com/threads/right-way-for-performance-divide-scene.1023673/#post-6754330

    https://forum.unity.com/threads/problem-with-canvas-ui-prefabs.1039075/#post-6726169

    A multi-scene loader thingy:

    https://pastebin.com/Vecczt5Q

    My typical Scene Loader:

    https://gist.github.com/kurtdekker/862da3bc22ee13aff61a7606ece6fdd3

    Other notes on additive scene loading:

    https://forum.unity.com/threads/removing-duplicates-on-load-scene.956568/#post-6233406

    Timing of scene loading:

    https://forum.unity.com/threads/fun...ject-in-the-second-scene.993141/#post-6449718

    Also, if something exists only in one scene, DO NOT MAKE A PREFAB out of it. It's a waste of time and needlessly splits your work between two files, the prefab and the scene, leading to many possible errors and edge cases.

    Two similar examples of checking if everything is ready to go:

    https://forum.unity.com/threads/daily-events-and-content-changes.1108202/#post-7143713

    https://forum.unity.com/threads/uni...on-before-other-scripts.1153739/#post-7401794
     
    Nad_B likes this.
  14. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    326
    Thank you for the suggestions about Additive scenes, I knew it existed and how it basically works, but never thought about using them as an architectural element. Will definitely check the links you provided.

    I know how that the Editor is super useful, and kind of necessary for a lot of game genres (mainly games with different levels/maps, which represents the vast majority of games), but the actual project I'm working on has nearly no use for the Editor, as it's a builder/tycoon type, with random generated maps, random characters, lot of data (buildings, professions, game items, AI behaviors/types...), lot of UI/Widows/Dialogs (using Noesis/Xaml/Databinding) and most important: a lot of economy... aside from setting game data (SO) properties on the editor, I have no real use for it.

    Not needing to use the Editor is probably one of the main reasons I chose this game genre (beside loving it of course), and also because I'm really bad in graphic design :D (thankfully Tycoon fans are a lot more forgiving on graphics quality than other genres)
     
    Last edited: Oct 12, 2022