question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] FSM not compatible with disabled domain reload

See original GitHub issue

Describe the bug Variables use OnEnable to reset themselves

However, Unity does not call OnEnable on scriptable objects when entering playmode IFF:

  • the ScriptableObject is already loaded
  • domain reload on play is disabled

To Reproduce Steps to reproduce the behavior:

  1. Disable Domain Reload
  2. Create any variable
  3. Change the variable value during play mode
  4. Value will be preserved on next play mode

Expected behavior Variables should reset to initial value for every play mode sessions

Additional context My current hack

using UnityAtoms;
using UnityEditor;
using UnityEngine;

namespace Atoms.Editor.EnsureVarReset
{
  // ensure class initializer is called whenever scripts recompile
  [InitializeOnLoad]
  public static class EnsureVarReset
  {
    // register an event handler when the class is initialized
    static EnsureVarReset()
    {
      // Whenever the game enters or leaves playmode
      EditorApplication.playModeStateChanged += LogPlayModeState;
    }

    static void LogPlayModeState(PlayModeStateChange state)
    {
      // Reset each loaded variable
      foreach (AtomBaseVariable variable in ScriptableObject.FindObjectsOfType<AtomBaseVariable>())
      {
        // Reset the var
        variable.Reset();
      }
    }
  }
}

Issue Analytics

  • State:closed
  • Created 4 months ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
soraphiscommented, Jul 25, 2023

Not necessarily a warning, just enough any diagnostic logs

if you wanna elaborate on that topic or bring up implementation ideas that could solve that without harming performance or other implementations, feel free to do so, But I’d ask you, to do so in #323 (feel free to re-open the ticket if necessary!)

1reaction
soraphiscommented, Jul 24, 2023

what is considered “incorrect state”? that’s business logic, only you can decide about that and you could write an validator for that.

modifying the FSM implementation to throw errors in that case, would force you to setup every possible transition.

let’s say you feed your keyboard input as actions into your FSM to handle your game state WORLD —ESC—> MENU WORLD — I —> INVENTORY …

that way you only handle transitions and every key that is not a transition is a “stay in state”. works perfectly fine, but does not work anymore (or would spam warnings) if #323 would be implemented.

the issue in this case is less that the FSM does not throw warnings, the issue is more that the FSM was able to be in an invalid starting state that was seemingly not visible in the inspector (as you had to resolve to the debug inspector)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bug - Addressables.LoadSceneAsync Breaks with Domain ...
When I try to load a scene via the addressables system it loads correctly the first time, but when I run play mode...
Read more >
Support disabling domain reloading · Issue #1518
Unity 2019.3 has an experimental feature that disables domain reloading when entering play mode. This skips (re)initialising game state, ...
Read more >
Instantly Enter PlayMode Unity 2019.3 for ...
But it seems playmaker is not correctly working yet when I disable Domain reload? Every Fsm stays on first state.(I can forced to...
Read more >
Manual: Domain Reloading
Disabling Domain Reloading​​ To disable Domain Reloading: Go to Edit > Project Settings > Editor. Make sure Enter Play Mode Options is enabled....
Read more >
Using Reflection To Help Disable Domain Reload
When domain reload is disabled, the method ResetStatic() will be invoked by Unity before running the game. You would want your static variables ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found