I’m having an issue with functions. I want to make all functions be organized into their tables. (That’s why I’m using sha1, it’s so pointless to have twice the same function so I use that to gimmick a memory reference (yeah, i know its dumb))
I’ve been doing a lot of tests, and this code always crash in the lua_call command at the last line. (Added there for testing purposes). If I change it to run a function from the field string, it works, the function actually runs.
Some reason when I check using lua_global_get the field returns undefined, and when passeed to the table, the same thing happens.
What I’m doing wrong? It has been a few days trying this and still not getting this to work.
If then I run this snippet from a file I get an error.
function onLoad()
console.log("Hello, World!");
end
Error:
attempt to call a string value
stack traceback:
Here is the module adder script.
function lua_add_module(state, module, refs){
var memory_table = "";
var len = array_length(refs);
for (var i = 0; i < len; i++){
var ref = array_get(refs, i);
var val = ref.val;
var name = ref.name;
var field = "_" + sha1_string_utf8(script_get_name(val));
lua_add_function(state, field, val);
memory_table += " " + name + " = " + field + (i + 1 == len ? "" : "\n");
}
var data = string(module) + " = { " + memory_table + " };";
lua_add_code(state, data);
lua_call(state, "console.log", "Hello, World!");
}
This is what calls the command
// Add necessary functions
lua_add_module(state, "console", [
{ name: "log", val: lua_console_log }
]);
And this is the script lua_console_log with it’s method lua_console_log
function lua_console_log(msg){
show_debug_message(msg);
}
Crash Thanks in advance!