SS3D icon indicating copy to clipboard operation
SS3D copied to clipboard

InputSystem in OnStart and OnDestroyed

Open iamteapot422 opened this issue 1 year ago • 4 comments

InputSystem subscriptions should be in OnEnable and unsubscribes should be in OnDisabled in all files, so input actions won't trigger disabled scripts. Currently this causes null reference erorrs, because OnEnables in subscribers are called before OnAwake in InputSystem. Figure out how to fix this without changing the code execution order.

iamteapot422 avatar Jul 02 '23 12:07 iamteapot422

@iamteapot422 does it needs to be in all files ? Only those dealing with controls I assume ? It'd be useful to have a bit more information on what causes the null ref errors too.

stilnat avatar Sep 04 '23 09:09 stilnat

@stilnat yeah, its needed only where subscriptions to inputs are present. Null ref is caused because subscriptions in OnEnable are used before InputSystem initialization

iamteapot422 avatar Sep 04 '23 13:09 iamteapot422

@iamteapot422 Ok thanks, well a possible solution I have in mind would be to have an Interface, IInputRegistering, with at least 3 methods :

  • HandleInputSystemReady() -> Suscribe the following method to an event fired by the input system when it's ready, called in OnEnable. Also check a bool on InputSystem to see if it's already ready, because we might want to suscribe in the middle of the game and the event will fire only once.
  • SuscribeToInputs() -> triggered by events on Input system when ready. Suscribes inputs.
  • UnsuscribeFromInputs -> Should be called in OnDisable of the script. Unsuscribe inputs.

Not perfect but gives some structure on the way to register inputs, and an interface where we can write the doc.

stilnat avatar Sep 04 '23 14:09 stilnat

@stilnat the real problem is that SystemLocator registers InputSystem later the OnEnables are called

iamteapot422 avatar Sep 04 '23 16:09 iamteapot422