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 .
You are not logged in. Please login or register.
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 .
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);
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.
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 !"
this works like a charm thanks anael .
Powered by PunBB, supported by Informer Technologies, Inc.
Currently installed 3 official extensions. Copyright © 2003–2009 PunBB.