Module Server
Keeps clients up to date by sending messages back and forth
Info:
- Release: 0.0.1
- Author: Will
| Network |
Stores network variables and callbacks |
-
start (port)
-
First call, used to initialize the server. Sets up UDP to listen for clients
Parameters:
- port
number
Port number to host server on
-
on (cmd, callback)
-
Used to set callbacks which are called when the clients send a certain command.
The function can return a string and a table as a parameter, which will then be broadcasted
to all players on the server.
Parameters:
- cmd
string
The command to listen for. Ex: 'update'
- callback
function
The function called when this command arrives
-
send (ip, port, cmd, params, playerID)
-
Sends a message to the clients
Parameters:
- ip
string
IP to send the command to
- port
number
Port to send the command to
- cmd
string
The command to send. This should be the same as the listener on the client side
- params
table
Parameters to send to the listeners. It will be serialized to a string and deserialized later
- playerID
number
The ID of the player sending the command
-
broadcast (cmd, params, playerID)
-
Send a message from one player to every player on the server
Parameters:
- cmd
string
The command to send. This should be the same as the listener on the client side
- params
table
Parameters to send to the listeners. It will be serialized to a string and deserialized later
- playerID
number
The ID of the player sending the command
-
instantiate (params, senderID)
-
Spawn an object accross all clients
Parameters:
- params
table
Parameters to send to the listeners. It will be serialized to a string and deserialized later
- senderID
number
The ID of the player sending the command
-
update ()
-
Update the network. Loops through all messages, handling them by calling their callbacks
-
Deserialize (ser)
-
Deserialize a string by running it with Lua
Parameters:
-
newID ()
-
Create a new ID used for either entities or players. The ID is unique and ranges from [0, 999999]
Returns:
number The newly created ID
-
log (str)
-
Print to the command line and log to a file
Parameters:
-
Network
-
Stores network variables and callbacks
Fields:
- udp
The interface with the clients
- callbacks
A table filled with callbacks that the user creates
- id
The ID assigned to the server. Default: -1
- players
A table filled with player objects
- numPlayers
Keeps track of the number of players connected to the server
- entities
Keeps track of any entities spawned into the game with instantiate