Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - toreau

Pages: [1]
1
TNet 3 Support / TNManager.Create'ed objects doesn't initialize
« on: June 12, 2014, 01:59:23 PM »
Hi!

I have an inventory system where I can drag anything from the inventory and into "the open", ie. the terrain, and I want the game to create a "sack" over the network and fill it whatever I'm throwing away.

Easy enough, one would think, but the problem I'm facing is that Create() doesn't behave like Instantiate, as it doesn't really instantiate the object. Consider this code, which is run whenever I drop an item (or items) into "the open."

  1. public void OnDragEnd ( dfControl source, dfDragEventArgs args ) {
  2.         if ( args.Target.GetType().ToString() == "dfPanel" && args.Data is SlotPanelController ) {
  3.  
  4.                 // Create a new ThrowAwaySack
  5.                 TNManager.Create( "Prefabs/ThrowAwaySack", playerCamera.transform.position, Quaternion.identity, false );
  6.  
  7.                 // Populate the ThrowAwaySack's inventory
  8.                 InventoryController throwAwaySackInventoryController = throwAwaySack.GetComponent<InventoryController>();
  9.                 SlotPanelController slotPanelController = (SlotPanelController)args.Data;
  10.  
  11.                 Slot sourceSlot = slotPanelController.slot;
  12.  
  13.                 int itemsAdded = throwAwaySackInventoryController.AddItems( sourceSlot.items[0], sourceSlot.items.Count );
  14.                 sourceSlot.RemoveItems( sourceSlot.items[0], itemsAdded );
  15.  
  16.                 // Mark as dropped
  17.                 args.State = dfDragDropState.Dropped;
  18.  
  19.         }
  20. }
  21.  

Each ThrowAwaySack has an InventoryController attached to it, and with this code the sack itself gets created over the network just fine. However, its InventoryController is never initialized, ie. it never does Awake(), which is crucial for things to work.

With f.ex. PhotonNetwork, I could do a PhotonNetwork.Instantiate() and get an initialized GameObject back, but TNManager.Create() is void, so...?

Any help is appreciated!

Thanks!

2
How would/should one approach this? Change the server code so that it takes care of everything weather-/season-related? Or...?

This also goes for day/night cycles, of course.

3
TNet 3 Support / Player gets spawn twice, but only occasionally
« on: May 13, 2014, 09:07:00 AM »
I have another weird problem with TNet, and this time I'll be sure to flood you with information. :) It might be related to another problem I have, but I wanted to start a new thread "to make it clean."

I have a project with two scenes; Game and Disconnect. The latter is empty, only showing a GUIText "You are now disconnected", while the former has a cube (for ground), a directional light and a "Connection" object.

The "Connection" object has two components attached to it: a TNManager, with the Player prefab attached to it, and a ConnectionController script, which is pretty basic and looks like this:

  1. using UnityEngine;
  2. using TNet;
  3.  
  4. public class ConnectionController : MonoBehaviour {
  5.  
  6.         void Start () {
  7.                 TNManager.Connect( "127.0.0.1:5127" );
  8.         }
  9.  
  10.         void OnNetworkConnect ( bool success, string message ) {
  11.                 if ( success ) {
  12.                         TNManager.JoinChannel( 1, "Game" );
  13.                 }
  14.         }
  15.  
  16.         void OnNetworkJoinChannel ( bool success, string message ) {
  17.                 if ( success ) {
  18.                         TNManager.Create( "Prefabs/Player", false );
  19.                 }
  20.         }
  21.  
  22. }
  23.  

Connecting/disconnecting works just fine, and I can mention that my client (workstation) is a MacBook Pro running the latest version of OSX, the latest version of Unity3D, and have the latest version of TNet (1.9.0b).

The server is running Debian 6.0.9, and is not running on 127.0.0.1 as mentioned in the code above, but I want certain things to be a secret. :) But - as mentioned - connecting and disconnecting works just fine (or so it seems, at least.)

The Player prefab has two components attached to it; a TNObject (id = 0) and a PlayerController script, which looks like this:

  1. using UnityEngine;
  2. using TNet;
  3.  
  4. public class PlayerController : TNBehaviour {
  5.  
  6.         static public PlayerController instance;
  7.  
  8.         private CharacterController characterController;
  9.         private Camera playerCamera;
  10.  
  11.         void Awake () {
  12.                 Debug.Log ( "PlayerController.Awake()" );
  13.                 if ( TNManager.isThisMyObject ) {
  14.                         instance = this;
  15.  
  16.                         characterController = GetComponent<CharacterController>();
  17.                         playerCamera        = GetComponentInChildren<Camera>();
  18.  
  19.                         Instantiate( Resources.Load("Prefabs/UI Root") );
  20.                 }
  21.     }
  22.  
  23.  

The "UI Root" prefab has nothing attached to it, except the standard DF-GUI stuff. I know I probably should use NGUI, but I had to choose which one to spend $90 on, and I went for DF-GUI. Sorry about that. :)

Now - back to the issue at hand.

Every now and then - most of the time, it seems - when I play the game, the Player is instantiated twice, and they are assigned two different IDs. I've attached a screenshot.

Any ideas why this is happening?

UPDATE: It might be worth mentioning that when this happens, the server says that only one client is connected.

UPDATE #2: I added some debug messages in TNManager.cs, and the second Create() method is being called twice, but I'm 200% sure that Create() is being called only once from "my end" (ie. in the ConnectionController script.)

UPDATE #3: After some more debugging, I was wrong about the 200%. :D The OnNetworkJoinChannel() in my ConnectionController script is actually being called twice, but the OnNetworkConnect() is called only once when that happens. In what cirumstances can that happen?

4
TNet 3 Support / First person controller not working as expected
« on: May 10, 2014, 11:48:21 AM »
I almost feel ashamed of posting this, but here goes.

I'm in the process of converting my code from Photon to TNet, and I instantly encountered a problem with my first person player. The problem itself is well-known; I do a build, connect, the player works just fine. I go back to Unity and connect from there, everything works fine. Back to the build, and now I control the other player.

I have searched this forum for any clues, but every working piece of code "logic" I find, it makes me just think that my code should work.

Anyway, here it is:

  1. using UnityEngine;
  2. using TNet;
  3.  
  4. public class PlayerController : TNBehaviour {
  5.  
  6.         static public PlayerController instance;
  7.  
  8.         #region MouseLook
  9.  
  10.         private float mouseSensitivity  =   2.0f;
  11.         private float upDownRotation    =   0.0f;
  12.         private float minUpDownRotation = -70.0f;
  13.         private float maxUpDownRotation =  70.0f;
  14.    
  15.     #endregion
  16.  
  17.         #region Movement
  18.  
  19.         private float walkingSpeed   =   6.0f;
  20.         private float runningSpeed   =  12.0f;
  21.         private float crouchingSpeed =   2.0f;
  22.         private float currentSpeed   =   0.0f;
  23.  
  24.         private float jumpHeight    = 5.0f;
  25.         private float gravityForce  = 9.8f;
  26.         private float verticalSpeed = 0.0f;
  27.  
  28.         private bool isRunning   = false;
  29.         private bool isCrouching = false;
  30.  
  31.         #endregion
  32.  
  33.         private CharacterController characterController;
  34.         private Camera              playerCamera;
  35.  
  36.         void Awake () {
  37.                 if ( TNManager.isThisMyObject ) {
  38.                         instance = this;
  39.                 }
  40.     }
  41.  
  42.         void Start () {
  43.                 if ( !tno.isMine ) {
  44.                         this.enabled = false;
  45.         }
  46.  
  47.                 characterController = GetComponent<CharacterController>();
  48.                 playerCamera        = GetComponentInChildren<Camera>();
  49.     }
  50.  
  51.         void Update () {
  52.                 if ( !tno.isMine ) {
  53.                         return;
  54.                 }
  55.  
  56.                 UpdateMouseLook();
  57.                 UpdateMovement();
  58.     }
  59.  
  60.         void UpdateMouseLook () {
  61.                 float leftRightRotation = Input.GetAxis ("Mouse X") * mouseSensitivity;
  62.                 characterController.transform.Rotate ( 0, leftRightRotation, 0 );
  63.                
  64.                 upDownRotation -= Input.GetAxis ("Mouse Y") * mouseSensitivity;
  65.                 upDownRotation  = Mathf.Clamp ( upDownRotation, minUpDownRotation, maxUpDownRotation );
  66.                
  67.                 playerCamera.transform.localRotation = Quaternion.Euler ( upDownRotation, 0, 0 );
  68.     }
  69.  
  70.         void UpdateMovement () {
  71.  
  72.                 // Decide speed
  73.                 isRunning    = ( Input.GetKey(KeyCode.LeftShift) ) ? true : false;
  74.                 currentSpeed = ( isRunning ) ? runningSpeed : walkingSpeed;
  75.                
  76.                 // Crouching speed?
  77.                 if ( isCrouching ) {
  78.                         currentSpeed = crouchingSpeed;
  79.                 }
  80.                
  81.                 // Gravity
  82.                 verticalSpeed -= gravityForce * Time.deltaTime;
  83.                 verticalSpeed = Mathf.Clamp ( verticalSpeed, -3000, jumpHeight ); // TODO: Do this in a better way
  84.  
  85.                 // Things that can only be done while on the ground
  86.                 if ( characterController.isGrounded ) {
  87.                        
  88.                         // Are we jumping?
  89.                         if ( Input.GetButtonDown ("Jump") ) {
  90.                                 verticalSpeed = jumpHeight;
  91.                         }
  92.                        
  93.                         // Are we crouching?
  94.                         if ( Input.GetKey (KeyCode.LeftControl) ) {
  95.                                 if ( !isCrouching ) {
  96.                                         characterController.height /= 2;
  97.                                        
  98.                                         isCrouching = true;
  99.                                 }
  100.                         }
  101.                         else {
  102.                                 if ( isCrouching ) {
  103.                                         characterController.height *= 2;
  104.                                        
  105.                                         Transform characterTransform = characterController.transform;
  106.                                         characterTransform.position = new Vector3( characterTransform.position.x, characterTransform.position.y + (characterController.height / 2), characterTransform.position.z );
  107.                                        
  108.                                         isCrouching = false;
  109.                                 }
  110.                         }
  111.                        
  112.                 }
  113.         // Things that can only be done while NOT on the ground
  114.         else {
  115.             // TODO
  116.         }
  117.        
  118.         // Calculate speed
  119.         Vector3 motion = new Vector3 (Input.GetAxis ("Horizontal") * currentSpeed, verticalSpeed, Input.GetAxis ("Vertical") * currentSpeed);
  120.        
  121.         motion = characterController.transform.rotation * motion;
  122.        
  123.         // Do the actual movement
  124.         characterController.Move ( motion * Time.deltaTime );
  125.  
  126.         }
  127.  
  128. }
  129.  

Everything is AutoSync'ed at the moment (I will convert to RFCs when I'm sure things are working as expected). I'm sure I'm overlooking something super-obvious, but I can't for the life of it see what.

Pages: [1]