XRTK / com.xrtk.core

The Official Mixed Reality Framework for Unity
https://xrtk.io
MIT License
307 stars 34 forks source link

Need a check on "TryGetService" when it is called from Awake, to avoid confusion by trying to get services too early #867

Closed SimonDarksideJ closed 3 years ago

SimonDarksideJ commented 3 years ago

XRTK - Mixed Reality Toolkit Feature Request

Is your feature request related to a problem? Please describe

Describe the solution you'd like

If TryGetService is called external from the XRTK, before the toolkit is initialised, it should throw a "NotInitialised" error with a helpful message, such as "The XRTK has not yet initialised, are you attempting to call it during awake?"

Describe alternatives you've considered

User rolling around in confusion as to why their script does not work.

StephenHodgson commented 3 years ago

I suspect TryGet should just return false (as the name implies it could fail to return a valid instance of a service). Adding the errors could increase the log spam in unintended ways, even though it is a valid check.

Also you can wait until the xrtk is initialized (we have examples in several monobehaviour handlers like the base input handler, or the base controller pointer, where we wait until the xrtk is initialized or when a system/service is initialized.

We can also encourage people to just simply check if the xrtk has been initialized (As there is a simple assert method on the type)

MixedRealityToolkit.AssertIsInitialized()

Probably the best method to use here is GetSystemAsync<IMixedRealitySystem>() or GetServiceAsync<IMixedRealityService>() which will properly wait until the xrtk is initialized and will pass back the service once it's ready to use.