Hi,
I am learning how to make binary module and I have an error with my code, I can't find the solution:
I would like to call this function in this lua code in c++:
[CODE]
T = {}
function T:test(id, data)
print(id)
PrintTable(data)
end
T:test("id", {text = "text", color = { r = 0, g = 255, b = 0, a = 255}})
[/CODE]
I have tried this (main.cpp):
[CODE]
#define GMMODULE
#include "Interface.h"
GarrysMod::Lua::ILuaBase *LAU;
void test(const char* text, const double r, const double g, const double b, const double a)
{
LAU->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB);
LAU->GetField(-1, "T");
LAU->GetField(-1, "test");
LAU->PushString("id");
LAU->CreateTable();
LAU->PushString(text);
LAU->SetField(-2, "text");
LAU->CreateTable();
LAU->PushNumber(r);
LAU->SetField(-2, "r");
LAU->PushNumber(g);
LAU->SetField(-2, "g");
LAU->PushNumber(b);
LAU->SetField(-2, "b");
LAU->PushNumber(a);
LAU->SetField(-2, "a");
LAU->SetField(-2, "color");
LAU->Call(2, 0);
LAU->Pop();
LAU->Pop();
}
GMOD_MODULE_OPEN()
{
LAU = LUA;
test("text", 0, 255, 0, 255);
return 0;
}
GMOD_MODULE_CLOSE()
{
return 0;
}
[/CODE]
And when the test function is executed I got a Lua Panic error:
"lua/includes/extensions/table.lua:711: bad argument #1 to 'pairs' (table expected, got nil)"
Can you help me please ?
That looks like PrintTable throwing an error with its call to [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/table/GetKeys]table.GetKeys[/url]
The [B]test[/B] function is defined with [B]:[/B] instead of [B].[/B]
This means that there is an additional hidden parameter in the function definition, which serves as the [B]self[/B] reference. In your module, you are calling your function with only 2 arguments: the ID and data table, but you aren't passing the self reference, which needs to be the first argument.
[lua]-- There are THREE parameters here.
-- self, id, data
function T:test(id, data)
...
end
-- This is the same thing as the above.
function T.test(self, id, data)
...
end
-- This...
T:test("id", {text = "text", color = { r = 0, g = 255, b = 0, a = 255}})
-- ...is the same thing as this.
T.test(T, "id", {text = "text", color = { r = 0, g = 255, b = 0, a = 255}})[/lua]
The easiest solution would be to change T:test to T.test and not use a self reference, but if for whatever reason you need to use a self reference here, you can just do LAU->Push(-2) before pushing the string and change the call to use 3 arguments. More info [URL="https://www.lua.org/pil/16.html"]here[/URL].
Thanks you sannys it works !
Maybe do you know a library in c++ which does the same thing as the Timer lua library ?
pierre what is the connection with your code, thanks you anyway ?
Sorry, you need to Log In to post a reply to this thread.