Hi, I am attempting to make my netcode as efficient as possible. It is being used to update the state of each player in game. Here is what I'm currently doing.
While this.udp.isReadable()
received = this.udp.receive(this.buffer, 0, 20)
if received > 0
params = {}
params.id = resolve_two_byte_integer(this.buffer[12],this.buffer[13])
params.x = resolve_two_byte_integer(this.buffer[14],this.buffer[15])
params.y = resolve_two_byte_integer(this.buffer[16],this.buffer[17])
params.color = this.buffer[18]
params.destroy = this.buffer[19]
id_exists = false
for a = 0 to this.players.Count()-1
if this.players[a].id = params.id
this.players[a].x = params.x
this.players[a].y = params.y
if params.destroy = 1
print "Removed player - id : " ; this.players[a].id
this.players[a].sprite.Remove()
this.players.Delete(a)
end if
id_exists = true
exit for
end if
end for
if not id_exists
params.sprite = this.compositor.NewSprite(270, 500, this.region_ring[params.color])
params.sprite.SetData(params.id)
this.players.Push(params)
end if
end if
End While
And here is the "resolve_two_byte_integer()" function
Function resolve_two_byte_integer(byte_a, byte_b)
value = byte_a + (byte_b * 256)
return value
End Function
It currently takes 20-40ms with 20 active players using Roku 1 (slowest device I have). I know odds are there won't even be this many players, but I'd like to just make sure I have it as efficient as possible.
Thanks!