Online game

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
NikitaPupy
Prole
Posts: 2
Joined: Sun Jun 02, 2024 8:09 pm

Online game

Post 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?
User avatar
keharriso
Party member
Posts: 109
Joined: Fri Nov 16, 2012 9:34 pm

Re: Online game

Post by keharriso »

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
User avatar
dusoft
Party member
Posts: 611
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Online game

Post by dusoft »

You might use love.thread module.
NikitaPupy
Prole
Posts: 2
Joined: Sun Jun 02, 2024 8:09 pm

Re: Online game

Post 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"
User avatar
dusoft
Party member
Posts: 611
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Online game

Post 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.
RNavega
Party member
Posts: 340
Joined: Sun Aug 16, 2020 1:28 pm

Re: Online game

Post by RNavega »

For real-time communication between a client (the players) and server (you), there's the WebSocket specification.
Some links:
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests