196
TNet 3 Support / Re: variables for ALL players
« on: September 13, 2016, 05:07:30 PM »
If it shows as -1 then it means the RFC is not being executed locally or across the network.
Are you connected to a game server? Is there a TNObject component on the gameobject?
Go through the official tutorials here to gain a better understanding. Additionally, TNet comes packaged with an example scene. Load up this scene and see how everything works first.
So, tno.Send("SetMyVariable", Target.AllSaved, (playerSearchNumber+40)). Breaking it down, this is a function call. The function being called is Send belonging to the class instance tno. The first parameter is a string, the second parameter is an enum, and the third parameter is an object (in this case it'll be serialized to an integer).
(playerSearchNumber + 40) is the same as writing(-1 + 40) and passes 39 as the third parameter. I only left it in because that's what Aren wrote above.
playerSearchNumber += 40 is adding 40 and then *assigning* the result back to the variable which is why it shows up on the local client. It's the same as writing (playerSearchNumber = playerSearchNumber + 40;) and variable assignments (the '=' operator) probably shouldn't be done inside function calls (tno.Send(...)).
This is all basic programming and not specific to TNet.
This code should output an appropriate message:
edit: added link, fixed formatting, fixed usage of reserved keyword
Are you connected to a game server? Is there a TNObject component on the gameobject?
Go through the official tutorials here to gain a better understanding. Additionally, TNet comes packaged with an example scene. Load up this scene and see how everything works first.
playerSearchNumber + 40?What is it?
So, tno.Send("SetMyVariable", Target.AllSaved, (playerSearchNumber+40)). Breaking it down, this is a function call. The function being called is Send belonging to the class instance tno. The first parameter is a string, the second parameter is an enum, and the third parameter is an object (in this case it'll be serialized to an integer).
(playerSearchNumber + 40) is the same as writing(-1 + 40) and passes 39 as the third parameter. I only left it in because that's what Aren wrote above.
playerSearchNumber += 40 is adding 40 and then *assigning* the result back to the variable which is why it shows up on the local client. It's the same as writing (playerSearchNumber = playerSearchNumber + 40;) and variable assignments (the '=' operator) probably shouldn't be done inside function calls (tno.Send(...)).
This is all basic programming and not specific to TNet.
This code should output an appropriate message:
- int playerSearchNumber = -1;
- void OnClick ()
- {
- if (tno == null)
- {
- Debug.LogError("Missing TNObject component on gameobject");
- return;
- }
- if (!TNManager.isConnected)
- {
- Debug.LogError("Not connected to a game server");
- return;
- }
- if (!tno.isMine)
- {
- Debug.LogWarning("TNObject does not belong to local player");
- }
- tno.Send("SetMyVariable", Target.AllSaved, (playerSearchNumber + 40));
- Debug.Log("Sent RFC w/ value: " + playerSearchNumber + " (note: should be -1)");
- }
- [RFC]
- protected void SetMyVariable (int val)
- {
- Debug.Log("Received RFC w/ value: " + val + "(note: should be 39)");
- playerSearchNumber = val;
- }
edit: added link, fixed formatting, fixed usage of reserved keyword