Topic: How to bind custom lua functions?

Hello there ,
as the topic subject  suggests how would one bind custom/new functions for lua scripting . is it possible through game plugin ?.
I hope this is the right section for the topic .

Re: How to bind custom lua functions?

Hi,
yes you can add custom function using a game plugin with MScriptContext :

MEngine* engine = MEngine::getInstance();
MScriptContext* script = engine->getScriptContext();
if(script)
    script->addFunction("myFunction", myFunction);

Re: How to bind custom lua functions?

hey thanks for that just another small question how should the  c++ function for lua script  should be?
can you give me  an small example of the function it will be really helpful. i seem to cannot get it right
thank you for help.

Re: How to bind custom lua functions?

the function needs to return an int,
0 if the lua function doesn't return anything,
1 if the function returns 1 value, 2 for 2 values etc.

You can send values to lua using MScriptContext pushInt, pushString...
And get arguments from the lua call using getInteger, getString...

exemple :

#include <MEngine.h>
#include "MyPlugin.h"

int myFunction(void)
{
    MEngine * engine = MEngine::getInstance();
    MScriptContext * script = engine->getScriptContext();
    script->pushString("Hello World !");
    return 1;
}

void StartPlugin(void)
{
    MEngine * engine = MEngine::getInstance();
    MScriptContext* script = engine->getScriptContext();

    script->addFunction("myFunction", myFunction);
}

In lua the function will be used like this :

text = myFunction() -- "Hello World !"

Re: How to bind custom lua functions?

this works like a charm thanks anael  big_smile .