Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Socket only sends data with Debug

Discussion in 'Multiplayer' started by AtlantiaKing, Feb 6, 2020.

  1. AtlantiaKing

    AtlantiaKing

    Joined:
    Jul 23, 2015
    Posts:
    6
    Hello all!
    (Sorry if this is the wrong section of the forum to post this)

    I'm creating an online survival game with a self-made server.

    Everything worked fine until I stumbled upon a strange bug. When sending the data for the creation of a new entity the client won't send any data. When I put a Debug.Log in front of it, it suddenly starts working.
    When I remove the Debug.Log again, it stops working.

    I tried putting a try/catch around it, but nothing is wrong with it.

    SyncEntity script: This Debug.Log ALWAYS gets triggered, even if SendData doesn't get called.
    Code (CSharp):
    1. public void SyncEntity(EntityType type, Vector3 location, Quaternion rotation)
    2.     {
    3.         PacketBuffer buffer = new PacketBuffer();
    4.         buffer.WriteInteger((int)ClientPackets.SyncEntity);
    5.         buffer.WriteString(type.ToString());
    6.         buffer.WriteFloat(location.x);
    7.         buffer.WriteFloat(location.y);
    8.         buffer.WriteFloat(location.z);
    9.         buffer.WriteFloat(rotation.eulerAngles.y);
    10.         SendData(buffer.ToArray());
    11.         buffer.Dispose();
    12.         Debug.Log("SyncEntity finished");
    13.     }
    SendData script: Socket only sends data if there is a Debug.Log (or Console.WriteLine) in front of it
    Code (CSharp):
    1. public void SendData(byte[] data)
    2.     {
    3.         Debug.Log("test");
    4.         handler._clientsocket.Send(data);
    5.     }
    Any idea how this is possible or how to fix this issue?

    Thank you in advance.
     
    Last edited: Feb 6, 2020
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Can you show more of the script(s) and your console output showing the behavior you're saying is happening?
     
  3. AtlantiaKing

    AtlantiaKing

    Joined:
    Jul 23, 2015
    Posts:
    6
    Another interesting thing: If there is no Debug.Log it works once, but never more then once.

    Complete ClientSender class:
    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. class ClientSender : MonoBehaviour
    6. {
    7.     private ClientHandler handler;
    8.  
    9.     private void Start()
    10.     {
    11.         handler = GetComponent<ClientHandler>();
    12.     }
    13.  
    14.     public void SendData(byte[] data)
    15.     {
    16.         Debug.Log("test");
    17.         handler._clientsocket.Send(data);
    18.     }
    19.  
    20.     public void SendClientData(string username)
    21.     {
    22.         PacketBuffer buffer = new PacketBuffer();
    23.         buffer.WriteInteger((int)ClientPackets.ClientData);
    24.         buffer.WriteString(username);
    25.         SendData(buffer.ToArray());
    26.         buffer.Dispose();
    27.     }
    28.  
    29.     public void SendChatMessage(string msg)
    30.     {
    31.         PacketBuffer buffer = new PacketBuffer();
    32.         buffer.WriteInteger((int)ClientPackets.ClientChatMessage);
    33.         buffer.WriteString(msg);
    34.         SendData(buffer.ToArray());
    35.         buffer.Dispose();
    36.     }
    37.  
    38.     public void SendInput(Vector2 input)
    39.     {
    40.         PacketBuffer buffer = new PacketBuffer();
    41.         buffer.WriteInteger((int)ClientPackets.PlayerInput);
    42.         buffer.WriteFloat(input.x);
    43.         buffer.WriteFloat(input.y);
    44.         SendData(buffer.ToArray());
    45.         buffer.Dispose();
    46.     }
    47.  
    48.     public void SendMouseInput(Vector2 rot)
    49.     {
    50.         PacketBuffer buffer = new PacketBuffer();
    51.         buffer.WriteInteger((int)ClientPackets.PlayerMouseInput);
    52.         buffer.WriteFloat(rot.x);
    53.         buffer.WriteFloat(rot.y);
    54.         SendData(buffer.ToArray());
    55.         buffer.Dispose();
    56.     }
    57.  
    58.     public void SendJump()
    59.     {
    60.         PacketBuffer buffer = new PacketBuffer();
    61.         buffer.WriteInteger((int)ClientPackets.PlayerJump);
    62.         SendData(buffer.ToArray());
    63.         buffer.Dispose();
    64.     }
    65.  
    66.     public void SendInteract()
    67.     {
    68.         PacketBuffer buffer = new PacketBuffer();
    69.         buffer.WriteInteger((int)ClientPackets.PlayerInteract);
    70.         SendData(buffer.ToArray());
    71.         buffer.Dispose();
    72.     }
    73.  
    74.     public void AskInventory()
    75.     {
    76.         PacketBuffer buffer = new PacketBuffer();
    77.         buffer.WriteInteger((int)ClientPackets.RequestInventory);
    78.         SendData(buffer.ToArray());
    79.         buffer.Dispose();
    80.     }
    81.  
    82.     public void SyncInventory(List<Item> inv, Item equiped)
    83.     {
    84.         PacketBuffer buffer = new PacketBuffer();
    85.         buffer.WriteInteger((int)ClientPackets.SyncInventory);
    86.         buffer.WriteInteger(inv.Count);
    87.         foreach(Item i in inv)
    88.         {
    89.             buffer.WriteString(i.ItemType.ToString());
    90.         }
    91.         if (equiped == null)
    92.         {
    93.             buffer.WriteString("null");
    94.         }
    95.         else
    96.         {
    97.             buffer.WriteString(equiped.ItemType.ToString());
    98.         }
    99.         SendData(buffer.ToArray());
    100.         buffer.Dispose();
    101.     }
    102.  
    103.     public void SyncInventory(Client c)
    104.     {
    105.         List<Item> inv = c.inventory.GetContent();
    106.         PacketBuffer buffer = new PacketBuffer();
    107.         buffer.WriteInteger((int)ClientPackets.SyncInventory);
    108.         buffer.WriteInteger(inv.Count);
    109.         foreach (Item i in inv)
    110.         {
    111.             buffer.WriteString(i.ItemType.ToString());
    112.         }
    113.         if(c.equiped == null)
    114.         {
    115.             buffer.WriteString("null");
    116.         } else
    117.         {
    118.             buffer.WriteString(c.equiped.ItemType.ToString());
    119.         }
    120.         SendData(buffer.ToArray());
    121.         buffer.Dispose();
    122.     }
    123.  
    124.     public void SendSprint(int sprinting)
    125.     {
    126.         PacketBuffer buffer = new PacketBuffer();
    127.         buffer.WriteInteger((int)ClientPackets.PlayerSprinting);
    128.         buffer.WriteInteger(sprinting);
    129.         SendData(buffer.ToArray());
    130.         buffer.Dispose();
    131.     }
    132.  
    133.     public void SendCrouch()
    134.     {
    135.         PacketBuffer buffer = new PacketBuffer();
    136.         buffer.WriteInteger((int)ClientPackets.PlayerSneaking);
    137.         SendData(buffer.ToArray());
    138.         buffer.Dispose();
    139.     }
    140.  
    141.     public void SendDash()
    142.     {
    143.         PacketBuffer buffer = new PacketBuffer();
    144.         buffer.WriteInteger((int)ClientPackets.PlayerDash);
    145.         SendData(buffer.ToArray());
    146.         buffer.Dispose();
    147.     }
    148.  
    149.     public void EquipItem(Item item)
    150.     {
    151.         PacketBuffer buffer = new PacketBuffer();
    152.         buffer.WriteInteger((int)ClientPackets.EquipItem);
    153.         buffer.WriteString(item.ItemType.ToString());
    154.         SendData(buffer.ToArray());
    155.         buffer.Dispose();
    156.     }
    157.  
    158.     public void UnequipItem()
    159.     {
    160.         PacketBuffer buffer = new PacketBuffer();
    161.         buffer.WriteInteger((int)ClientPackets.UnequipItem);
    162.         SendData(buffer.ToArray());
    163.         buffer.Dispose();
    164.     }
    165.  
    166.     public void SyncEntity(EntityType type, Vector3 location, Quaternion rotation)
    167.     {
    168.         PacketBuffer buffer = new PacketBuffer();
    169.         buffer.WriteInteger((int)ClientPackets.SyncEntity);
    170.         buffer.WriteString(type.ToString());
    171.         buffer.WriteFloat(location.x);
    172.         buffer.WriteFloat(location.y);
    173.         buffer.WriteFloat(location.z);
    174.         buffer.WriteFloat(rotation.eulerAngles.y);
    175.         SendData(buffer.ToArray());
    176.         buffer.Dispose();
    177.         Debug.Log("SyncEntity finished");
    178.     }
    179. }
    180.  
    $

    I also created some clips to show the difference when using Debug and when not using it.

    : Without Debug in front
    : With Debug in front

    The entity is only spawned when there is a response from the server. That's why no entity spawns in the without debug version.

    Greetings,
    AtlantiaKing