satoren / kaguya

C++ binding to Lua
Boost Software License 1.0
345 stars 70 forks source link

registering function not working #36

Closed Memorix101 closed 8 years ago

Memorix101 commented 8 years ago

Hey :)

I can't call the C++ function in Lua I just registered :(

void print_text(const char* msg)
{
    //printf("%s", msg.c_str());
    std::cout << "kaguya rocks ! " << msg << std::endl;
}

kaguya::State state;
kaguya::LoadLibs();
state.dofile("test.lua");

state["print_text"] = &print_text;

I always get "test.lua:7: attempt to call a nil value (global 'print_text')"

but calling state["print_text"]("meep"); works :P

In my lua script it's just print_text("hello world\n)

satoren commented 8 years ago

Are you running test.lua before register a function?

void print_text(const char* msg)
{
    //printf("%s", msg.c_str());
    std::cout << "kaguya rocks ! " << msg << std::endl;
}

kaguya::State state;
state["print_text"] = &print_text;
state.dofile("test.lua");
Memorix101 commented 8 years ago

Ahhh, that's it.
Sure, then it can't work.

Thank you ^-^

satoren commented 8 years ago

Your welcome.