Page 1 of 2

Why is my program doing this?

Posted: Sat Apr 09, 2016 4:20 am
by GhostAction
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!

Re: Why is my program doing this?

Posted: Sat Apr 09, 2016 4:28 am
by Vimm
just attach the .love file, dont post all the code lol, its easier that way

Re: Why is my program doing this?

Posted: Sat Apr 09, 2016 5:49 am
by zorg
Also, if you do want to post code, use

Code: Select all

[code] code tags 
[/code]

Re: Why is my program doing this?

Posted: Sat Apr 09, 2016 4:44 pm
by GhostAction
I couldn't get it to be a .love so I just attached main.lua and the server file as well:

Re: Why is my program doing this?

Posted: Sat Apr 09, 2016 6:43 pm
by Vimm
GhostAction wrote:I couldn't get it to be a .love so I just attached main.lua and the server file as well:
what do you mean you couldnt get it to be a .love? you just put the files into a zip and then rename the .zip to .love

Re: Why is my program doing this?

Posted: Sat Apr 09, 2016 6:55 pm
by GhostAction
I have winrar so yeah... but is it that hard to put it into a folder?

Re: Why is my program doing this?

Posted: Sat Apr 09, 2016 7:05 pm
by s-ol
GhostAction wrote:I have winrar so yeah...
you know you can zip files with winrar too right?
but is it that hard to put it into a folder?
Of course not. But you are asking us for help, and providing .love's is a forum "rule". Not only does a .love make it easier for us to help you (which is in your interest), it's also a bit of courtesy in a way.

By giving us a .love you make sure we have the exact files you are running, including misplaced images or scripts, typos and without copy and pasting errors. All of those things often amount to problems people have that we can't tell they have otherwise for example.

Re: Why is my program doing this?

Posted: Sat Apr 09, 2016 7:10 pm
by GhostAction
So how do you zip it with winrar then?

Re: Why is my program doing this?

Posted: Sat Apr 09, 2016 7:12 pm
by GhostAction
I'm just going to uninstall winrar.

Re: Why is my program doing this?

Posted: Sat Apr 09, 2016 7:14 pm
by s-ol
GhostAction wrote:I'm just going to uninstall winrar.
Select the files. right click -> add to archive. Choose zip, change name to .love. As far as I remember, not on windows atm.

How will uninstalling winrar help? That's just one tool less that can accomplish the task. If you don't get along with winrar you will at least need to install something else....?