Garry's Mod Wiki

Either

any Either( any condition, any truevar, any falsevar )

Description

An 'if then else'. This is almost equivalent to (condition and truevar or falsevar) in Lua. The difference is that if truevar evaluates to false, the plain Lua method stated would return falsevar regardless of condition whilst this function would take condition into account.

Arguments

1 any condition
The condition to check if true or false.
2 any truevar
If the condition isn't nil/false, returns this value.
3 any falsevar
If the condition is nil/false, returns this value.

Returns

1 any
The result.

Example

The following two print statements have identical results.

local ply = Entity( 1 ) print( "Player " .. Either( ply:IsAdmin(), "is", "is not" ) .. " an admin" ) print( "Player " .. ( ply:IsAdmin() and "is" or "is not" ) .. " an admin" )
Output: If Player 1 is admin, it will print "Player is an admin".

Example

Plain Lua alias version.

print("You are: "..( LocalPlayer():Alive() and "alive" or "dead" )) // conditional: print( "Halflife? "..( (LocalPlayer():Health()==50) and "halflife" or "not halflife") )
Output: You are alive

Halflife? not halflife

Page Links


Special Pages


Wikis

?

Render Time: 38ms

Session 0
DB GetPage 3
Generate Html 12
SaveChanges 9
Render Body 0
Render Sidebar 11