More tricks with conditions

From GMod Wiki

Revision as of 22:36, 8 February 2011 by JohnnyThunders (Talk | contribs)
Jump to: navigation, search

More tricks with conditions

A few lessons back I taught you how to use the if statement. There are a few tricks I can teach you still that come in handy.

If you remember, I said the condition had to be true for an if statement to run. Lua takes this literally:

 
myName = "Jeff" 
sayMyName = true 
 
if sayMyName then 
    Msg("Your name is "..myName..".\n") 
end 
 

sayMyName is true, literally. On the second line of the script we set it to true. Then in the if statement, we put sayMyName as the condition. So since sayMyName is true, it says my name.

This is faster than doing:

 
myName = "Jeff" 
sayMyName = true 
 
if sayMyName == true then 
    Msg("Your name is "..myName..".\n") 
end 
 

Lastly, you can use it to check to see if a variable exists:

 
--Real exists, so the condition is true. 
real = "blahblahblah" 
if real then 
    Msg("The real variable exists!") 
end 
 
--There is no variable called fake, so the condition is false. 
if fake then 
   Msg("The fake variable exists!") 
end 
 

Like you can see above, we create the variable real, but not the variable fake. So it will always say the real variable exists, but the fake one doesn't.

BE AWARE: If you want to check if a number or boolean exists, you should compare it to nil. If you don't, it might think a variable set to false doesn't exist! If your have a var that could be false, do it like this:

 
myVar = false 
if myVar == nil then 
    Msg("Var doesn't exist at all!\n") 
end 
 

Lastly, there are two other things I'd like to show you. The and and or operators. They are really simple to use - see the example below:

 
missiles = 50 
supermissiles = 1 
 
--You need a missile and a super missile 
if supermissiles >= 1 and missiles >= 1 then 
    Msg("Rocket Combo launched!!\n") 
 
    missiles = missiles - 1
    supermissiles = supermissiles - 1
 
    RocketCombo()
end 
 

Like you can see above, a rocket combo is only launched if you have a supermissile and a missile. "And" works by making sure the condition on the left and the condition on the right is true. If they ARE both true, then the whole thing is true. If one is false, and the other is true, then the whole thing is false. It's an "all or nothing" deal.

Now "or" is much more forgiving. It only returns false if both conditions fail.

See here:

 
fruit=0
meat=0
dairy=0
bread=3
sweets=0
 
if fruit >= 1 or meat >= 1 or dairy >= 1 or bread >= 1 or sweets >= 1 then 
    Msg("There's something to eat in the fridge.\n") 
else 
    Msg("There's nothing to eat in the fridge.\n") 
end 
 

Like you can see above, if there is fruit, meat, dairy, bread, or sweets in the fridge, then it will tell you there's something to eat in the fridge. Otherwise it will tell you there isn't. Everything else is 0 (which you know is treated like false in a condition), but bread is 3. Since at least one of the "or" conditions is true, it will tell you there is something to eat.

Oh, did I forget to mention you can line up several conditions like that using "and" and "or"? Well, you can! Writing several "and" or "or" statements can save you a lot of work.

You could turn this:

 
if hasBird then 
    if hasToucan then 
        if hasBirdseed then 
            Msg("You find your toucan and give it some birdseed.\n") 
        end 
    end 
end 
 

Into this:

 
if hasBird and hasToucan and hasBirdseed then 
    Msg("You find your toucan and give it some birdseed.\n") 
end 
 

Or turn this:

 
if hasSword then 
    Msg("You have a weapon.\n"); 
elseif hasMace then 
    Msg("You have a weapon.\n"); 
elseif hasDagger then 
    Msg("You have a weapon.\n"); 
end 
 

Into this:

 
if hasSword or hasMace or hasDagger then 
    Msg("You have a weapon.\n"); 
end 
 

You can combine "and" and "or" together. Note that, in Lua, 'and' is evaluated before 'or', meaning that 'a and b or c' is evaluated as '(a and b) or c'. If you want 'a and (b or c)', you'll need to use parentheses.

 
Ammo=50 
WeaponName="BGun" 
-- Special ability for a weapon.
-- If you're holding a "BGun" and you have at least 25 ammo,
-- or if you're holding a "GodGun"
-- then it fires a special burst.
if Ammo >= 25 and WeaponName == "BGun" or WeaponName == "GodGun" then
    FireHugeBurst()
end
 

This is the same thing as doing:

 
Ammo=50
WeaponName="BGun"
-- Special ability for a weapon.
-- If you're holding a "BGun" and you have at least 25 ammo,
-- or if you're holding a "GodGun"
-- then it fires a special burst.
if Ammo>=25 and WeaponName == "BGun" then
    FireHugeBurst()
elseif WeaponName == "GodGun" then
    FireHugeBurst()
end
 

The last thing I should mention (and this only works with Garry's Mod lua) is that you can type && instead of and, and || instead of or if you want.


Yarin Kaul Icon ArrowSquare32 left.png The for loop

Yarin Kaul Icon ArrowSquare32.png Back to Lua Tutorial Series

Using the for in loop Yarin Kaul Icon ArrowSquare32 right.png


Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox