Author Topic: How can I associate the TNPlayer class with my actual game objects?  (Read 2622 times)

gg67

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 66
    • View Profile
I'm having trouble understanding how the TNPlayer class is connected to game objects created based on each networked player. In my mind I would have the TNet Player script as a component of each player game object I create, however that's not how it was designed.

I suppose the problem I'm trying to solve is finding a remote player game object based on a TNPlayer object.

Thanks!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How can I associate the TNPlayer class with my actual game objects?
« Reply #1 on: December 27, 2013, 04:24:23 PM »
In the Start() function of your script attached to the same object as TNObject, check tno.isMine (if you derived it from TNBehaviour). This will tell you whether you own this object or not.

gg67

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 66
    • View Profile
Re: How can I associate the TNPlayer class with my actual game objects?
« Reply #2 on: December 27, 2013, 04:41:03 PM »
I'm not sure I asked my question properly. Let me explain my problem in a little bit more detail.

When a new player joins, I want them to send everybody else their username. Right now, I have this:

  1.         // Enable and disable player components based on whether they are *my* player, or the networked player clones
  2.         void setupNetworkedPlayer() {
  3.                 // If this is my player...
  4.                 if(tno.isMine) {
  5.  
  6.                         // Add username
  7.                         mPlayerName = TNManager.playerName;
  8.             // Tell everybody else to add my name to my clones in their game
  9.                         tno.Send(2,Target.OthersSaved, TNManager.player, mPlayerName);
  10.                         // Enable player controls
  11.                         instance.GetComponent<SimpleMove>().enabled = true;
  12.                         // Enable camera and audio
  13.                         instance.transform.FindChild("Camera").gameObject.SetActive(true);
  14.                         // Enable mouselook on capsule
  15.                         instance.GetComponent<MouseLook>().enabled = true;
  16.                 }
  17.  
  18.         }
  19.  
  20.     // This is where I'm having issues
  21.         [RFC(2)]
  22.         void setJoinedPlayerName(Player p, string name) {
  23.         // Find player p's gameObject, and set the mPlayerName variable in this script to the passed name
  24.                 mPlayerName = name;
  25.         }
  26.  

In the RFC, I'm not sure how to connect the Player p to the cloned game object player on everybody else's clients.

Another option in my mind would have the each TNPlayer "own" a list of TNObjects. Thus, as player1 I could find all objects belonging to player2, player3, etc. Though I don't want to go hack up your scripts because I'd imagine theres a better way.
« Last Edit: December 27, 2013, 06:44:42 PM by gg67 »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How can I associate the TNPlayer class with my actual game objects?
« Reply #3 on: December 28, 2013, 02:13:41 AM »
  1. void Start()
  2. {
  3.     if (tno.isMine) tno.Send("SetMyName", Target.OthersSaved, gameObject.name);
  4. }
  5.  
  6. [RFC]
  7. void SetMyName (string name)
  8. {
  9.     gameObject.name = name;
  10. }
That's for setting some local value.

If you really are trying to set player names... don't do it. TNManager.playerName is an auto-sync'd call. If you change it, everyone will automatically get the change in their TNManager.players.
« Last Edit: December 28, 2013, 05:10:33 PM by ArenMook »

gg67

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 66
    • View Profile
Re: How can I associate the TNPlayer class with my actual game objects?
« Reply #4 on: December 28, 2013, 02:44:26 AM »
Sweet! Thanks for clearing that up, I'm still wrapping my head around custom RFCs. Also, I had my RFC call in Awake() instead of Start(). I've read up on the differences, but why did that cause issues here?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How can I associate the TNPlayer class with my actual game objects?
« Reply #5 on: December 28, 2013, 05:11:41 PM »
Awake() is executed as soon as the object is instantiated, before TNObject's ID has been set. Start() is executed after it was set.