Lua/Conditions

From Wikiversity
< Lua
Jump to navigation Jump to search

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]

This lesson assumes you have already completed the Expressions lesson.

Create a Lua Script with Conditions[edit]

To create a Lua script with conditions:

  1. Navigate to Module:Sandbox.
  2. 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.
  3. 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]

To test your Lua script:

  1. Navigate to either the Module_talk:Sandbox page, the Wikiversity:Sandbox page, or your own user or sandbox page.
  2. 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]

To understand your Lua script:

  1. local and the following code defines the variables hour and result. Both are nil.
  2. 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.
  3. tonumber() converts the string to a number.
  4. hour = stores the numeric value of the server time in the variable hour.
  5. if hour < 12 then begins a conditional code block and checks hour to see if it is less than 12. If it is, the following statement(s) are executed. If not, the following statements are skipped.
  6. result = 'Good morning!' sets the value of result.
  7. elseif hour < 18 then checks hour to see if it is less than 18. 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.
  8. 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.
  9. end ends the conditional code block.
  10. return result returns the current value of result as the result of the function.

Conclusion[edit]

Congratulations! You've now created, tested, and understood a Lua script with conditions. Continue on to the Loops lesson.

See Also[edit]

References[edit]