Author Topic: Best approach for sending commands via NGUI to a network object..  (Read 2719 times)

r6834

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 5
    • View Profile
What is the best approach for building your character input system on top of NGUI, then sending commands from NGUI buttons and widgets to your own player over the network?

Would you want to create the UI like most other network objects at the game start?

Is it necessary to have your controller script send your own players commands over the network, or is it best to just send the results from that input command to the rest of the room?

What is the best way to reference and send commands to your own player?

Currently, I have it working pretty well like this, but I want to understand if this is the right approach.

My input handler script attached to an NGUI widget that lives in the scene :
  1. var myPlayer : GameObject;
  2.  
  3. function Start () {
  4.        
  5. }
  6.  
  7. function OnClick () {
  8.                
  9.                 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
  10.                 var hit : RaycastHit;
  11.                        
  12.                 Debug.Log("click");
  13.                          
  14.                 if (Physics.Raycast (ray, hit, 100)) {
  15.                                
  16.                         if (hit.collider.gameObject.layer == 9) {
  17.                                        
  18.                                 var targetPoint = hit.point;
  19.                                 var myTno = myPlayer.GetComponent(TNObject);
  20.                                 myTno.Send("OnSetTarget", TNet.Target.AllSaved, targetPoint);
  21.                                        
  22.                         }
  23.                                
  24.                 }
  25.  
  26. }

My character controller script :
  1. #pragma strict
  2. var targetPos : Vector3;
  3. var tno : TNObject;
  4. var myController : TouchHandler;
  5.  
  6. function Start () {
  7.  
  8.         tno = gameObject.GetComponent.<TNObject>();
  9.         targetPos = transform.position;
  10.        
  11.         if (tno.isMine) {
  12.        
  13.                 myController = GameObject.Find("LookArea").GetComponent(TouchHandler);
  14.                 myController.myPlayer = gameObject;
  15.  
  16.         }
  17. }
  18.  
  19. @TNet.RFC
  20. function OnSetTarget (tar) {
  21.  
  22.         transform.position = tar;
  23.  
  24. }
« Last Edit: May 01, 2014, 09:31:17 PM by r6834 »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Best approach for sending commands via NGUI to a network object..
« Reply #1 on: May 02, 2014, 09:55:20 AM »
I wouldn't do any raycasts myself. NGUI already does raycasts for you as long as there is a UICamera on the camera that sees the objects. All objects are capable of receiving all NGUI events, not just UI elements.

Separate your sync script from your input. In the example you posted you seem to have a OnSetTarget script on every object. This seems wrong, as this means you can have many targets active at once. Unless it's intentional, you should likely have one object -- one script -- and set its target instead, especially considering how you're saving the RFC.

Remember, you can always send the TNO ID to be the target, and can easily find this object via TNObject.Find.