Forum Games InvalidOperationException When Authenticating From Editor

Discussion relating to game development with Unity

InvalidOperationException When Authenticating From Editor

Postby hyperion51 » August 31st, 2017, 8:24 am

Greetings,

I'm in the process of migrating a game to PlayerIO hosted multiplayer. To that effect, things previously stored in ScriptableObjects need to be moved onto BigDB. To do that, I added an button to the ScriptableObject's inspector which is meant to store the SO's values in BigDB, and an editor extension to authenticate and provide that Client instance to other editor scripts.

It seems this use case was not considered, though, since authenticating from an editor script causes the following exception:

Code: Select all
InvalidOperationException: The following game object is invoking the DontDestroyOnLoad method: PlayerIO-b1f207b6edc84cd8ac6e72876efc1d75. Notice that DontDestroyOnLoad can only be used in play mode and, as such, cannot be part of an editor script.
__PlayerIO_UnityInterop.Get () (at <845ea6bf4ddb4435a4783693c560680c>:0)
PlayerIOClient.Internal.identifier1036.Get () (at <845ea6bf4ddb4435a4783693c560680c>:0)
PlayerIOClient.Internal.identifier49.startUnityMessagePump () (at <845ea6bf4ddb4435a4783693c560680c>:0)
PlayerIOClient.Internal.identifier49.Enqueue (System.Collections.IEnumerator enumerator) (at <845ea6bf4ddb4435a4783693c560680c>:0)
PlayerIOClient.Internal.identifier126+identifier828.Call[A,O,E] (System.Int32 method, A args, PlayerIOClient.Callback`1[T] success, PlayerIOClient.Callback`1[T] error) (at <845ea6bf4ddb4435a4783693c560680c>:0)
PlayerIOClient.PlayerIO+ChannelMonitor.Call[A,O,E] (System.Int32 methodId, A args, PlayerIOClient.Callback`1[T] success, PlayerIOClient.Callback`1[T] error) (at <845ea6bf4ddb4435a4783693c560680c>:0)
PlayerIOClient.Internal.identifier126.Authenticate (System.String gameId, System.String connectionId, System.Collections.Generic.Dictionary`2[TKey,TValue] authenticationArguments, System.Collections.Generic.List`1[T] playerInsightSegments, System.String clientAPI, System.Collections.Generic.Dictionary`2[TKey,TValue] clientInfo, System.Collections.Generic.List`1[T] playCodes, PlayerIOClient.Callback`1[T] onSuccess, PlayerIOClient.Callback`1[T] onError) (at <845ea6bf4ddb4435a4783693c560680c>:0)
PlayerIOClient.PlayerIO+<>c__DisplayClass13.<Authenticate>b__f () (at <845ea6bf4ddb4435a4783693c560680c>:0)
PlayerIOClient.PlayerIO.ensureInitialized (PlayerIOClient.Callback callback) (at <845ea6bf4ddb4435a4783693c560680c>:0)
PlayerIOClient.PlayerIO.Authenticate (System.String gameId, System.String connectionId, System.Collections.Generic.Dictionary`2[TKey,TValue] authenticationArguments, System.String[] playerInsightSegments, PlayerIOClient.Callback`1[T] successCallback, PlayerIOClient.Callback`1[T] errorCallback) (at <845ea6bf4ddb4435a4783693c560680c>:0)
PlayerIOWindow.OnGUI () (at Assets/Scripts/Editor/PlayerIOWindow.cs:29)
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:305)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:313)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at /Users/builduser/buildslave/mono/build/mcs/class/referencesource/mscorlib/system/reflection/methodbase.cs:229)
UnityEditor.HostView.Invoke (System.String methodName, System.Object obj) (at C:/buildslave/unity/build/Editor/Mono/HostView.cs:272)
UnityEditor.HostView.Invoke (System.String methodName) (at C:/buildslave/unity/build/Editor/Mono/HostView.cs:265)
UnityEditor.HostView.OnGUI () (at C:/buildslave/unity/build/Editor/Mono/HostView.cs:94)


This could be fixed rather trivially on your end by checking Application.isEditor before calling DontDestroyOnLoad.
hyperion51
 
Posts: 17
Joined: June 29th, 2017, 3:52 pm

Re: InvalidOperationException When Authenticating From Edito

Postby hyperion51 » August 31st, 2017, 8:36 am

Just realized that PlayerIO-b1f207b6edc84cd8ac6e72876efc1d75 was in fact an object which was added to the Unity scene. This leads me to believe that calling DontDestroyOnLoad is merely one of many things which PlayerIO does that don't make sense in an Editor script.

Next thing to try is the regular C# SDK, I guess...

Update: Seems like it should work, but causes a namespace collision. Unity can compile it as long as the import settings are set to exclude the editor on PlayerIOUnity3DClient and standalone on PlayerIOClient, but my IDE (Rider, though I'm sure VS would have the same issue) doesn't know which PlayerIOClient namespace to use. No idea how to fix that.

Update 2: What does PlayerIOUnity3DClient even do that's Unity-specific?
hyperion51
 
Posts: 17
Joined: June 29th, 2017, 3:52 pm

Re: InvalidOperationException When Authenticating From Edito

Postby nev » January 29th, 2018, 11:39 pm

I recall doing something similar when synchronizing my world settings (set in the Unity Editor) with BigDB.

There were some threads about it way way back. If we are indeed having the same issue, the solution I got was to do it while in 'Play' mode. For some reason the connection object would not run/pulse/beat when paused or when in edit mode. I just setup a separate scene with all the objects I needed to synch, hit play and pressed my master synch button. The connection went through and updated BigDB. A dirty fix but it works.

I noticed there were some editor functions that you could call to step through frames while just in the editor, but it wasn't enough to get it working reliably. So doing any connection with PlayerIO needs to happen when your game is playing (and frames are processed).

I too wanted to use straight C# to make the connection without all the hassle. but I remember facing some other big hurdle. I think it was trying to run the C# SDK from the editor or something. I didn't think it was worth the extra time needed to implement a cleaner fix.

Let us know if going into play mode is the solution. More importantly, tell me if you find a solution that doesn't require extra steps and therefore more chance to break/corrupt something in BigDB when not done properly by non-coders.
nev
 
Posts: 4
Joined: April 24th, 2013, 8:39 pm

Re: InvalidOperationException When Authenticating From Edito

Postby hyperion51 » January 30th, 2018, 12:05 am

Everything's running smoothly now, I'm using the non-Unity version, and it runs fine both inside my editor tools and in play mode. I have the same use case as you, uploading world data to BigDB.

I have a pretty sweet static shim simplifying my netcode, does both async and main thread dispatching of message listeners without bringing MonoBehavior or an event queue into it, and I have a mini-serializer that'll convert any class into a DatabaseObject automatically. Lemme know if you want it.
hyperion51
 
Posts: 17
Joined: June 29th, 2017, 3:52 pm

Re: InvalidOperationException When Authenticating From Edito

Postby nev » January 31st, 2018, 7:28 am

Wow that's awesome! If you don't mind posting a snippet of how you connect using the .NET client that would be super helpful for anyone else hitting this problem.

How did you overcome the namespace conflict? Are you still running it from the editor, and if so how?

I do serialization manually and pack each bit as tight as I can. I have a dynamic map and need to send a lot of that info back and forth, so I think it's worth the processing trad-off to pack/unpack. If I didn't need to do that, I would definitely love to have a well written auto serialiser and BigDB storage routine.
nev
 
Posts: 4
Joined: April 24th, 2013, 8:39 pm

Re: InvalidOperationException When Authenticating From Edito

Postby hyperion51 » January 31st, 2018, 5:48 pm

There's no more namespace conflict since I am now only using the .NET client, which works fine both in and out of play mode. I'll be posting my shim along with a sample project very soon.
hyperion51
 
Posts: 17
Joined: June 29th, 2017, 3:52 pm

Re: InvalidOperationException When Authenticating From Edito

Postby nev » February 2nd, 2018, 12:04 am

OK I get it, just use the .NET client and not the Unity client for all PlayerIO related code. You would still have to use an actual Monobehaviour derived class to call the appropriate functions from the shim (in Start and/or Update). A bit more work but much cleaner and avoids the issue mentioned at the start of the thread.
nev
 
Posts: 4
Joined: April 24th, 2013, 8:39 pm


Return to Games