Lua/Conditions
Lua modules based on the Scribunto/Lua extension are stored in resource pages using the Module: namespace. Each module uses a table to hold functions and variables, and that containing table is returned at the end of the module code.[1] Conditions are code structures used to make choices and control the flow of scripts. This lesson will show you how to use conditions in your scripts.
Prerequisites
[edit | edit source]This lesson assumes you have already completed the Expressions lesson.
Create a Lua Script with Conditions
[edit | edit source]To create a Lua script with conditions:
- Navigate to Module:Sandbox.
- Clear all existing code.
- It's a sandbox. Everyone is free to play in the sandbox. But if you find another user is actively editing the sandbox at the same time, you may also use Module:Sandbox/Username, where Username is your Wikiversity username.
- Add the following code and save the page:
local p = {} function p.conditions() local hour local result hour = tonumber(os.date('%H')) if hour < 12 then result = 'Good morning!' elseif hour < 18 then result = 'Good afternoon!' else result = 'Good evening!' end return result end return p
Test Your Lua Script
[edit | edit source]To test your Lua script:
- Navigate to either the Module_talk:Sandbox page, the Wikiversity:Sandbox page, or your own user or sandbox page.
- Add the following code and save the page:
{{#invoke:Sandbox|conditions}}
The result should be either:
Good morning!
or
Good afternoon!
or
Good evening!
depending on the UTC time of day.
Understand Your Lua Script
[edit | edit source]To understand your Lua script:
local
and the following code defines the variableshour
andresult
. Both arenil
.os.date('%H')
returns the current server time formatted in hours (%H) from 0 to 23 as a string. Server time is typically set to UTC rather than local time.tonumber()
converts the string to a number.hour =
stores the numeric value of the server time in the variablehour
.if hour < 12 then
begins a conditional code block and checkshour
to see if it is less than12
. If it is, the following statement(s) are executed. If not, the following statements are skipped.result = 'Good morning!'
sets the value of result.elseif hour < 18 then
checkshour
to see if it is less than18
. If it is, the following statements are executed. If not, the following statements are skipped.- Conditions are mutually exclusive. If the first condition was true (if), the second condition (elseif) is not evaluated.
elseif
is optional in a conditional code block and may be repeated.
else
indicates that if none of the previous conditions were true, the following statements are executed. If one of the previous statements was true, the following statements are skipped.else
is optional in a conditional code block.
end
ends the conditional code block.return result
returns the current value of result as the result of the function.
Conclusion
[edit | edit source]Congratulations! You've now created, tested, and understood a Lua script with conditions. Continue on to the Loops lesson.