Why is my program doing this?
Posted: Sat Apr 09, 2016 4:20 am
So I'm trying to make a multiplayer game follow this guide: https://love2d.org/wiki/Tutorial:Networ ... -TheClient
For some reason whenever start up a client, its getting data from the user that joined before it and itself, not getting data from later joiners. If you don't understand, just copy and paste and try it out.
Here is the client code:
--Core Functions Load First
--Console
local consoleText = ""
local rounds = 10
function console(text)
rounds = rounds + 1
consoleText = text..string.char(10)..consoleText
if rounds > 40 then
consoleText = ""
rounds = 0
end
end
--Client
local width = love.graphics.getWidth()
local height = love.graphics.getHeight()
local state = "load"
local frames = 0
--Server
local socket = require "socket"
local address = "localhost"
local port = 2001
udp = socket.udp()
udp:settimeout(0)
udp:setpeername(address, port)
local world = {}
--Player
local playerName = os.time()
playerPositionX = 0
playerPositionY = 0
--Client Player
local CamPositionX = 395
local CamPositionY = 295
local keyFowardUp = false
local keyBackwardsUp = false
local keyLeftUp = false
local KeyRightUp = false
console("connecting to "..address..":"..port)
function love.update()
--Receiving Data from Server
local receivedData = udp:receive()
if receivedData then
local command = {}
console(receivedData)
for i in string.gmatch(receivedData, "%S+") do
table.insert(command, i)
end
if command[1] == "updatePos" then
world[command[2]] = {posX = command[3], posY = command[4]}
--[[for a, b in pairs(world) do
console(a.." X:"..b.posX.." Y:"..b.posY)
end]]--
end
command = {}
end
--Send Update Command to Server
udp:send("updatePos "..playerName.." "..playerPositionX.." "..playerPositionY)
end
function love.draw()
love.graphics.print(consoleText, 0, 0)
end
And the server code:
--Server
local socket = require "socket"
local udp = socket.udp()
udp:settimeout(0)
udp:setsockname("*", 2001)
local world = {}
local data, msg_or_ip, port_or_nil
local player, cmd, parms
local command = {}
local running = true
while running do
data, msg_or_ip, port_or_nil = udp:receivefrom()
if data then
for i in string.gmatch(data, "%S+") do
table.insert(command, i)
end
if command[1] == "updatePos" then
--print("Updating Position for "..msg_or_ip..":"..port_or_nil)
world[command[2]] = {posX = command[3], posY = command[4], ip = msg_or_ip, port = port_or_nil}
end
command = {}
end
for _, c in pairs(world) do
for a, b in pairs(world) do
print("Sending to "..c.ip..":"..c.port.." "..b.ip..":"..b.port.." info a:"..a)
udp:sendto("updatePos "..a.." "..b.posX.." "..b.posY, c.ip, c.port)
end
end
end
Thanks!
For some reason whenever start up a client, its getting data from the user that joined before it and itself, not getting data from later joiners. If you don't understand, just copy and paste and try it out.
Here is the client code:
--Core Functions Load First
--Console
local consoleText = ""
local rounds = 10
function console(text)
rounds = rounds + 1
consoleText = text..string.char(10)..consoleText
if rounds > 40 then
consoleText = ""
rounds = 0
end
end
--Client
local width = love.graphics.getWidth()
local height = love.graphics.getHeight()
local state = "load"
local frames = 0
--Server
local socket = require "socket"
local address = "localhost"
local port = 2001
udp = socket.udp()
udp:settimeout(0)
udp:setpeername(address, port)
local world = {}
--Player
local playerName = os.time()
playerPositionX = 0
playerPositionY = 0
--Client Player
local CamPositionX = 395
local CamPositionY = 295
local keyFowardUp = false
local keyBackwardsUp = false
local keyLeftUp = false
local KeyRightUp = false
console("connecting to "..address..":"..port)
function love.update()
--Receiving Data from Server
local receivedData = udp:receive()
if receivedData then
local command = {}
console(receivedData)
for i in string.gmatch(receivedData, "%S+") do
table.insert(command, i)
end
if command[1] == "updatePos" then
world[command[2]] = {posX = command[3], posY = command[4]}
--[[for a, b in pairs(world) do
console(a.." X:"..b.posX.." Y:"..b.posY)
end]]--
end
command = {}
end
--Send Update Command to Server
udp:send("updatePos "..playerName.." "..playerPositionX.." "..playerPositionY)
end
function love.draw()
love.graphics.print(consoleText, 0, 0)
end
And the server code:
--Server
local socket = require "socket"
local udp = socket.udp()
udp:settimeout(0)
udp:setsockname("*", 2001)
local world = {}
local data, msg_or_ip, port_or_nil
local player, cmd, parms
local command = {}
local running = true
while running do
data, msg_or_ip, port_or_nil = udp:receivefrom()
if data then
for i in string.gmatch(data, "%S+") do
table.insert(command, i)
end
if command[1] == "updatePos" then
--print("Updating Position for "..msg_or_ip..":"..port_or_nil)
world[command[2]] = {posX = command[3], posY = command[4], ip = msg_or_ip, port = port_or_nil}
end
command = {}
end
for _, c in pairs(world) do
for a, b in pairs(world) do
print("Sending to "..c.ip..":"..c.port.." "..b.ip..":"..b.port.." info a:"..a)
udp:sendto("updatePos "..a.." "..b.posX.." "..b.posY, c.ip, c.port)
end
end
end
Thanks!