• GetNetworkedInt to net.Receive
    8 replies, posted
Hi, I'm trying to convert all my code from having to use Set/GetNetworkedInt to [URL="http://wiki.garrysmod.com/page/Net_Library_Usage"]net.Broadcast/Send/Receive[/URL]. I have managed to do the following: [B][U]Server side code[/U][/B] [CODE]if (group_id == 4 || group_id == 2) then ply.PlayerIsVIP = true net.Start("PG_SAM_VIP") net.WriteBit(true) net.Broadcast() end if ply.PlayerIsVIP then print("The player is a VIP") end[/CODE] [B][U]Client side code[/U][/B] [CODE]net.Receive( "PG_SAM_VIP", function () if net.ReadBit() == 1 then PlayerIsVIP = true else PlayerIsVIP = false end end) if PlayerIsVIP then print("You are a VIP") end[/CODE] Although that method isn't much of a problem for that particular scenario, it is for all my other variables (over 30) - I would really prefer not to create a new variable within the net.Receive so that I may later use it in client code. Instead I would like to be able to do something similiar to [B]ply:GetNWInt("PlayerIsVIP", 0)[/B] which can be used in IF statements and where necessary, to get the needed value immediately. I hope someone can help out :)
You're welcome to look into the Netwrapper library I wrote a few weeks ago, whether to use or learn from. It sounds close to what you're wanting to accomplish. Facepunch page: [URL]http://facepunch.com/showthread.php?t=1383088[/URL] GitHub - Addon format (documented): [URL]https://github.com/Mista-Tea/netwrapper[/URL] GitHub - Single file (not documented): [URL]https://github.com/Mista-Tea/netwrapper/tree/condensed[/URL] I wrote it particularly to avoid using the Set/GetNetworked* library with the use of net messages, so I hope you find something useful in it :)
-wrong-
[QUOTE=AnonTakesOver;44578932]I believe 0 is false and 1 is true if you do an if statement, so [CODE] if net.ReadBit() == 1 then PlayerIsVIP = true else PlayerIsVIP = false end [/CODE] can be simply [CODE] PlayerIsVip = net.ReadBit() [/CODE] correct me if I'm wrong[/QUOTE] Absolutely not. Any number will evaluate to [I]true [/I]in an if statement, even 0: [lua]if 0 then print( "0 is true" ) end > "0 is true"[/lua] And regardless, your second piece of code literally just asigns [I]PlayerIsVip[/I] to either the number 0 or 1, when in most cases you would want it to be a proper boolean. An easier way to do it would be like so: [lua]PlayerIsVip = tobool( net.ReadBit() )[/lua] [I]tobool[/I] evaluates 0 as [I]false[/I] and non-0 values as [I]true[/I], as defined [url=https://github.com/garrynewman/garrysmod/blob/9a90af4f8cdb40fd91e27757779e2e1978eb0373/garrysmod/lua/includes/util.lua#L227]here[/url]: [lua]function tobool( val ) if ( val == nil || val == false || val == 0 || val == "0" || val == "false" ) then return false end return true end[/lua]
[QUOTE=EvacX;44578958] :words:[/QUOTE] You need to turn it to bool first, didn't know that (obviously). You learn something new everyday
PlayerIsVip = net.ReadBit() doesn't (entirely) work since PlayerIsVip is now a number instead of a boolean. However, PlayerIsVip = (net.ReadBit() == 1) will work since it will check if the result of ReadBit is 1 and set PlayerIsVip to the appropriate boolean.
Here's how to network booleans: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/networking/networking_booleans.lua.html[/url]
You could always do: [code] PlayerIsVip = net.ReadBit() == 1 and true or false. [/code]
[QUOTE=dingusnin;44595000]You could always do: [code] PlayerIsVip = net.ReadBit() == 1 and true or false. [/code][/QUOTE] Yep, for my example I sent two, the names _true and _false were for two separate bits. But a ternary operation is great shorthand! [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/ternary_operations.lua.html[/url] But for receiving them: [lua]local _value = tobool( net.ReadBit( ) );[/lua] is pretty short too, and it'll be true/false depending.
Sorry, you need to Log In to post a reply to this thread.