I am attempting to make a multiplayer game and would like some feedback on my ideas & answers to some questions.
My goal is to make the following game have multiplayer functionality (here is a video: https://streamable.com/e/m9wxy6). All the files you need to run the game are attached.
So far the game runs only on one computer, I instantiated a world and create physics bodies where a player can control themselves in it, they also have an aiming direction.
----------------------------------------------------------------------------------------------------------------------
I started by reading the lua-enet examples: https://love2d.org/wiki/lua-enet and realized that the method which sends the data (https://love2d.org/wiki/enet.peer:send) only allows sending strings.
So in my code you will find the method for a player called inputStateAsText which converts the players input into a string with the following format
|xDir|yDir|rotationAngle|
xDir = 1 implies positive x movement, xDir = -1 implies negative x movement, 0 is nothing, same concept for yDir
rotationAngle is the angle of the aiming line.
----------------------------------------------------------------------------------------------------------------------
My idea is to send this information to the server when the update function is called, let the server do the physics calculation based on their current inputs and then return where there new position should be. (Eventually also say if someone has gotten hit/died/etc... - but that's down the line)
Note: I am aware that eventually some type of interpolation should be added to smooth out the positions that are returned from the server so that it's not all jumpy, but for now, I just want something basic to work.
----------------------------------------------------------------------------------------------------------------------
I want to understand how to design the server now. So if I have N concurrent players all sending their data to the server, I will need a way determine who sent what info, so I can update their position only and not some other players accidentally, how would you recommend I do this? (1)
----------------------------------------------------------------------------------------------------------------------
Based on the server example code:
Code: Select all
-- server.lua
local enet = require "enet"
local host = enet.host_create("localhost:6789")
while true do
local event = host:service(100)
while event do
if event.type == "receive" then
print("Got message: ", event.data, event.peer)
event.peer:send( "pong" )
elseif event.type == "connect" then
print(event.peer, "connected.")
elseif event.type == "disconnect" then
print(event.peer, "disconnected.")
end
event = host:service()
end
end
----------------------------------------------------------------------------------------------------------------------
In my mind when you boot up the server, the following things should happen:
- Instantiate the physics world
- Start an infinite while loop
- If a new player joins, then make them their own player object
- Each players client will be calling the at different moments in time, which sends a packet of their input information to the server
Code: Select all
love.update(dt)
- The moment the packet is received by the server - I need to apply physics based on their movement and then send back their new position
- I am also realizing that I need to somehow timestamp when these inputs are received, as if I process them in some update function on the server, then it would be processing them as if they had all occured at the same time - which would make dodging and other movement unfair if you reacted first. How would you deal with this? (3) (maybe this doesn't matter because the delta time is small enough?)
- The moment the packet is received by the server - I need to apply physics based on their movement and then send back their new position
----------------------------------------------------------------------------------------------------------------------
So far I have read this discussion: https://love2d.org/forums/viewtopic.php?t=84776
@zorg mentions:
So I know that love.update is involved on the server in some way or another, but other than that I am a little lost.although you need to modify the infinite loops since löve already has love.update you should use.
Thanks in advance.