Thread Tools Display Modes
07/08/20, 04:20 AM   #1
DarkPhalanx
AddOn Author - Click to view addons
Join Date: Sep 2019
Posts: 6
Question Clarification on function paramter types needed

Hi guys,

I'm new to lua and addon development and I created a function and have a question about paramters for lua functions. In C# I need to pass the input type

Code:
function X.FunctionName(bool unlock, string name)
end
But it doens't work like that, have tried to look other addons and they don't seem to pass the type in it. But can't figure out why it doesn't work for me or what the best practise is.

Code:
function X.FunctionName(unlock, name)
end
Any clarification on this is welcome
  Reply With Quote
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,912
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
07/09/20, 04:49 AM   #3
DarkPhalanx
AddOn Author - Click to view addons
Join Date: Sep 2019
Posts: 6
Originally Posted by Baertram View Post
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
Thanks for the explanation, makes more sense now
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Clarification on function paramter types needed

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off