Author Topic: Stuttering, dropping frames at 20 AI objects?  (Read 4592 times)

decerto

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 20
    • View Profile
Stuttering, dropping frames at 20 AI objects?
« on: September 28, 2014, 07:19:30 PM »
Hello!

I wrote a simple script that syncs AI Birds over TNet in my game. However I've noticed when there are about 20 or so birds flying in the air, the host stutters and drops frames once every 5 - 10 seconds. Profiler is pointing at the update() function that is causing the stutter, most likely the messages that get sent?

However, as I'm new to TNet and AI in general, I don't really know what is causing it. It seems I've gone about syncing up the position and rotation of the bird wrong OR TNet does not support a high number (20 or so) of objects that send messages.

Here is the script:

  1. -snip
  2.  

I understand if I decrease nextActionTime the more messages I send (which syncs up the objects faster). If I increase this number the fewer messages I'm sending. I understand the more messages I send leads to lag. However, I don't know if this is a good way of syncing up position and rotation and why TNet is stuttering on 20 or so objects OR if it's TNet not supporting that number?

Have I gone wrong about syncing up position and rotation on this script? What is the best way of handling something like this?

Thanks for your time!
« Last Edit: October 24, 2014, 08:11:40 PM by decerto »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Stuttering, dropping frames at 20 AI objects?
« Reply #1 on: September 29, 2014, 07:26:39 AM »
Stutter every few seconds means garbage collection. Look at what allocates GC in the profiler. Turn on Deep Profiling if you have to -- it will help you track down what's causing GC allocs.

decerto

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 20
    • View Profile
Re: Stuttering, dropping frames at 20 AI objects?
« Reply #2 on: September 29, 2014, 10:20:43 AM »
It comes from TNObject.Send() all the way through to TcpProtocol.SendTcpPacket(). See http://gyazo.com/4e2be9e97dc3921e3cdc13407702eced

Does this mean TNet cannot support 20 or so networked objects? How do fix this problem? I don't really know what I'm looking at.

Thanks again,

MCoburn

  • Jr. Member
  • **
  • Thank You
  • -Given: 1
  • -Receive: 7
  • Posts: 69
    • View Profile
Re: Stuttering, dropping frames at 20 AI objects?
« Reply #3 on: September 29, 2014, 10:52:25 PM »
It comes from TNObject.Send() all the way through to TcpProtocol.SendTcpPacket(). See http://gyazo.com/4e2be9e97dc3921e3cdc13407702eced

Does this mean TNet cannot support 20 or so networked objects? How do fix this problem? I don't really know what I'm looking at.

Thanks again,
I think you're spamming TCP packets, and TNet has to do a lot of work to accept them all. Try using the SendQuickly approach, which uses UDP.
TCP has to be acknowledged by the server/host before it's accepted, so if you have high latency, then you can get the stutter. UDP is "droppable", and should be used for non-critical things where you can tolerate some packet loss (ie. movement). You could also be having bandwidth limitations.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Stuttering, dropping frames at 20 AI objects?
« Reply #4 on: September 30, 2014, 04:30:38 AM »
Yeah you've got 23 Send() calls in that frame... each one allocates 468 bytes, so it quickly eats up memory and results in GC. You need to send data less frequently, and prefferably staggered out.

In my case in Windward I don't just yield return new WaitForSeconds(0.25f) or such -- I use randomization to ensure that multiple components don't send everything in the same update.
  1. yield return new WaitForSeconds(Random.Range(0.2f, 0.3f));