Store a Lua function?
Calling a Lua function from C is fairly straight forward but is there a way to store a Lua function somewhere for later use? I want to store user defined Lua functions passed to my C function for use on events, similar to how the Connect function works in wxLua.
check the registry (
luaL_ref()). it manages a simple table that lets you store any Lua value (like the function), and refer to it from C by a simple integer.
6 – More about Functions, An obvious consequence of first-class functions is that we can store functions not only in global variables, but also in table fields and in local variables. We have The store.lua module contains a simple interface to store key/value pairs in a persistent storage mechanism. Simply put, if you want to store and/or retrieve information across interfaces cross-message; this might be the module to do it.
Building on Javier's answer, Lua has a special universally-accessible table called the registry, accessible through the C API using the pseudo-index
LUA_REGISTRYINDEX. You can use the
luaL_ref function to store any Lua value you like in the registry (including Lua functions) and receive back an integer that can be used to refer to it from C:
// Assumes that the function you want to store is on the top of stack L int function_index = luaL_ref(L, LUA_REGISTRYINDEX);
6.2 – Non-Global Functions, Several predefined functions in Lua return multiple values. An example is the string.find function, which locates a pattern in a string. It returns two indices: the Lua - Functions. A function is a group of statements that together perform a task. You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division usually unique, is so each function performs a specific task.
The easiest way to do this is for your function to take a "name" and the lua function text. Then you create a table in the interpreter (if it doesn't exist) and then store the function in the table using the named parameter.
In your app just keep hold of a list of function names tied to each event. When the event fires just call all the functions from your table whose key matches the names in the list.
5.1, The library function type returns a string describing the type of a that can be used by any C code to store whatever Lua value it needs to store. I need to store a user-provided Lua function in a struct that implements UserData. I tried using UserDataMethods to add a method that takes in a function and stores it but I keep running into lifetime errors. rlua::Lua is expected to liv
Lua 5.1 Reference Manual, by a couple of functions in the auxiliary library that allow you to store values in pops a value from the stack, stores it into the registry with a fresh integer key, Building on Javier's answer, Lua has a special universally-accessible table called the registry, accessible through the C API using the pseudo-index LUA_REGISTRYINDEX. You can use the luaL_ref function to store any Lua value you like in the registry (including Lua functions) and receive back an integer that can be used to refer to it from C:
Programming in Lua : 27.3.2, To read the value of any global Lua variable, one can use the function: To store a value previously pushed onto the stack in a global variable, there is the DATA STORE SCRIPT. AvishalYT Aug 27th, 2019 377 Never Not a member of Pastebin yet? Sign Up, it unlocks many cool features! raw download clone embed report print Lua 1.09 KB local DataStore = game: GetService ("DataStoreService")--// Getting DataStore Se
5.3 Manipulating Lua Objects, Functions let you store a piece of code in a value, are useful to be able to run the same piece of code from multiple places, without having to A function value can be called by using the ( ) operator, which runs the code in the function. The ( ) pair goes after the function expression, and optionally contains a comma-separated list of arguments. This means that Lua functions are considered anonymous (no pre-set name), and first-class (not treated differently from other values).