This is a somewhat difficult question to answer, since it deals with networking, but I'm trying to make a basic multiplayer game. I followed this tutorial https://rvagamejams.com/learn2love/page ... art-1.html for the networking setup and movement, but the movement and entities weren't physics based. Therefore, I tried to add physics on my own. The movement now works for window the player is controlling and the host is receiving the messages as you can see here: However, when I have both windows side by side, the movement is only being seen by the player itself. For all the other windows the movement is not synced, even though they are receiving the messages with the player's position. Here is my entity code:
Code: Select all
local input = require('input')
local entity = {}
entity.entities = {} --All entities
entity.spawn = function(id, x, y)
local sprites = {
blueSprite,
yellowSprite,
redSprite,
purpleSprite,
}
local body = love.physics.newBody(world, x, y, "dynamic")
local shape = love.physics.newCircleShape(100)
return {
sprite = sprites[(tonumber(id) % #sprites) + 1],
id = id,
x = x,
y = y,
body = body,
shape = shape,
fixture = love.physics.newFixture(body, shape)
}
end
entity.draw = function(entity)
love.graphics.setColor(1, 1, 1, 1)
love.graphics.draw(entity.sprite, entity.body:getX(), entity.body:getY(), nil, 0.5, 0.5)
end
entity.move = function()
local player = entity.entities[entity.id]
if input.right then --press the right arrow key to push the ball to the right
player.body:applyForce(400, 0)
elseif input.left then --press the left arrow key to push the ball to the left
player.body:applyForce(-400, 0)
elseif input.up then --press the up arrow key to set the ball in the air
player.body:applyLinearImpulse(0, -500)
end
player.x = player.body:getX()
player.y = player.body:getY()
end
return entity
Code: Select all
local message_handlers = {
['move'] = function(message, event, is_server)
local player_id = message[2]
local x_pos = message[3]
local y_pos = message[4]
entity.entities[player_id].x = x_pos
entity.entities[player_id].y = y_pos
if is_server then
-- Relay this message to the other players
for id, peer in pairs(peers) do
if id ~= player_id then
peer:send(event.data)
end
end
end
end
}
local event_handlers = {
connect = function(event, is_server)
--Only server needs to do stuff during connect
if is_server then
local player = entity.spawn(tostring(event.peer:connect_id()), 100, 100) --connect_id() creates a number as an id
entity.entities[player.id] = player --Store this player in the player table with the player ID as the key.
event.peer:send('your-id|' .. player.id .. '|' .. player.x .. '|' .. player.y) -- Send the initial "your-id" message back to the connecting client so they can spawn this entity too.
-- Let all the other peers know about this player
for _, peer in pairs(peers) do
local peer_player = entity.entities[tostring(peer:connect_id())]
peer:send('peer-id|' .. player.id .. '|' .. player.x .. '|' .. player.y)
event.peer:send('peer-id|' .. peer_player.id .. '|' .. player.x .. '|' .. player.y)
end
peers[tostring(event.peer:connect_id())] = event.peer --Add this peer to the peer list
end
end,
}
Code: Select all
if entity.id then
local player = entity.entities[entity.id]
local old_x = player.x
local old_y = player.y
entity.move()
if player.x ~= old_x or player.x ~= old_y then
server.send('move|' .. player.id .. '|' .. player.x .. '|' .. player.y)
end
end
server.update()
world:update(dt)
I understand that this is quite a hard problem to track down, since I've been trying to figure it out myself for a few days now
Thanks for any help though!! I really appreciate it.