• Checking if a variable is exists
    16 replies, posted
I forget how to do this tbh. But basically I need to check if a variable exists so it doesn't error out the rest of my code if it doesn't exist.
unassigned variables are defaulted to the value of "nil" so you would do this: [code] if myvar == nil then myvar = 100 elseif myvar == 100 then print("myvar is 100.\n") end [/code]
if( Variable ) then //rest of your code here
c-unit that's what I have but it would error out so I'm going to try doing if var != nil then [editline]21st August 2011[/editline] Odd, neither work. [editline]21st August 2011[/editline] How about I do something different. How can I check the gamemode or gamemode name?
using GAMEMODE.Name
[QUOTE=InfernalCookie;31865742]Odd, neither work.[/QUOTE] You're doing it wrong, paste the code.
if( var ) then is the exact same thing as if( var != nil ) because if the var has any type of value at all it will return true
[QUOTE=c-unitV2;31867459]if( var ) then is the exact same thing as if( var != nil ) because if the var has any type of value at all it will return true[/QUOTE] It's not exactly the same because if(var) will return false if var is false or nil. For many purposes, if(var) is probably good enough, but it's good to know exactly what's going on just in case.
yeah, but unless you want to check if it's false there is no need to do if( var == nil ).
Hook 'PaintNewHud' Failed: attempt to call method 'GetRole' (a nil value) [lua] ... elseif v:GetRole() == ROLE_TRAITOR then ... [/lua] Testing it on non-TTT servers so that's why it's erroring right now.
how is that relevant? and v:getRole() won't exist if the code isn't on a TTT server..
Exactly, I want it to not do anything if it doesn't exist.
What the fuck? You are trying to check if the function _R.Player:GetRole() exists? If not then don't continue?
basically.
This is the stupidest thread I have ever seen, just run it in TTT.
Yah yah yah. Say what you want. Can you just answer my question?
This should achieve what you want. [lua] elseif v.GetRole and v:GetRole() == ROLE_TRAITOR then [/lua]
Sorry, you need to Log In to post a reply to this thread.