6

Basic table, how they should be. But me need do it by function, how i can do that?

local mainMenu = {
  caption = "Main Window",
  description = "test window",
  buttons = {
  { id = 1, value = "Info" },
  { id = 2, value = "Return" },
  { id = 3, value = "Ok" },
  { id = 4, value = "Cancel" }
  },
  popup = true
  }

Table should be based on outside params, and code one table for each variable of options - not better way. I make a function for that, they should create basic options like caption or description and pop up, and insert values to buttons table (if option enabled - add button). But here the problem, they wont insert to tmp table, buttons table and their values for next options.

   function createMenu()
    tmp = {}
    --buttons insert
   if(config.info) then
    table.insert(tmp, {buttons = {id = 1, value = "Info"}});
   elseif(config.return) then
    table.insert(tmp, {buttons = {id = 2, value = "Return"}});
   end
    --table main
   table.insert(tmp, {
    caption = "Main Window",
    description = "test window",
    popup = true
    })
     return tmp
   end

How i can fixing them?

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Happy Day
  • 297
  • 1
  • 5
  • 14

1 Answers1

5

From looking at your createMenu function, two obvious problems stick out:

  1. assigning to global tmp a new table every time createMenu is called.
  2. using the return keyword as a key in config.

One can be a problem if you use tmp somewhere else in your code outside the createMenu function. The obvious fix is to change it to:

local tmp = {}

For the second problem, you can use a lua keyword as a table key if you really want but you won't be able to use the . dot syntax to access this since Lua will parse this the wrong way. Instead, you need to change:

config.return

to

config["return"].

Edit: After reading your comment and checking the example table, it looks like only the button table is accessed by numerical index. In that case, you'll want to use table.insert only on button. If you want to create the table to have associative keys then you'll have to do something like this:

function createMenu()
  local tmp = 
  {
    --table main
    caption = "Main Window",
    description = "test window",
    popup = true,
    --button table
    buttons = {}
  }
  --buttons insert
  if config.info then
    table.insert(tmp.buttons, {id = 1, value = "Info"});
  elseif config['return']  then
    table.insert(tmp.buttons, {id = 2, value = "Return"});
  end

  return tmp
end

This will produce the mainMenu table you're describing in your question.

greatwolf
  • 20,287
  • 13
  • 71
  • 105
  • yes, you right, but top table - only for example. So createMenu function should create table, and usage window = createMenu() and send window by function - doCreateWindow(window, param1, callback). CreateMenu - return table. And here the problem - insert to tmp table - buttons table and their values based on options/outside data (problems with insert, becouse table.insert(tmp, {buttons[2] = {id = 4, value = "Text"}}) does not work for me ) – Happy Day Jun 30 '13 at 04:32
  • @HappyDay You'll have to better clarify the problem you're having. What is the expected output after calling `createMenu`? What do you expect the table returned to look like? What do you actually get instead? – greatwolf Jun 30 '13 at 04:35
  • So, problems only with table.insert, i cant insert values to table - buttons inside table tmp =( – Happy Day Jun 30 '13 at 04:38
  • `table.insert` works with the index part of the table only. In other words, after doing `table.insert`, your button table will be at `tmp[1]`, `tmp[2]` etc.. Use the dot syntax or `[]` syntax to insert by associative key. – greatwolf Jun 30 '13 at 04:44
  • so, i can use that function? table.insert(tmp.buttons, {id = 1, value = "text"}), and they will work? – Happy Day Jun 30 '13 at 04:48
  • Tested, application crushed by lua - Error module name:lua5.1.dll. They looks a right way, but something wrong here, becouse lua5.1.dll crushed here. – Happy Day Jun 30 '13 at 05:07
  • @HappyDay that definitely should not be happening if you're using the lua interpreter to test the script. Lua's a safe language, any script errors will give a stacktrack of the problem -- not crash with a core dump from a dll. Are you embedding Lua in another host application? – greatwolf Jun 30 '13 at 05:14
  • yep, that my game. So, i debug that by string to table, and they return to me that string = {description='test window',caption='Main Window',{id=1,value='Info',},popup=true,} -- but in options, i am enable 2 options for return and info. – Happy Day Jun 30 '13 at 05:32
  • @HappyDay Note your using nested if's in your `createMenu` function so at most only one of the options will be in. You'll have to un-nest them to allow more than one button option. – greatwolf Jun 30 '13 at 05:41