network_send() - How does it work?

Spring E. Thing

Member
Modder
Apr 8, 2024
41
5
230
Code:
local function checkSend(m)
    if(m.playerIndex == 0 and (m.controller.buttonPressed & A_BUTTON ~=0)) then
        network_send(true,{sig="testSig",msg="you have received my packet"})
    end
end
local function checkReceive(packet)
    log_to_console("well SOMETHING was sent")
    if(packet["sig"] == "testSig") then
        log_to_console(packet["msg"])
    end
end
hook_event(HOOK_MARIO_UPDATE,checkSend)
hook_event(HOOK_ON_PACKET_RECEIVE,checkReceive)
I've got this block of code here I used earlier today to test my understanding of the network_send() method. Whatever my understanding is, it seems to be wrong. In this code, when a local player presses the A button, I expect checkReceive() to occur at minimum once for every player in the server and at maximum MAX_PLAYERS times (not sure which currently). If the check of the sig key's value in the table of the packet sent to checkReceive() fails, I'll at least know the hook worked based on the log_to_console() outside of that check. Additionally, I set the reliable bool parameter of the network_send() call to true so that I can be sure the packet sends lest the server desync. To my confusion, when the A button is pressed, no packet is sent based upon reading the in-game console as nothing gets logged.

I have two theories as to why:
1. I'm testing this .lua in a server alone, perhaps network_send() goes out to every player EXCEPT the one who sent it? The documentation for Co-Op DX did not mention this if it's true. This way, I'd simply never noticed in a 1-player server if a packet was sent since the only one to receive a packet would be the sender themselves who by definition in this theory cannot receive the packets they send.
2. I'm playing on a bogus server that nobody can join via Direct Connection. This means that technically it can never be anything but a singleplayer game and perhaps networking capabilities are failing due to the bogus server config? This one seems far less likely to me than my first theory, but I thought I'd bring it up anyway.

Any ideas guys? Many thanks : )
 
Solution
Doing ["sig"] is incorrect, it would be packet.sig.
I'm playing on a bogus server that nobody can join via Direct Connection. This means that technically it can never be anything but a singleplayer game and perhaps networking capabilities are failing due to the bogus server config?
Thats fine, not the problem.

Make sure you have another client open, you can't send packets to yourself.
Doing ["sig"] is incorrect, it would be packet.sig.
I'm playing on a bogus server that nobody can join via Direct Connection. This means that technically it can never be anything but a singleplayer game and perhaps networking capabilities are failing due to the bogus server config?
Thats fine, not the problem.

Make sure you have another client open, you can't send packets to yourself.
 
  • Like
Reactions: Spring E. Thing
Solution
Doing ["sig"] is incorrect, it would be packet.sig.
Ah, gotcha, makes sense, guess I'm just not used to Lua enough yet. In my mind, tables are very much analogous to a Dictionary<>, and this is how I'd access a Dictionary<> in something like C#.
Make sure you have another client open, you can't send packets to yourself.
Yeah, after changing things like packet["sig"] to be packet.sig, I'm still not receiving packets, and this must be why with my first theory being correct. What if I want something networked to change for every player including the sender, do I just have to manage the sender's data to be in-sync with the packets everyone else is receiving? For example, I want everyone in the server to receive some packet with a luigi key set to 5 and then set some local variable to the value of the luigi key when they get the packet. Would I have to set the luigi key to 5 for the sender separate to the network_send() call?
 
Firstly, sounds like you should use gGlobalSyncTable, that syncs all variables that are in there

Example usage:
gGlobalSyncTable.syncedVar = false

-- automatically syncs
gGlobalSyncTable.syncedVar = true

perhaps network_send() goes out to every player EXCEPT the one who sent it
This is correct. A easy way to fix this is the very next line down, run checkReceive(dataTableHere). Also, the data sent thru network_send should be put into a variable called p, as that is what it is in the source code, and helps readability.

For your case, you would send a packet like:

local p = {key = "luigi", value = 5}
network_send(p, true)

Then receive it like

if p.key == "luigi" then
luigi = p.value
end

Although, this sounds like the use case for gGlobalSyncTable. I'd recommend taking a look at gPlayerSyncTable and gGlobalSyncTable.
 
  • Like
Reactions: Spring E. Thing

Users who are viewing this thread