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.


Messages - Rexima

Pages: [1] 2 3 ... 6
1
TNet 3 Support / Make a dedicated authoritative unity Server?
« on: August 16, 2017, 07:34:45 PM »
Hey,

i plan to make Transport tycoon and i can't let the player be host and send all vehicles to the gameserver and the gameserver relays it to other players.

I need to make a authoritative unity server, where the unity instance is the server and hoster.
So if I want to create a vehicle, the server is creating it and send every data to other players.

I made an quick test with 5000 Objects that are created on my pc and I had an outbound at 1Mbit to the gameserver(From the zip)(hosted on my datacenter with 1Gbs Up and Down).

So I would be Hard limited, if other players who host a game on their pc with a lower bandwidth.
So I decided to make an authoritative gameserver with unity logic.

And here is the question, how can I make in unity, that the gameserver is also the Hoster?

2
TNet 3 Support / Re: Is TNet using something like Protobuf?
« on: August 16, 2017, 07:25:23 PM »
Okay thank you very much

3
TNet 3 Support / Re: Is TNet using something like Protobuf?
« on: August 16, 2017, 04:30:46 PM »
I ask, because the original BinaryFormatter from the .Net Framework is much slower than Protobuf or MsgPack.

4
TNet 3 Support / Is TNet using something like Protobuf?
« on: August 16, 2017, 05:10:50 AM »
Hi,

is TNet using something like Protobuf or Messagepack?
Or is it possible to extend TNet with one of them?

5
NetworkTransform is my own class. Posted on this thread(http://www.tasharen.com/forum/index.php?topic=14959.0)

6
TNet 3 Support / Re: RFC got fired only at the third try
« on: November 05, 2016, 10:26:34 AM »
NetworkTransform is my own script. I dont use Unity Networking.

Thats my NetworkTransform:
  1. using UnityEngine;
  2. using System.Collections;
  3. using TNet;
  4.  
  5. public abstract class NetworkTransform : TNBehaviour
  6. {
  7.     private int m_currentOwner = 0;
  8.  
  9.     [Range(0.0f, 30.0f)]
  10.     public int m_updatesPerSecond = 20;
  11.  
  12.     [Range(0.0f, 10.0f)]
  13.     public double m_InterpolationBackTime = 0.1;
  14.  
  15.     [Range(0.0f, 20.0f)]
  16.     public double m_ExtrapolationLimit = 0.5;
  17.  
  18.     [HideInInspector]
  19.     public CharacterController m_characterController;
  20.     [HideInInspector]
  21.     public PlayerController m_playerController;
  22.  
  23.     private Rigidbody m_rigidBody;
  24.  
  25.     internal struct State
  26.     {
  27.         internal double timestamp;
  28.         internal Vector3 pos;
  29.         internal Vector3 velocity;
  30.         internal Vector3 angularVelocity;
  31.         internal Quaternion rot;
  32.         internal Vector3 moveInput;
  33.         internal Quaternion mouseInput;
  34.     }
  35.  
  36.     // We store twenty states with "playback" information
  37.     State[] m_BufferedState = new State[20];
  38.     // Keep track of what slots are used
  39.     int m_TimestampCount;
  40.  
  41.     public virtual void Awake()
  42.     {
  43.         if (GetComponent<Rigidbody>() != null)
  44.         {
  45.             m_rigidBody = GetComponent<Rigidbody>();
  46.         }
  47.  
  48.         if (TNManager.isHosting)
  49.             m_currentOwner = TNManager.playerID;
  50.     }
  51.  
  52.     public virtual void Start()
  53.     {
  54.     }
  55.  
  56.     public virtual void Update ()
  57.     {
  58.         if (!tno.isMine)
  59.             InterpolateTransform();
  60.     }
  61.  
  62.     public IEnumerator PeriodicSync()
  63.     {
  64.         for (;;)
  65.         {
  66.             if (TNManager.IsInChannel(tno.channelID))
  67.             {
  68.                 if (IsOwnerOfThisObject)
  69.                 {
  70.                     Sync();
  71.                 }
  72.                 else if (HostOwnsThisObject)
  73.                 {
  74.                     Sync();
  75.                 }
  76.             }
  77.             yield return new WaitForSeconds(1f / m_updatesPerSecond);
  78.         }
  79.     }
  80.  
  81.     void Sync()
  82.     {
  83.         if (m_rigidBody != null)
  84.         {
  85.             tno.SendQuickly("SetTransform", Target.OthersSaved, m_rigidBody.position, m_rigidBody.rotation, m_rigidBody.velocity, m_rigidBody.angularVelocity, Vector3.zero, Quaternion.identity);
  86.         }
  87.         else
  88.         {
  89.             tno.SendQuickly("SetTransform", Target.OthersSaved, transform.position, transform.rotation, Vector3.zero, Vector3.zero, Vector3.zero, Quaternion.identity);
  90.         }
  91.     }
  92.  
  93.     void InterpolateTransform()
  94.     {
  95.         // We have a window of interpolationBackTime where we basically play
  96.         // By having interpolationBackTime the average ping, you will usually use interpolation.
  97.         // And only if no more data arrives we will use extra polation
  98.  
  99.         // This is the target playback time of the rigid body
  100.         double interpolationTime = Time.time - m_InterpolationBackTime;
  101.  
  102.         // Use interpolation if the target playback time is present in the buffer
  103.         if (m_BufferedState[0].timestamp > interpolationTime)
  104.         {
  105.             // Go through buffer and find correct state to play back
  106.             for (int i = 0; i < m_TimestampCount; i++)
  107.             {
  108.                 if (m_BufferedState[i].timestamp <= interpolationTime || i == m_TimestampCount - 1)
  109.                 {
  110.                     // The state one slot newer (<100ms) than the best playback state
  111.                     State rhs = m_BufferedState[Mathf.Max(i - 1, 0)];
  112.                     // The best playback state (closest to 100 ms old (default time))
  113.                     State lhs = m_BufferedState[i];
  114.  
  115.                     // Use the time between the two slots to determine if interpolation is necessary
  116.                     double length = rhs.timestamp - lhs.timestamp;
  117.                     float t = 0.0F;
  118.                     // As the time difference gets closer to 100 ms t gets closer to 1 in
  119.                     // which case rhs is only used
  120.                     // Example:
  121.                     // Time is 10.000, so sampleTime is 9.900
  122.                     // lhs.time is 9.910 rhs.time is 9.980 length is 0.070
  123.                     // t is 9.900 - 9.910 / 0.070 = 0.14. So it uses 14% of rhs, 86% of lhs
  124.                     if (length > 0.0001)
  125.                         t = (float)((interpolationTime - lhs.timestamp) / length);
  126.  
  127.                     // if t=0 => lhs is used directly
  128.                     transform.localPosition = Vector3.Lerp(lhs.pos, rhs.pos, t);
  129.                     transform.localRotation = Quaternion.Slerp(lhs.rot, rhs.rot, t);
  130.  
  131.                     if(m_playerController != null)
  132.                     {
  133.                         m_playerController.MoveInput = Vector3.Lerp(lhs.moveInput, rhs.moveInput, t);
  134.                         m_playerController.MouseInput = Quaternion.Slerp(lhs.mouseInput, rhs.mouseInput, t);
  135.                     }
  136.                     return;
  137.                 }
  138.             }
  139.         }
  140.         // Use extrapolation
  141.         else
  142.         {
  143.             State latest = m_BufferedState[0];
  144.  
  145.             float extrapolationLength = (float)(interpolationTime - latest.timestamp);
  146.             // Don't extrapolation for more than 500 ms, you would need to do that carefully
  147.             if (extrapolationLength < m_ExtrapolationLimit)
  148.             {
  149.                 float axisLength = extrapolationLength * latest.angularVelocity.magnitude * Mathf.Rad2Deg;
  150.                 Quaternion angularRotation = Quaternion.AngleAxis(axisLength, latest.velocity);
  151.  
  152.                 if (m_rigidBody != null)
  153.                 {
  154.                     m_rigidBody.position = latest.pos + latest.angularVelocity * extrapolationLength;
  155.                     m_rigidBody.rotation = angularRotation * latest.rot;
  156.                     m_rigidBody.velocity = latest.velocity;
  157.                     m_rigidBody.angularVelocity = latest.angularVelocity;
  158.                 }
  159.                 else
  160.                 {
  161.                     transform.position = latest.pos + latest.angularVelocity * extrapolationLength;
  162.                     transform.rotation = angularRotation * latest.rot;
  163.  
  164.                     if (m_playerController != null)
  165.                     {
  166.                         m_playerController.MoveInput = latest.moveInput;
  167.                         m_playerController.MouseInput = latest.mouseInput;
  168.                     }
  169.  
  170.                     if (m_characterController != null)
  171.                     {
  172.                         m_characterController.SimpleMove(latest.velocity);
  173.                     }
  174.                 }
  175.             }
  176.         }
  177.     }
  178.  
  179.     [RFC]
  180.     protected void SetTransform(Vector3 pos, Quaternion rot, Vector3 vel, Vector3 angularVelocity, Vector3 moveInput, Quaternion mouseInput)
  181.     {
  182.         // Shift the buffer sideways, deleting state 20
  183.         for (int i = m_BufferedState.Length - 1; i >= 1; i--)
  184.         {
  185.             m_BufferedState[i] = m_BufferedState[i - 1];
  186.         }
  187.  
  188.         // Record current state in slot 0
  189.         State state;
  190.         state.timestamp = Time.time;
  191.         state.pos = pos;
  192.         state.velocity = vel;
  193.         state.rot = rot;
  194.         state.angularVelocity = angularVelocity;
  195.         state.moveInput = moveInput;
  196.         state.mouseInput = mouseInput;
  197.         //state.input = input;
  198.         m_BufferedState[0] = state;
  199.  
  200.         // Update used slot count, however never exceed the buffer size
  201.         // Slots aren't actually freed so this just makes sure the buffer is
  202.         // filled up and that uninitalized slots aren't used.
  203.         m_TimestampCount = Mathf.Min(m_TimestampCount + 1, m_BufferedState.Length);
  204.  
  205.         // Check if states are in order, if it is inconsistent you could reshuffel or
  206.         // drop the out-of-order state. Nothing is done here
  207.         for (int i = 0; i < m_TimestampCount - 1; i++)
  208.         {
  209.             if (m_BufferedState[i].timestamp < m_BufferedState[i + 1].timestamp)
  210.                 Log.Info("State inconsistent");
  211.         }
  212.     }
  213.  
  214.     public void SetLocalWantControl(bool _set)
  215.     {
  216.         tno.Send("OnPlayerWantControls", Target.Host, TNManager.playerID, _set);
  217.     }
  218.  
  219.     [RFC]
  220.     void OnPlayerWantControls(int _playerID, bool _set)
  221.     {
  222.         if (_set) //PLAYER WANT CONTROL
  223.         {
  224.             if (IsOwnerOfThisObject) //HOSTER CONTROLS THIS OBJ
  225.             {
  226.                 OwnerOfThisObject = _playerID;
  227.             }
  228.         }
  229.         else //PLAYER DONT WANT CONTROL - LET HOSTER CONTROL
  230.         {
  231.             OwnerOfThisObject = TNManager.playerID;
  232.         }
  233.  
  234.         tno.Send("OnPlayerGetControls", Target.Others, m_currentOwner);
  235.     }
  236.  
  237.     [RFC]
  238.     void OnPlayerGetControls(int _newOwner)
  239.     {
  240.         OwnerOfThisObject = _newOwner;
  241.     }
  242.  
  243.     public bool IsOwnerOfThisObject
  244.     {
  245.         get
  246.         {
  247.             return OwnerOfThisObject == TNManager.playerID;
  248.         }
  249.     }
  250.  
  251.     public bool HostOwnsThisObject
  252.     {
  253.         get
  254.         {
  255.             return IsOwnerOfThisObject && TNManager.isHosting;
  256.         }
  257.     }
  258.  
  259.     public int OwnerOfThisObject
  260.     {
  261.         get
  262.         {
  263.             return m_currentOwner;
  264.         }
  265.         set
  266.         {
  267.             m_currentOwner = value;
  268.         }
  269.     }
  270. }
  271.  

Whats really suspicious, i have about 10 cars. And only one car make this error. All other 9 cars work, without any error.

7
This is my sync script for vehicles attached on each vehicle


  1. public class NetworkVehicle : NetworkTransform
  2. {
  3.     [Range(0.0f, 30.0f)]
  4.     public int m_updatesPerSecondInput = 10;
  5.  
  6.     private VehicleController m_vehicleController;
  7.  
  8.     public override void Awake()
  9.     {
  10.         base.Awake();
  11.  
  12.         m_vehicleController = GetComponent<VehicleController>();
  13.     }
  14.  
  15.     IEnumerator Start()
  16.     {
  17.         while (TNManager.isJoiningChannel) yield return null;
  18.  
  19.         StartCoroutine(PeriodicSync());
  20.         StartCoroutine(PeriodicSyncInput());
  21.     }
  22.  
  23.     //public override void Start ()
  24.     //{
  25.     //    base.Start();
  26.  
  27.                    
  28.     //}
  29.  
  30.     public override void Update ()
  31.     {
  32.         base.Update();
  33.     }
  34.  
  35.     IEnumerator PeriodicSyncInput()
  36.     {
  37.         for (;;)
  38.         {
  39.             if (TNManager.IsInChannel(tno.channelID) && IsOwnerOfThisObject)
  40.             {
  41.                 tno.SendQuickly("SetAxis", Target.OthersSaved, m_vehicleController.MovementInput, m_vehicleController.HandbrakeInput);
  42.             }
  43.  
  44.             yield return new WaitForSeconds(1f / m_updatesPerSecondInput);
  45.         }
  46.     }
  47.  
  48.     [RFC]
  49.     protected void SetAxis(Vector2 v, bool _handbrake)
  50.     {
  51.         m_vehicleController.MovementInput = v;
  52.         m_vehicleController.HandbrakeInput = _handbrake;
  53.     }
  54. }
  55.  


8
I dont renamed a prefab or deleted it.

When a channel gets created and the player joins, all objects will be correctly instantiated without any errors. Than an another player joins the game and he got this errors.

I wrapped all my sync functions with if (TNManager.isInChannel) and it doesnt work...

9
TNet 3 Support / RFC got fired only at the third try
« on: November 04, 2016, 09:54:34 AM »
This happens on my vehicle.
When i try to enter it, i need to press it a few times and than i can get into it. (Im asking the hoster, if i can access this vehicle)

Error Message:
  1. NullReferenceException: Object reference not set to an instance of an object
  2. TNet.TNObject.RebuildMethodList () (at Assets/ThirdParty/TNet/Client/TNObject.cs:637)
  3. TNet.TNObject.Execute (System.String funcName, System.Object[] parameters) (at Assets/ThirdParty/TNet/Client/TNObject.cs:541)
  4. TNet.TNObject.SendRFC (Byte rfcID, System.String rfcName, Target target, Boolean reliable, System.Object[] objs) (at Assets/ThirdParty/TNet/Client/TNObject.cs:882)
  5. TNet.TNObject.Send (System.String rfcName, Target target, System.Object[] objs) (at Assets/ThirdParty/TNet/Client/TNObject.cs:685)
  6. NetworkTransform.SetLocalWantControl (Boolean _set) (at Assets/GameAssets/Scripts/Network/NetworkTransform.cs:216)
  7. VehicleSeats.EnterVehicleLocal (Boolean _enter, UnityEngine.GameObject _player) (at Assets/GameAssets/Scripts/Vehicle/VehicleSeats.cs:74)
  8. VehicleController.OnTriggerStay (UnityEngine.Collider other) (at Assets/GameAssets/Scripts/Vehicle/VehicleController.cs:107)

VehicleController.cs:107
  1. m_vehicleSeats.EnterVehicleLocal(true, m_player);

VehicleSeats.cs:74 (m_networkVehicle is a Child Class from NetworkTransform)
  1. m_networkVehicle.SetLocalWantControl(true);

NetworkTransform.cs:216
  1. public void SetLocalWantControl(bool _set)
  2.     {
  3.         tno.Send("OnPlayerWantControls", Target.Host, TNManager.playerID, _set);
  4.     }
  5.  
  6.     [RFC]
  7.     void OnPlayerWantControls(int _playerID, bool _set)
  8.     {
  9.         if (_set) //PLAYER WANT CONTROL
  10.         {
  11.             if (IsOwnerOfThisObject) //HOSTER CONTROLS THIS OBJ
  12.             {
  13.                 OwnerOfThisObject = _playerID;
  14.             }
  15.         }
  16.         else //PLAYER DONT WANT CONTROL - LET HOSTER CONTROL
  17.         {
  18.             OwnerOfThisObject = TNManager.playerID;
  19.         }
  20.  
  21.         tno.Send("OnPlayerGetControls", Target.Others, m_currentOwner);
  22.     }
  23.  
  24.     [RFC]
  25.     void OnPlayerGetControls(int _newOwner)
  26.     {
  27.         OwnerOfThisObject = _newOwner;
  28.     }
  29.  
  30.     public bool IsOwnerOfThisObject
  31.     {
  32.         get
  33.         {
  34.             return OwnerOfThisObject == TNManager.playerID;
  35.         }
  36.     }
  37.  
  38.     public bool HostOwnsThisObject
  39.     {
  40.         get
  41.         {
  42.             return IsOwnerOfThisObject && TNManager.isHosting;
  43.         }
  44.     }
  45.  
  46.     public int OwnerOfThisObject
  47.     {
  48.         get
  49.         {
  50.             return m_currentOwner;
  51.         }
  52.         set
  53.         {
  54.             m_currentOwner = value;
  55.         }
  56.     }

10
I have currently a huge problem. I dont know what i did, but my game worked a few days ago, and now when a second player trys to join the game, it's freeze and than i got disconnected.
My first player is my builded .exe and my second player is my Unity Editor.

And during the freeze time i got the "[TNet] Trying to execute a function xy before it has been created." logs and about 250 times per gameobject..

Here is an screenshot of my editor


When i disable the synchronizing function, it works. But i didn't changed them.

11
TNet 3 Support / Re: Not able to obtain unique playerID
« on: October 23, 2016, 12:32:54 PM »
  1.                 [RCC]
  2.                 static GameObject CreatePlayer (GameObject prefab, Vector3 pos, Quaternion rot)
  3.                 {
  4.                         // Instantiate the prefab
  5.                         GameObject go = prefab.Instantiate();
  6.  
  7.                         // Set the position and rotation based on the passed values
  8.                         Transform t = go.transform;
  9.                         t.position = pos;
  10.                         t.rotation = rot;
  11.                         go.name = "player" + TNManager.playerID; // The name of the gameobject is the same for each player that joins
  12.                         return go;
  13.  
  14.                 }
  15.  
  16.  

If im correct, this part will get called on all players. And when someone's joins the game, my game calls this function to create the other player and it will use my "TNManager.playerID".
I suggest you, if you have an class on your Player Gameobject, for example called "Player.cs" to put in the Start() this code:
  1. if (tno.isMine)
  2. {
  3.      gameObject.name = TNManager.playerName + "_" + tno.ownerID;
  4. }else{
  5.      gameObject.name = TNManager.GetPlayer(tno.ownerID).name + "_" + tno.ownerID;
  6. }
  7.  

Thats, how i do it. (Don't forget, to set the Playername before joining)

12
TNet 3 Support / How to simulate latency and packet loss?
« on: October 21, 2016, 06:49:14 AM »
How can i simulate latency and packet loss in TNet, like in UNet?

13
TNet 3 Support / How to see stats like incoming and outgoing bytes?
« on: October 21, 2016, 03:01:57 AM »
Is it possible to display in TNet my incoming and outgoing bytes and messages(rfc's)?
To see how much bandwith are used on Client and Server side?

14
TNet 3 Support / Re: Send RFC Error on manuel added TNObject
« on: October 18, 2016, 12:16:21 PM »
Okay, found the problem. I can transfer the channel when its dynamically created.
I will split up my code like you explained it. Thank you very much.

15
TNet 3 Support / Re: Set Player Limit doenst work?
« on: October 18, 2016, 12:00:41 PM »
Thank you, got it!

Pages: [1] 2 3 ... 6