InputSystem icon indicating copy to clipboard operation
InputSystem copied to clipboard

InputSystem.devices.count returns 0 in MonoBehavior.Start method

Open todi1856 opened this issue 8 years ago • 13 comments

When application starts, InputSystem.devices will contain zero devices, if you would query it from Start method. The devices are populated later...

Can this be fixed somehow? Like device creation moved before any script's Start or even Awake method?

todi1856 avatar Apr 26 '18 11:04 todi1856

Hmm, bit of a tricky one. Problem is that devices are sent as part of updates -- which happen in the player loop. So with the first update happening after Start(), managed won't see any data coming in before.

What we probably want is to at least do one initial push of device discoveries before Start() and then let the system run from them on the way it does now.

Rene-Damm avatar Apr 26 '18 18:04 Rene-Damm

I should add this is inconsistent with Editor behavior, in Editor if you query InputSystem.devices in Start you get a list of devices, while in player you get zero devices.

todi1856 avatar May 04 '18 09:05 todi1856

In Start() in an ExecuteInEditMode MonoBehaviour?

Rene-Damm avatar May 04 '18 17:05 Rene-Damm

No, in simple Start method without any attributes, when you click Play in Editor.

todi1856 avatar May 04 '18 19:05 todi1856

Ah right. That makes sense. In the editor, the input system would get initialized on startup by the first edit mode update and by the time you go into play mode, it's already working off a fully initialized state that is simply persisted between domain reloads.

Rene-Damm avatar May 04 '18 20:05 Rene-Damm

So I assume you agree this should be fixed then ? :)

todi1856 avatar May 07 '18 08:05 todi1856

Yeah I do. Doug is looking into it.

Rene-Damm avatar May 07 '18 14:05 Rene-Damm

status update for everyone. I was having difficulty finding a way to push devices after RuntimeInitializeOnLoadMethod but before start(). None of the global callbacks worked. I asked around, and there is a PR in flight that will give me the exact callback we need. https://ono.unity3d.com/unity/unity/changeset/bf2841236b9a303f26697bf8ed37b6ebcdcab871] Eneko says this will trigger after RuntimeInitializeOnLoadMethod and before start(). I'm going to test out the branch and see if it gives us what we want.

douglas-piconi avatar May 15 '18 13:05 douglas-piconi

Before Start and Awake?

todi1856 avatar May 15 '18 13:05 todi1856

I'll test. I think so, but will test to make sure.

douglas-piconi avatar May 15 '18 13:05 douglas-piconi

@piconid Will we get that branch for .2, though?

Are you sure that none of the global callbacks are suitable? Looking around, one thing that seems like it should work is using sceneLoadedAfterAwake. ATM InputSystem initialization in the player will run in sceneLoadedBeforeAwake. Pushing device discoveries in sceneLoadedAfterAwake should make it so devices are pushed out after Awake() is called on all objects. That way the devices should be available in Start(). It'd get called on every first-scene load, but IMO that's just fine. If there's no new devices to push out, it'll do nothing.

Rene-Damm avatar May 15 '18 16:05 Rene-Damm

Fix in flight, PR under review to trunk. Will also backport to 2018.2

douglas-piconi avatar May 15 '18 19:05 douglas-piconi

Fixed on managed side. Took me a while to realize that we have everything we need on the managed side and don't need any supporting native changes. Was a one line fix in the end.

Rene-Damm avatar May 17 '18 21:05 Rene-Damm