Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Add function is not working with tables somehow

A topic by AntikoreDev created Nov 18, 2022 Views: 243 Replies: 4
Viewing posts 1 to 2

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!

Developer(+1)

You can’t just use “console.log” in lua_call as a name - it takes a name of a global to call. It doesn’t do parsing.

You could make a Lua-side global function to call sub-functions, and call that.

I noticed the first thing actually.

What do you mean on “Lua-side global function to call sub functions”. I’m kinda lost on this

Thanks in advance :)

Developer(+1)

Like

function call_sub(obj, field, ...)
    return _G[obj][field](...)
end

and then lua_call("call_sub", "console", "log", 1, 2, 3)

Cool! Thanks :)

I already fixed my issue actually, they were different problems altogether, one of them the file paths, for example.

Thanks for the soon response, have a good day :D