Either

From Garry's Mod
Jump to: navigation, search
 Either( )

Contents

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

any condition

The condition to check if true or false.

Arguments

any truevar

If the condition isn't nil/false, returns this value.

Arguments

any falsevar

If the condition is nil/false, returns this value.

Returns

any

The result.

Examples

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".

Examples

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
Personal tools
Navigation