Author Topic: variables for ALL players  (Read 4295 times)

krvc

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 16
    • View Profile
variables for ALL players
« on: September 13, 2016, 03:04:25 AM »
Sorry for my noob question. How i can change variables for all players?
public  int  PlayersNumber;

Void PlayerNumberPlus () {PlayersNumber +=1}

and all my players have PlayersNumber =1 (not 1 2 3......)

 i try to use DataNode, but it not worked too... All users must be admins to write?

        TNManager.SetAdmin("passKey");
        TNManager.SetServerData("PlayersSearchNumber += 40");
        int myInt = TNManager.GetServerData<int>("Assets/TNet/Common/DataNode.cs"); // i really dont know path..... not have examples in scenes
        Debug.Log(myInt);

« Last Edit: September 13, 2016, 11:15:09 AM by krvc »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: variables for ALL players
« Reply #1 on: September 13, 2016, 11:29:13 AM »
You really should go through TNet's tutorials. You're basically asking here about the very first thing covered with TNet: RFCs.
  1. int playerSearchNumber = 0;
  1. tno.Send("SetMyVariable", Target.AllSaved, playerSearchNumber + 40);
  1. [RFC] protected void SetMyVariable (int value)
  2. {
  3.     playerSearchNumber = value;
  4.     Debug.Log("Value should be set to: " + value);
  5. }

krvc

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 16
    • View Profile
Re: variables for ALL players
« Reply #2 on: September 13, 2016, 12:26:10 PM »
 Debug.Log("Value should be set to: " + value); not worked in RFC? - i dont see nothing

change all to:

    int playerSearchNumber;

    void OnClick ()
{
    tno.Send("SetMyVariable", Target.AllSaved, playerSearchNumber += 40);
    Debug.Log(playerSearchNumber);
}

    [RFC] protected void SetMyVariable (int value)
    {
        playerSearchNumber = value;
       
    }

Debug.Log show playerSearchNumber = 40 for 1 client, in other client  playerSearchNumber = 0

cmifwdll

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 149
  • Posts: 285
  • TNet Alchemist
    • View Profile
Re: variables for ALL players
« Reply #3 on: September 13, 2016, 01:17:33 PM »
Debug.Log("Value should be set to: " + value); not worked in RFC? - i dont see nothing

change all to:

    int playerSearchNumber;

    void OnClick ()
{
    tno.Send("SetMyVariable", Target.AllSaved, playerSearchNumber += 40);
    Debug.Log(playerSearchNumber);
}

    [RFC] protected void SetMyVariable (int value)
    {
        playerSearchNumber = value;
       
    }

Debug.Log show playerSearchNumber = 40 for 1 client, in other client  playerSearchNumber = 0

Firstly:
tno.Send("SetMyVariable", Target.AllSaved, playerSearchNumber += 40);
Assigning a value to a variable in a function call seems like it could lead to some unspecified behaviour (though I tested and it actually works, what is this wizardry), however, the assignment itself is redundant since it'll be assigned in the RFC anyway (which is executed on the local client immediately).

Secondly:
Add a Debug.Log("RFC Called"); in the RFC function body to make sure it's even being called.

  1. int playerSearchNumber = -1;
  2.  
  3. void OnClick ()
  4. {
  5.         tno.Send("SetMyVariable", Target.AllSaved, (playerSearchNumber + 40));
  6.         Debug.Log("Sent RFC w/ value: " + playerSearchNumber);
  7. }
  8.  
  9. [RFC]
  10. protected void SetMyVariable (int value)
  11. {
  12.         Debug.Log("Received RFC with value: " + value);
  13.         playerSearchNumber = value;
  14. }
  15.  

edit: Took my own advice ;)

krvc

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 16
    • View Profile
Re: variables for ALL players
« Reply #4 on: September 13, 2016, 04:08:18 PM »
Variables in unity its very easy to use. But in Tnet3 variables is a not easy to use for me.


  int playerSearchNumber = -1;
 tno.Send("SetMyVariable", Target.AllSaved, (playerSearchNumber + 40));

playerSearchNumber + 40???? What is it? my debag log show me -1, i fix to   tno.Send("SetMyVariable", Target.AllSaved, (playerSearchNumber += 40)); when debug show me 39 79... etc every time then i click but in 1 client not all. (1 client is  unity editor 2 client bilded game) i click, click, click in bilded game to much, then i click in unity editor, he show me 39 79... not more...

my Game have Players. Players click Start - Game started. Players have Numbers. Player1 have Number 1, Player 2 Number 2, etc.....

cmifwdll

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 149
  • Posts: 285
  • TNet Alchemist
    • View Profile
Re: variables for ALL players
« Reply #5 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.

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:
  1. int playerSearchNumber = -1;
  2.  
  3. void OnClick ()
  4. {
  5.         if (tno == null)
  6.         {
  7.                 Debug.LogError("Missing TNObject component on gameobject");
  8.                 return;
  9.         }
  10.         if (!TNManager.isConnected)
  11.         {
  12.                 Debug.LogError("Not connected to a game server");
  13.                 return;
  14.         }
  15.         if (!tno.isMine)
  16.         {
  17.                 Debug.LogWarning("TNObject does not belong to local player");
  18.         }
  19.         tno.Send("SetMyVariable", Target.AllSaved, (playerSearchNumber + 40));
  20.         Debug.Log("Sent RFC w/ value: " + playerSearchNumber + " (note: should be -1)");
  21. }
  22.  
  23. [RFC]
  24. protected void SetMyVariable (int val)
  25. {
  26.         Debug.Log("Received RFC w/ value: " + val + "(note: should be 39)");
  27.         playerSearchNumber = val;
  28. }
  29.  

edit: added link, fixed formatting, fixed usage of reserved keyword
« Last Edit: September 13, 2016, 05:14:56 PM by cmifwdll »

krvc

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 16
    • View Profile
Re: variables for ALL players
« Reply #6 on: September 13, 2016, 05:46:07 PM »
TNet comes packaged with an example scene. Load up this scene and see how everything works first.
I not search in TNet scene  how to use int, string, variables in multiplayer.

I read tutorials here http://www.tasharen.com/tnet/docs/
search "int" - nothing
search "variables" - nothing


RANDOM COPY-COPY-DELETE-DELETE-COPY-COPY FILES
and now its WORKS! i dont know what is problem, but now it realy work

i think in TNAutoJoin.cs i change public int channelID = 1; to public int channelID = 0;
« Last Edit: September 13, 2016, 07:12:16 PM by krvc »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: variables for ALL players
« Reply #7 on: September 14, 2016, 06:42:59 AM »
TNet's tutorials are included as PDFs, and are also found here in a sticky post at the top of this forum.

http://www.tasharen.com/forum/index.php?topic=13953.0