• Confusion on the If Statement in Lua
    15 replies, posted
So I have the following function in my NPC: [lua] function ENT:AcceptInput(Name, Activator, Caller) if !Activator.cantUse and Activator:IsPlayer() then Activator.cantUse = true net.Start("npcdialogue") net.WriteString(Activator:getCharacterSex()) net.Send(Activator) timer.Simple(1.5, function() Activator.cantUse = false end) end end [/lua] -- Source: [url]https://scriptfodder.com/scripts/view/1062/character-creation-addon-for-darkrp[/url] (I'm creating my own NPC based off of his. If you do not believe that I purchased this script, I'll be more than happy to show you.) I don't understand the "if !Activator.cantUse" part. I don't know what the exclamation mark does? Please help! Thanks guys!
[QUOTE=Percipience;49883035]So I have the following function in my NPC: [lua] function ENT:AcceptInput(Name, Activator, Caller) if !Activator.cantUse and Activator:IsPlayer() then Activator.cantUse = true net.Start("npcdialogue") net.WriteString(Activator:getCharacterSex()) net.Send(Activator) timer.Simple(1.5, function() Activator.cantUse = false end) end end [/lua] -- Source: [url]https://scriptfodder.com/scripts/view/1062/character-creation-addon-for-darkrp[/url] (I'm creating my own NPC based off of his. If you do not believe that I purchased this script, I'll be more than happy to show you.) I don't understand the "if !Activator.cantUse" part. I don't know what the exclamation mark does? Please help! Thanks guys![/QUOTE] [code] if !Activator.cantUse then -- is the same as if not Activator.cantUse then [/code] It's good practice to use the 2nd one, as the first one is garry
All the exclamation mark means is that it's checking the opposite of the variable, so [CODE] local var = true print( !var ) [/CODE] Would return 'false' since that's the opposite. Also, as said above, you can also just use 'not' in place of the exclamation mark Also, the reason that code is checking that thing with the exclamation mark is just because it's trying to find whether the player CAN use the entity, rather than if they can't
You can also use the ! to inverse a collection of logical comparisons. if !(cat && dog) then that comparison is only false (does not execute the if statement) if both cat and dog are true if cat = true, dog = false, statement is true if dog = true, cat = false, statement is true if dog = false, cat = false, statement is true if dog = true, cat = true, statement is false
[QUOTE=bitches;49883188]&&[/QUOTE] Please never use that again
[QUOTE=MPan1;49883196]Please never use that again[/QUOTE] i don't actually write for gmod anymore, just a C++ habit though gmod supports those operators thanks to garry so
[QUOTE=bitches;49883204]gmod supports those operators thanks to garry so[/QUOTE] So it doesn't work with normal Lua which makes porting code with those symbols in it painful
just be glad it isn't [url=https://en.wikipedia.org/wiki/Brainfuck]brainfuck[/url] [code]-,+[ Read first character and start outer character reading loop -[ Skip forward if character is 0 >>++++[>++++++++<-] Set up divisor (32) for division loop (MEMORY LAYOUT: dividend copy remainder divisor quotient zero zero) <+<-[ Set up dividend (x minus 1) and enter division loop >+>+>-[>>>] Increase copy and remainder / reduce divisor / Normal case: skip forward <[[>+<-]>>+>] Special case: move remainder back to divisor and increase quotient <<<<<- Decrement dividend ] End division loop ]>>>[-]+ End skip loop; zero former divisor and reuse space for a flag >--[-[<->+++[-]]]<[ Zero that flag unless quotient was 2 or 3; zero quotient; check flag ++++++++++++<[ If flag then set up divisor (13) for second division loop (MEMORY LAYOUT: zero copy dividend divisor remainder quotient zero zero) >-[>+>>] Reduce divisor; Normal case: increase remainder >[+[<+>-]>+>>] Special case: increase remainder / move it back to divisor / increase quotient <<<<<- Decrease dividend ] End division loop >>[<+>-] Add remainder back to divisor to get a useful 13 >[ Skip forward if quotient was 0 -[ Decrement quotient and skip forward if quotient was 1 -<<[-]>> Zero quotient and divisor if quotient was 2 ]<<[<<->>-]>> Zero divisor and subtract 13 from copy if quotient was 1 ]<<[<<+>>-] Zero divisor and add 13 to copy if quotient was 0 ] End outer skip loop (jump to here if ((character minus 1)/32) was not 2 or 3) <[-] Clear remainder from first division if second division was skipped <.[-] Output ROT13ed character from copy and clear it <-,+ Read next character ] End character reading loop[/code]
[QUOTE=bitches;49883212]just be glad it isn't [url=https://en.wikipedia.org/wiki/Brainfuck]brainfuck[/url] [code]-,+[ Read first character and start outer character reading loop -[ Skip forward if character is 0 >>++++[>++++++++<-] Set up divisor (32) for division loop (MEMORY LAYOUT: dividend copy remainder divisor quotient zero zero) <+<-[ Set up dividend (x minus 1) and enter division loop >+>+>-[>>>] Increase copy and remainder / reduce divisor / Normal case: skip forward <[[>+<-]>>+>] Special case: move remainder back to divisor and increase quotient <<<<<- Decrement dividend ] End division loop ]>>>[-]+ End skip loop; zero former divisor and reuse space for a flag >--[-[<->+++[-]]]<[ Zero that flag unless quotient was 2 or 3; zero quotient; check flag ++++++++++++<[ If flag then set up divisor (13) for second division loop (MEMORY LAYOUT: zero copy dividend divisor remainder quotient zero zero) >-[>+>>] Reduce divisor; Normal case: increase remainder >[+[<+>-]>+>>] Special case: increase remainder / move it back to divisor / increase quotient <<<<<- Decrease dividend ] End division loop >>[<+>-] Add remainder back to divisor to get a useful 13 >[ Skip forward if quotient was 0 -[ Decrement quotient and skip forward if quotient was 1 -<<[-]>> Zero quotient and divisor if quotient was 2 ]<<[<<->>-]>> Zero divisor and subtract 13 from copy if quotient was 1 ]<<[<<+>>-] Zero divisor and add 13 to copy if quotient was 0 ] End outer skip loop (jump to here if ((character minus 1)/32) was not 2 or 3) <[-] Clear remainder from first division if second division was skipped <.[-] Output ROT13ed character from copy and clear it <-,+ Read next character ] End character reading loop[/code][/QUOTE] Oh my god. Not only is it hard as fuck... But it's also ugly as hell. So, basically "if !Activator.cantUse" means if the activator CAN use?
[QUOTE=Kevlon;49883069][code] if !Activator.cantUse then -- is the same as if not Activator.cantUse then [/code] It's good practice to use the 2nd one, as the first one is garry[/QUOTE] Actually, I'd use the ! / != because most programming languages don't use not.
[QUOTE=Verideth;49887287]Actually, I'd use the ! / != because most programming languages don't use not.[/QUOTE] Really it's just preference, just now it's gonna be more confusing if you try to switch to normal Lua. Lua 5.3 and onward especially because it introduced bitwise operators in the form of ! and &
[QUOTE=bigdogmat;49887462]Really it's just preference, just now it's gonna be more confusing if you try to switch to normal Lua. Lua 5.3 and onward especially because it introduced bitwise operators in the form of ! and &[/QUOTE] Ehhhh, I don't know about that.
[QUOTE=Percipience;49887509]Ehhhh, I don't know about that.[/QUOTE] It's really up to you. Do you want to stay and only code Lua? If so then it'd probably be a better practice to use not / and / blah blah blah. But if youre say, wanting to learn like C++ use !=, &&, and !
[QUOTE=MPan1;49883208]So it doesn't work with normal Lua which makes porting code with those symbols in it painful[/QUOTE] You chastise him for using &&, yet you think using ! is perfectly fine? Neither are default lua operators; both were added by garry.
I have only heard that using C++ operators in gmod is bad but I have never heard anyone explain why.
Guys, no one cares about the difference between C++ operators/GLua's/Lua's I wanted to know what it meant, not the difference between those operators. I don't care which one should or shouldn't be used. If they work, they work. // Solved
Sorry, you need to Log In to post a reply to this thread.