• Networking functions
    4 replies, posted
I have some code that does clever stamina calculations where I only network constants that feed into some equation to cut back on what needs to be networked. I want to know if anyone has managed to network functions themselves, short of sending over lua code as a string and using CompileString() or something. If I do go the CompileString() route, are there any way to check (via assert() or something) to make sure the function is ONLY a function of time, and doesn't rely on other closures or global variables? My (pseudo) code is as follows: whatever.lua if SERVER then -- Server to player, updates inputs for the functions util.AddNetworkString("update_stamina") -- Player to server, use up some stamina util.AddNetworkString("use_stamina") -- Table of stamina for each player local staminas = {} net.Receive("use_stamina",function(ply,len) staminas[ply] = staminas[ply] or 100 if staminas[ply] >= 10 then print("You used some stamina!") staminas[ply] = staminas[ply] - 10 else -- Player didn't have enough stamina return end net.Start("update_stamina") net.WriteUInt(staminas[ply],16) net.WriteDouble(CurTime()) net.Send(ply) end) else concommand.Add("use_stamina",function(ply,cmd,args) net.Start("use_stamina") net.WriteUInt(10,16) net.SendToServer() end) local lastupdate = 0 local lastupdatetime = 0 net.Receive("update_stamina",function() lastupdate = net.ReadUInt(16) lastupdatetime = net.ReadDouble() end) hook.Add("tick","print_stamina",function() local stamina = lastupdate + (CurTime() - lastupdatetime) print("Stamina is now", math.Clamp(stamina,0,100)) end) end
You can't network functions due networking concepts, what instructions set would you send over a net message, you only can send values, and if you worry that much about optimization, you should despite the idea about sending a string and the compiling it A much better solution would be using SetNW2Float or SetNWFloat, and you'll be okay with that
Why should I "despise" the idea of sending a string over the network and compiling it? It seems to me I only need to send the function once, then the inputs to the function after that. I can't just use SetNW2Float or whatever because it doesn't allow for arbitrary functions. What if I decide there should be a delay before the stamina starts regenerating? What if I add upgrades for regeneration rate or maximum stamina? I would have to change the above code. What I'm asking for is a way to decouple the function from the code used to network the function. Or at least a way to check that a function is network-able.
Because it's a symptom of bad design. As la_grib said, why not just have the function shared? The NW function is also an option if you don't mind players reading each others' stamina and you want a quick and easy solution. And what you said about SetNW2Float doesn't make any sense. The point of that function isn't to network your logic, it's to network the result of your logic.
Sorry, you need to Log In to post a reply to this thread.