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?
Online game
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Online game
Networking is complicated. It's hard to diagnose the problem without some sample code.
LÖVE-Nuklear - a lightweight immediate mode GUI for LÖVE games
Re: Online game
You might use love.thread module.
My boat driving game demo: https://dusoft.itch.io/captain-bradley- ... itius-demo
-
- Prole
- Posts: 2
- Joined: Sun Jun 02, 2024 8:09 pm
Re: Online game
Now i have this code, i have fixed it a little, because networking function didnt even run
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"
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
Re: Online game
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.
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.
My boat driving game demo: https://dusoft.itch.io/captain-bradley- ... itius-demo
Re: Online game
For real-time communication between a client (the players) and server (you), there's the WebSocket specification.
Some links:
Some links:
- https://github.com/flaribbit/love2d-lua-websocket (thanks @flaribbit)
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 3 guests