1
votes

I building a little game with a multiplayer, my problem is when I sending the bullets is there is a lag when it's exceeding the amount about 80.

I using UDP type, my connect code to the server:

udp = socket.udp()
udp:settimeout(0)
udp:setpeername(address, port)

My udp:send of the bullet to the server:

udp:send('%S03'..startX..','..startY..','..bulletAngleX..','..bulletAngleY)

Server: retrieving the bullets and sending them back to the rest of the clients:

    elseif code == '%S03' then

        local bulleta = string.gmatch(params, "[^,]+")

        local sX = tonumber(bulleta())
        local sY = tonumber(bulleta())
        local dX = tonumber(bulleta())
        local dY = tonumber(bulleta())


        for i,v in ipairs(clients) do
            udp:sendto('%C01'..math.random(120, 200)..','..sX..','..sY..','..dX..','..dY, v['ip'], tonumber(v['port']))
        end
    end

Client: getting the bullets data and creating them in a table:

        elseif code == '%C01' then
        local xy = string.gmatch(re, "[^,]+")
        local dis = tonumber(xy())
        local xStart = tonumber(xy())
        local yStart = tonumber(xy())
        local xAngle = tonumber(xy())
        local yAngle = tonumber(xy())
        table.insert(bullets, {distance = dis, sX = xStart, sY = yStart, x = xStart, y = yStart, dx = xAngle, dy = yAngle})

The updating of the x and y cordition of the bullets is happening in the client when he gets the bullets x, y and removing the bullets when they distance are more then 300 pixel from the first position.

But my problem is still that there is lags when I am shooting..

1

1 Answers

0
votes

I'm not extremely familiar with networking details, however it is quite likely you are simply sending too many packets, and need to bundle or otherwise compress the data you send to reduce the overall number of UDP messages you are sending.

From this Love2d networking tutorial (also using UDP):

It's very easy to completely saturate a network connection if you aren't careful with the packets we send (or request!), so we hedge our chances by limiting how often we send (and request) updates.

(For the record, ten times a second is considered good for most normal games (including many MMOs), and you shouldn't ever really need more than 30 updates a second, even for fast-paced games.)

We could send updates for every little move, but we'll consolidate the last update-worth here into a single packet, drastically reducing our bandwidth use.

I can't confirm if this is the problem without seeing all/more of your code. However, sending the data for each bullet separately, to each client, many times per second, the bandwidth usage could become very high, most notably sending the data for each bullet separately.

I would first try bundling the data for every bullet together before sending the bundled data to each client, which will greatly reduce the number of individual packets being sent out. Also, in case you aren't, make sure you are not sending the packets in love.update, which is called very, very often. Instead, make a seperate function for updating over the network and use timers to call it only about once every 100ms.

Let me know if any of this is already being accounted for in the code, or show us a larger context of your networking code.