How does the NetVars hook work?

Status
Not open for further replies.

amperbee

thunderdome denizen
Retired Staff
I read the wiki entry for the hook NetVars and the function you can use in the hook ( network(function? network) ), but I don't completely understand how does it work.

What does it do? How and when do I have to call network()?
If it handles both sides of networking, how do I know I'm telling the server to send a variable's value to a client and not viceversa?
There are more questions I have about it, I think, but these are the only ones that I find important.
 
Ok, let me try to explain: the network argument is passed in a different function for each side of the NetVars hook.

When the server is creating the network variables, network(var) pushes var onto the queue of variables to send off. It then returns that same variable for a reason I'll get to shortly.

When the client is receiving the network variables, network() pulls the first variable from the queue and returns it. I don't think it necessarily takes an argument, but it won't hurt anything to pass one in.

Therefore, the proper use of the function is variable = network(variable). This will store the variable on the server's side, and read it on the client's side, seamlessly. Due to the way this works, it's important that the type and order of variables read is exactly the same on both sides; if that type/order can vary in any way, you need to store that information inside the variable queue itself. For instance, if you need to store a variable amount of numbers for, say, a list of scores on a map:

Code:
local scores = {} -- Example list, pretend it gets filled in elsewhere in the script

addHook("NetVars", function(network)
    local scoreCount = #scores -- This will get overridden shortly for the client, so it's ok
    scoreCount = network(scoreCount)
    for i = 1, scoreCount do
        scores[i] = network(scores[i])
    end
end)
You can apply similar logic to send other kinds of lists/tables/etc. I don't recall exactly what kinds of variables can be sent across the NetVars hook (maybe the wiki lists it? I've never looked up the hook because it was broken the last time I needed to use it) but be aware that some things might not be directly transferrable. In those cases you'll have to get creative.

Also note that variables inside player/mobj structs are networked automatically, so you only need the NetVars hook for variables that aren't stored in one of those.
 
I don't recall exactly what kinds of variables can be sent across the NetVars hook (maybe the wiki lists it? I've never looked up the hook because it was broken the last time I needed to use it) but be aware that some things might not be directly transferrable. In those cases you'll have to get creative.

Nah the wiki doesn't go into detail on that part currently, but quickly following the source code most types of variables can be sent/recieved with notable exceptions to:
  • "light" userdata (not to be confused with normal userdata, very few instances you'd ever encounter these in SRB2's Lua so it's not a problem),
  • threads (not sure when this is relevant to SRB2's Lua offhand),
  • and functions.
Though it should be noted that only certain types of userdata can be sent via NetVars as of 2.1.16:
  • mobjinfo_t
  • state_t
  • mobj_t
  • player_t
  • mapthing_t
  • vertex_t
  • line_t
  • side_t
  • subsector_t
  • sector_t
  • mapheader_t
Any userdata types not listed above (including special sub-userdata types for arrays like player.powers, line.sidenum etc.) as well as the other exceptions I listed will make the game spit out a console error saying they can't be archived (in other words, you can't send them).

I think all of this also applies to custom variables for mobj_t and player_t, btw.
 
Status
Not open for further replies.

Who is viewing this thread (Total: 1, Members: 0, Guests: 1)

Back
Top