-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.lua
86 lines (73 loc) · 3.3 KB
/
net.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
---@meta _
--- The `net` module is used when writing online co-op compatible code.
net = {}
net.ALL = 'all' -- Sends the packet to all connected clients. `player` can be nil when using this
net.EXCLUDE = 'exclude' -- Sends the packet to all players except for the specified player
net.DIRECT = 'direct' -- Only sends the packet to the specified player
---@alias SendTarget
---| `net.ALL` Sends the packet to all connected clients. `player` can be nil when using this
---| `net.EXCLUDE` Sends the packet to all players except for the specified player
---| `net.DIRECT` Only sends the packet to the specified player
---@type boolean? Whether the game is currently online co-op.
net.online = nil
---@type boolean? When `net.online` is true indicates whether the user is the host, otherwise always true
net.host = nil
---@type PlayerInstance? The [PlayerInstance](https://saturnyoshi.gitlab.io/RoRML-Docs/class/playerInstance.html) of the local player.
net.localPlayer = nil
--- When passing RoRML types (except [NetInstance](https://saturnyoshi.gitlab.io/RoRML-Docs/class/netInstance.html)) the resource must exist on both the sender and the receiver.
--- If you cannot be sure, send the name of the resource over as a string and `find` it on the receiving end.
---
--- *WARNING*: As of the current version, passing nil as an argument to a packet will cut off any following arguments.
---
---@alias NetType
---| number
---| string
---| GMObject
---| Sprite
---| Sound
---| Item
---| NetInstance
---| boolean
---| nil
--- The `net.Packet` class is used to transfer information between clients in online co-op.
---
---@class Packet
---
---@overload fun(name: string, handle: fun(player: PlayerInstance, ...: NetType)): Packet
net.Packet = {}
--- Defines a new packet type.
---
--- Takes a function which is called when the packet is received.
---
---@param name string The name of the packet. Used to identify the packet between clients
---@param handle fun(player: PlayerInstance, ...: NetType) The function called when the packet is received. The first argument is the `PlayerInstance` of the sender, followed by arguments the packet was sent with
---@return Packet '' The new packet type
function net.Packet.new(name, handle) end
--- Sends the packet to the game host.
---
--- Does nothing if in singleplayer or if the local player is the host.
---
---@param ... NetType
function net.Packet:sendAsClient(...) end
--- Sends the packet to clients.
---
--- Does nothing if in singleplayer or if the local player is not the host.
---
---@param target SendTarget Any of the above 3 sending types
---@param player? PlayerInstance The player to send to or exclude when using the `DIRECT` or `EXCLUDE` settings respectively. *can be nil when using the `ALL` setting
---@param ... NetType Any number of valid net type arguments
function net.Packet:sendAsHost(target, player, ...) end
--- The `NetInstance` class is used to transfer instances which are already synced by the vanilla game.
---
---@class NetInstance
---@field object GMObject
---@field id Id The internal ID of the instance
---@field ID Id alias for `id`
local NetInstance = {}
--- Used to find the actual instance.
---
--- It is possible that the instance no longer exists and this will fail,
--- in which case `nil` will be returned instead.
---
---@return Instance
function NetInstance:resolve() end