• Serverside/Clientside Variables.
    12 replies, posted
Say I have the following server-side: [code]var = "string";[/code] Then client-side I do: [code]print (var);[/code] What would be the easiest way to make it print that client-side instead of resulting in nil? Could I just put something like this in a shared lua file so it reads the server-side variable and makes it so it can be seen client-side? [code]var = var;[/code] I'm working on something much more complex then that but I just need a simple understanding how to get the client to see a function, variable, etc that is server-side.
Serverside you could send it to the client with SendLua or a usermessage. function SendIt(Player) Player:SendLua([[var = "string"]]); end or you could do it directly. Player:SendLua([[print(]]..var..[[)]]);
[QUOTE=Drew P. Richard;25474023]Serverside you could send it to the client with SendLua or a usermessage. function SendIt(Player) Player:SendLua([[var = "string"]]); end or you could do it directly. Player:SendLua([[print(]]..var..[[)]]);[/QUOTE] Awesome, got it working. Thanks.
Anytime.
Using SendLua all the time is bad since it uses unnecassary network performence. Try to use usermessages only.
or you can stop being lazy Assuming the value is always a string. Serverside: [lua] function SendValue(player, value) umsg.Start("get_var", player); umsg.String(value); umsg.End(); end; [/lua] Clientside: [lua] usermessage.Hook("get_var", function(um) local value = um:ReadString(); print(value); end); [/lua]
I just found an old script I made once for people who did not get usermessages too good. [lua]if SERVER then function SendVar(name,val,ply) local t = type(val) local msgtype local id if(t == "number") if(math.floor(val) == val) then if (val >= -128 and val <= 127) then msgtype = "short" id = 0 elseif(val >= -32,768 and val <= 32,767) then msgtype = "char" id = 1 elseif(val >= -2.147.483.648 and val <= 2.147.483.647) then msgtype = "long" id = 2 end else msgtype = "float" id = 3 end elseif(t == "string") then msgtype = t id = 4 elseif(t == "angle") then msgtype = t id = 5 elseif(t == "vector") msgtype = t id = 6 elseif(t == "bool") then msgtype = t id = 7 elseif(t == "entity") then msgtype = t id = 8 end umsg.Start("SendVar",ply) umsg.Char(id) umsg[msgtype](val) umsg.String(name) umsg.End() end else SendVars = {} local function SendVarRec(um) local msgtype = um:ReadChar() local val if(msgtype == 0) then val = um:ReadShort() elseif(msgtype == 1) then val = um:ReadChar() elseif(msgtype == 2) then val = um:ReadLong() elseif(msgtype == 3) then val = um:ReadFloat() elseif(msgtype == 4) then val = um:ReadString() elseif(msgtype == 5) then val = um:ReadAngle() elseif(msgtype == 6) then val = um:ReadVector() elseif(msgtype == 7) then val = um:ReadBool() elseif(msgtype == 8) then val = um:ReadEntity() end local name = um:ReadString() SendVars[name] = val hook.Call("VarReceived",nil,name,val) end end [/lua] It puts everything received in a table and also calls a hook... I'm not sure if this is working I can't find the final version atm.
That won't work but it's a nice psuedo lua example:smile:
SendLua is basically a usermessage that just calls RunString. It's not "inefficient", usermessages can be used easier though. SendLua I use often just because it's quicker and it's not like you're constantly sending shit in a Think hook.
[QUOTE=Drew P. Richard;25486220]SendLua is basically a usermessage that just calls RunString. It's not "inefficient", usermessages can be used easier though. SendLua I use often just because it's quicker and it's not like you're constantly sending shit in a Think hook.[/QUOTE] I think it sends the whole function string and then runs it. So yes, it will be more inefficient.
It's just sending a string, it is a bigger string yes, but it's not so much of a deal that it's going to lag the server if you use it to send things at certain times.
[QUOTE=Drew P. Richard;25486687]It's just sending a string, it is a bigger string yes, but it's not so much of a deal that it's going to lag the server if you use it to send things at certain times.[/QUOTE] If you're sending this string often and you have many players then this can prove to be very inefficient.
I guess, whatever.
Sorry, you need to Log In to post a reply to this thread.