1

I am trying to load a table into C++ from Lua. This is how the file looks:

function alma(arg)
  print(arg)
end

sometable = {
  num = 5,
  text = "this is a string",

  nested = {
    {"a", alma("argument")},
    {"table", alma("arg")},
    {"element", alma("asd")}
  }
}

If I call luaL_loadfile I only get the chunk. If I call lua_dofile I get the elements, but the alma function runs for each element. In this SO thread, someone said to wrap these kinds of stuff into functions and call that to get the data. When i wrap/call the function the 3 alma functions run the moment i call the getter. How can i get sometable and its elements without executing the alma function?

1
  • You can use metatable on a table to implement lazy evaluation of some elements in that table. So, alma will be invoked only when you access corresponding element for the first time. This feature is transparent: you will not have to invoke some function explicitly. Commented Jan 15, 2017 at 15:05

2 Answers 2

2

and I'd like to have onClick events for gui elements, which would be some functions, hence the {"some string", function} table elements

Ok, you need function called later. Just save value of that functiion, i.e. simply write its name:

nested = {
    {"a", func_argument},
    {"table", func_arg},
    {"element", func_asd}
  }

But you want to call same function, passing arguments. And you want that info saved as a function. So either define a function directly in the table, or call some function that will return another function, storing its args in a closure:

-- function to be called
function alma(arg)
    print(arg)
end

-- define functions in table
nested1 = {
    {"a", function() alma "argument" end},
    {"table", function() alma "arg" end},
    {"element", function() alma "asd" end}
}

-- or create function within another function
function alma_cb(name)
    return function() alma(name) end
end

nested2 = {
    {"a", alma_cb "argument"},
    {"table", alma_cb "arg"},
    {"element", alma_cb "asd"}
}
Sign up to request clarification or add additional context in comments.

1 Comment

@val, the point is that a function is a value. It doesn't have a name but it might have one or variables or fields referring to it.
1

You can't get any value without calling some function. Any chunk loaded is a function. Not the data, but function that will construct/return data. You must call it, so it will fill some global variables, or explicitly return values.
If you don't want alma() called, then don't call it. Fill your table without calling alma().
For example:

return {
  num = 5,
  text = "this is a string",

  nested = {
    {"a", "argument"},
    {"table", "arg"},
    {"element", "asd"}
  }
}

You must load and call this chunk, it will construct and return table with .nested subtable, and alma() won't be called.

3 Comments

So i cannot have functions nested like that, and if I want to avoid automatic function calling at load time i have to avoid any reference to the function in data?
It feels like XY problem is involved. You're insisting on having functions, but don't want to call them. What's your exact task? What do you want to achieve?
I want to write GUI data in lua, and load that into C++ so i dont have to keep recompiling the program, when i make slight adjustments, and I'd like to have onClick events for gui elements, which would be some functions, hence the {"some string", function} table elements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.