Garry's Mod Wiki

Either

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

Description

A compact '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

An example of the differences between Lua's ternary operator and this function.

local myCondition = true local consequent = false local alternative = "Second string" print(myCondition and consequent or alternative) print(Either(myCondition, consequent, alternative))
Output: Second string

false

Page Links


Special Pages


Wikis

?

Render Time: 83ms

DB GetPage 40
Generate Html 13
SaveChanges (1) 10
Render Body 0
Render Sidebar 18