ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   Lua/XML Help (https://www.esoui.com/forums/forumdisplay.php?f=175)
-   -   Clarification on function paramter types needed (https://www.esoui.com/forums/showthread.php?t=9259)

DarkPhalanx 07/08/20 04:20 AM

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 :)

Baertram 07/08/20 04:50 AM

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

DarkPhalanx 07/09/20 04:49 AM

Quote:

Originally Posted by Baertram (Post 41735)
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 :)


All times are GMT -6. The time now is 10:16 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2014 - 2022 MMOUI