Skip to content

Instantly share code, notes, and snippets.

@rmasters
Created December 16, 2012 14:22
Show Gist options
  • Save rmasters/4307772 to your computer and use it in GitHub Desktop.
Save rmasters/4307772 to your computer and use it in GitHub Desktop.
A demo of how to write Lua code for non-programmers
-- Ross's Lua-howto without knowing any Lua :)
-- To run this file:
-- Install lua
-- Copy this file (click the "download" link) to the Desktop
-- Open a command prompt
-- Type "cd Desktop" without quotes
-- Type "lua luatut.lua" and follow through this code.
-- First off, lines beginning with two hyphens are ignored by Lua (they're
-- called 'comments' - you can put them in code files to explain what's going on
-- without affecting the code Lua runs).
-- First, we'll set a variable. A variable is a bucket that has a name and a
-- value in it. For example:
max_level = 60
-- sets a new variable called 'max_level' to 60.
-- Variables make it easy to write formulas etc. For example:
current_level = 10
levels_to_go = max_level - current_level -- levels_to_go is 50 (60 - 10)
-- Find the percentage of levels we've completed.
-- (10 ÷ 60) x 100 = 16.66666667
progress_percentage = (current_level / max_level) * 100
-- We can output variables as well, using the print() function. A function is a
-- bit of code that we can write that makes it easy to do things in one line.
-- The print function might write something to a command line, or a text file,
-- or a console in WoW.
print("Hello") -- Outputs: Hello
-- A piece of text, called a string, is indicated by letters between quotation
-- marks. This is wrong, for example:
-- print(Hello)
-- As Lua tries to find a variable called Hello, which doesn't exist.
-- Output the current level (set at 10 earlier on).
print("Current level:")
print(current_level)
-- But this writes two lines to a file, like this:
-- Current level:
-- 10
-- We can join two variables together using the .. operator to print them on the
-- same line:
print("Current level: " .. current_level)
-- Now to do some WoW coding. This will only run when you run Lua from WoW,
-- which I have no idea how to do. You can probably put this file in the Scripts
-- folder in WoW then run it from the console in game. This needs to be done as
-- WoW sets a ton of functions you can use (http://www.wowwiki.com/World_of_Warcraft_API)
-- For this part, I've declared a Lua function below since we won't be running
-- this with the WoW functions included:
-- Return indicates the value that will be given if you did:
-- print(GetAccountExpansionLevel()) -- Outputs 4
-- something = GetAccountExpansionLevel() -- something is now = 4
-- etc.
function GetAccountExpansionLevel()
return 4
end
-- GetAccountExpansionLevel() is a WoW function that returns a number based on
-- the expansion packs you've bought. So expansionLevel would be set to one of:
-- 0 (means WoW Classic (no expansions))
-- 1 (means Burning Crusade)
-- 2 (means Wrath of the Lich King)
-- 3 (means Cataclysm)
-- 4 (means Pandamonium)
expansionLevel = GetAccountExpansionLevel()
print("Expansion level: " .. expansionLevel)
-- Might output: "Expansion level: 4", in our case it will always output that.
-- But that's not very useful, so we can make a list of aliases for these codes:
EXPANSION_NAMES = {}
EXPANSION_NAMES[0] = "Classic"
EXPANSION_NAMES[1] = "Burning Crusade"
EXPANSION_NAMES[2] = "Wrath of the Lich King"
EXPANSION_NAMES[3] = "Cataclysm"
EXPANSION_NAMES[4] = "Pandamonium"
-- This stores a list of values associated with number codes in EXPANSION_NAMES.
-- The list of expansion names is rarely going to change, so the convention is
-- to give it a name in capitals as it is referred to as 'constant'. Not
-- required, just a common convention in most languages.
-- Note: lists in most programming languages start from 0, so 0 is the first item.
print(EXPANSION_NAMES[0]) -- Outputs: Classic
print(EXPANSION_NAMES[1]) -- Outputs: Burning Crusade
print(EXPANSION_NAMES[5]) -- Outputs: nil - a special value meaning "Nothing" as we don't have an item at list index 5 (item 6)
-- We can get a label from the list based on the current expansion level:
expansionLevel = GetAccountExpansionLevel()
print(EXPANSION_NAMES[expansionLevel])
-- Or in one line:
print(EXPANSION_NAMES[GetAccountExpansionLevel()])
-- We could also define another list of player level caps per expansion pack:
MAX_PLAYER_LEVEL_TABLE = {}
MAX_PLAYER_LEVEL_TABLE[0] = 60
MAX_PLAYER_LEVEL_TABLE[1] = 70
MAX_PLAYER_LEVEL_TABLE[2] = 80
MAX_PLAYER_LEVEL_TABLE[3] = 85
MAX_PLAYER_LEVEL_TABLE[4] = 90
print("Max level: " .. MAX_PLAYER_LEVEL_TABLE[expansionLevel])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment