This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
kylergs's profile picture

*Resolved* [Lua] running functions from lists

Started by kylergs, 05 November 2012 - 08:24 AM
kylergs #1
Posted 05 November 2012 - 09:24 AM
**RESOLVED**

So, I am writing a *fairly* complex GUI program with scrolling menu's but I am having trouble running different functions for each item.
I am trying to do it by referencing a function from a list.

Here is the code getting the error according to the in-game computer.
(Error is an "attempt to call nil" at line 205(bellow))

	typelist[tftype].func()

the typelist is here…

local typelist = {
["menu"] = {func = changeMenu()},
["text"] = {func = plainText()},
["special"] = {}
}

I should note that during debugging i have found that the "tftype" variable is correct and on the list (always "menu" when testing)

the changeMenu() function is:

function changeMenu()
menustate = mopt[menustate].options[select].link
end

(menustate is "admin")

Finally, the relevant part of mopt is:

local mopt = {
  ["admin"] = {
  options = {
  {display= "Pink",
   link = "admin",
   ftype = "menu",
   colour = colours.pink} ,
  {display= "Apple",
   link = "test",
   ftype = "menu",
   colour = colours.green} ,

the code compiles but does not run past when I try and switch menu's

I will try to respond to any queries or extra info needed as quickly as possible…
MysticT #2
Posted 05 November 2012 - 09:29 AM
You have to asign the functions and not their return values, so remove the brackets there:

local typelist = {
["menu"] = { func = changeMenu },
["text"] = { func = plainText },
["special"] = {}
}
That way it will asign the functions instead of calling them and assign their return value.
kylergs #3
Posted 05 November 2012 - 09:33 AM
Thank you so much, I've only really just started this and was so confused.Thanks for the quick response!