Skip to content

Latest commit

 

History

History
78 lines (63 loc) · 2.21 KB

scripting.md

File metadata and controls

78 lines (63 loc) · 2.21 KB

scripting

mud reflection allows to automatically bind all of a module types, methods, fields and functions to a lua counterpart
it supports the following features :

  • call c++ functions from lua
  • create c++ objects from lua code
  • call c++ object methods from lua
  • pass any basic type, c++ object, or collection of objects back to these c++ functions
  • call lua functions from c++

given the following reflected c++ class :

class refl_ MyObject
{
public:
    constr_ MyObject(int var, std::string field);
    
    meth_ int method();
};

func_ void bar(MyObject& object);

mud allows you to write a lua script using any of the reflected functions, types, methods, fields

local object = MyObject(5, 'hello world!')
print(object:method())
bar(object) -- you can even pass c++ objects to a function

in order to do that, you simply create a lua interpreter and set it up to use your module

LuaInterpreter lua;
lua.reflect(MyModule::module());

then create a script object passing the lua code as a string

Script script = { lua_code };

you can then call this script like a regular function, as many times as you want

script();

script parameters

you can define a lua script taking some typed parameters

to do that, just create a signature specifying the sequence of parameters and pass that when creating the script object

Signature signature = { { "param", Ref(cls<int>()) } };
Script script = { "example script", lua_code, signature };

you can then call the lua script passing in the correct parameters

script({ var(57) });

the lua script can then access the parameter as if it was a function call

print(param) -- prints 57

visual script

create a visual script in which all the reflected primitives can be used

VisualScript script = {};

add nodes to the visual script from c++

Valve& arg = script.value(5);
Valve& field = script.value(string("cocorico"));
script.create<MyObject>({ &arg, &field }); // adds a node that creates an object