floatinghotpot / socket.io-unity

socket.io client for Unity, power game client with node.js back-end
494 stars 76 forks source link

get_transform can only be called from the main thread. #10

Open enviel opened 6 years ago

enviel commented 6 years ago

what's the best practice to set something like transform inside On event

i'm not really experienced with multithreading, from the example all i can found is "lock" which i'm not sure what to put inside

        socket.On("pos", (data) =>
        {
            string str = data.ToString();
            Vector3 pos = JsonConvert.DeserializeObject<Vector3>(str);
            remotePlayer.transform.position = pos;
        });

i'm using fpanettieri's before using this, and i can do above script

floatinghotpot commented 6 years ago

Yes, the callback is running in a background thread, so not operate the UI in that.

Instead, put the data in a variable, the use the main thread to read the variable then operate the UI.

See the example: https://github.com/floatinghotpot/socket.io-unity/blob/master/Demo/SocketIOScript.cs#L68

enviel commented 6 years ago

@floatinghotpot can you please update the lib to support the script that i'm doing? just like how this lib work https://github.com/fpanettieri/unity-socket.io-DEPRECATED crating a variable for every event seems unnecessary if i have lots of events it's going to be messy

floatinghotpot commented 6 years ago

I think that is same, the callback is a background thread, you can write log, but you cannot access UI.

enviel commented 6 years ago

@floatinghotpot i'm not accessing UI, i'm accessing player gameobject transform and i can do that in this lib https://github.com/fpanettieri/unity-socket.io-DEPRECATED i can even access UI when using that lib

floatinghotpot commented 6 years ago

But your suggestion is good. If initialize a function in main thread to regularly check the queue then call your callback, it should be okay.

enviel commented 6 years ago

yeah and that should be a core element on the lib can you add that to the lib?

floatinghotpot commented 6 years ago

I think that is not difficult, just add a wrapper. I will try.

enviel commented 6 years ago

@floatinghotpot thankyou so much sir :)