View Single Post
07/08/20, 04:50 AM   #2
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,989
read this, it should help:
https://wiki.esoui.com/New_to_lua%3F_GOTCHA

In lua you just define the variable and pass whatever type you like to it, function, table, string, number, bool.
You do not need to explicitly define the type, only for tables e.g.
by using {}
Lua Code:
  1. local var = {}

Be sure to use local in front of your variables or they will be polluting the namespace _G (a table named _G which is used to store all global variables. So _G["myVar"] is the same like myVar, if myVar was defined without local).
Locals are local to the scope so defined inside functions or if end or for do loops they will be only available inside them!
You can define a local at the top of your lua file and then use it inside the whole file (global to the file, local to other addons or files).
Or you define a global table
Lua Code:
  1. myUniqueAddonNameSpace = {}
and are able to add variables, functions etc. to it and reuse it inall your files via using the first line like this:
Lua Code:
  1. myUniqueAddonNameSpace  = myUniqueAddonNameSpace or {}
-> reuses the contents of table myUniqueAddonNameSpace or creates a new one if it was nil (nil is something like null, so undefined)

Lua interprets the lua files from top to bottom so the variables/functions/etc. you want to use need to be known/defined before you access them!


You can check the type via the "type" function
Lua Code:
  1. local ty = type(myVar)
  2.  if ty == "table" then
  3.  elseif ty == "number" then
  4.  ...
  5.  end

Last edited by Baertram : 07/08/20 at 04:52 AM.
  Reply With Quote