Page 1 of 1
Online game
Posted: Sun Jun 02, 2024 8:14 pm
by NikitaPupy
now i am making an online game, and i have this bug:
function that sends data on server and gets a response is taking a while to continue script and return those values, but if i do it every frame, game gets very laggy, i have like 2 fps in the game, how do i place this function in another loop, so it would work outside of love.update?
Re: Online game
Posted: Sun Jun 02, 2024 11:30 pm
by keharriso
Networking is complicated. It's hard to diagnose the problem without some sample code.
Re: Online game
Posted: Mon Jun 03, 2024 11:53 am
by dusoft
You might use love.thread module.
Re: Online game
Posted: Mon Jun 03, 2024 2:33 pm
by NikitaPupy
Now i have this code, i have fixed it a little, because networking function didnt even run
Code: Select all
require("json")
local http = require("socket.http")
local ltn12 = require("ltn12")
local threadChannel = love.thread.getChannel("threadChannel")
function sendData(params)
print("A")
local serverUrl = "http://pixelhogstudios.com/testserver/"
local fullUrl = serverUrl .. "?" .. params
local response = {}
local result, statusCode, headers = http.request{
url = fullUrl,
sink = ltn12.sink.table(response)
}
if statusCode == 200 then
responseData = table.concat(response)
threadChannel:push(responseData)
return response[1]
else
print("Error")
threadChannel:push(nil)
end
end
print(sendData("operation=movement&playerId=1&x=5&y=190"))
player1 = {}
player1.x = 100
player1.y = 100
player2 = {}
player2.x = 100
player2.y = 100
playerId = 0
speed = 5
function networking()
while true do
if playerId == 1 then
local encodedData = sendData("operation=movement&playerId=" .. playerId .. "&x=" .. player1.x .. "&y=" .. player1.y)
local decodedString = json.decode(encodedData)
print(decodedString.x)
player2.x = decodedString.x
player2.y = decodedString.y
elseif playerId == 2 then
local encodedData = sendData("operation=movement&playerId=" .. playerId .. "&x=" .. player1.x .. "&y=" .. player1.y)
local decodedString = json.decode(encodedData)
print(decodedString.x)
player1.x = decodedString.x
player1.y = decodedString.y
end
coroutine.yield()
end
end
function mainLoop()
while true do
if playerId == 0 then
if love.keyboard.isDown("1") then
playerId = 1
if love.keyboard.isDown("r") then
sendData("operation=registration&playerId=1")
else
sendData("operation=login&playerId=1")
end
elseif love.keyboard.isDown("2") then
playerId = 2
if love.keyboard.isDown("r") then
sendData("operation=registration&playerId=2")
else
sendData("operation=login&playerId=2")
end
end
elseif playerId == 1 then
if love.keyboard.isDown("w") then
player1.y = player1.y - speed
end
if love.keyboard.isDown("a") then
player1.x = player1.x - speed
end
if love.keyboard.isDown("s") then
player1.y = player1.y + speed
end
if love.keyboard.isDown("d") then
player1.x = player1.x + speed
end
elseif playerId == 2 then
if love.keyboard.isDown("w") then
player2.y = player2.y - speed
end
if love.keyboard.isDown("a") then
player2.x = player2.x - speed
end
if love.keyboard.isDown("s") then
player2.y = player2.y + speed
end
if love.keyboard.isDown("d") then
player2.x = player2.x + speed
end
end
coroutine.yield()
end
end
function love.update(dt)
coroutine.resume(coroutine.create(networking))
coroutine.resume(coroutine.create(mainLoop))
end
function love.draw()
love.graphics.rectangle("fill", player1.x, player1.y, 100, 100)
love.graphics.rectangle("fill", player2.x, player2.y, 100, 100)
end
but the game is very laggy, and i dont get decodedString.x print at all, so does the player position exchanging, i also get prints with text "A"
Re: Online game
Posted: Mon Jun 03, 2024 8:15 pm
by dusoft
I understand you are sending update with each position (pixel) change. That's a very naive approach and might either kill your server and/or lag your client.
You might read some tutorials on how to do some client-side prediction instead of sending every pixel change:
https://kinematicsoup.com/news/2017/5/3 ... prediction
Good luck.
Re: Online game
Posted: Tue Jun 04, 2024 10:00 pm
by RNavega
For real-time communication between a client (the players) and server (you), there's the WebSocket specification.
Some links: