SRB2 Message Board  

Go Back   SRB2 Message Board > Sonic Robo Blast 2 > Editing > Editing Help

Reply
 
Thread Tools Search this Thread
Old 09-12-2016   #1
Rapidgame7
Rookie modder
 
Rapidgame7's Avatar
Default How does the NetVars hook work?

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.
__________________
Lost interest on modding for a while...
Rapidgame7 is offline   Reply With Quote
Old 09-13-2016   #2
fickleheart
ms reflec beat stan
 
fickleheart's Avatar
Default

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.
__________________
OK Google, define "hubris".
fickleheart is offline   Reply With Quote
Old 09-13-2016   #3
Monster Iestyn
Friendly Neighbour-Hood
Moderator
 
Monster Iestyn's Avatar
Default

Quote:
Originally Posted by RedEnchilada View Post
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.
__________________
My page stuffed full of MIDI goodness!
The Hitchhiker's Guide to the Robo-Hoodiverse
Timeline of Sonic Robo Blast History!

That's an uppercase i, not a lowercase L, for the record. Also, it's pronounced "Yes-tin".
Monster Iestyn is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 01:16 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.