Skip to content

c++ call lua

asqbtcupid edited this page Feb 2, 2019 · 6 revisions

c++ call lua:

prerequisites

  1. add "LuaPluginRuntime" to your gamemodule's build.cs file.
  2. #include "TableUtil.h"

There are 5 macro for c++ to interact with lua.

1. LuaCtor("xxx.yyy.zzz", UObjectPointer)

"xxx.yyy.zzz" is lua path, require it in lua must return a lua table Inherit table CppObjectBase

UObjectPointer is a UObject* pointer.

This macro will create a lua table to represent the c++ object.

2. LuaCall("luafunctionname", UObjectPointer, paramter1, parameter2, ...)

After you call LuaCtor(), now you can use LuaCall to call luafuncion.

no matter how many paramter.

There are some rule for parameter.

  • Basic type(int,float,bool,double...) paramter is passed by value copy, means change their's value in lua won't change the value in c++.

  • Enum are changed to int.

  • Struct is passed by reference, means if you change the value of their member will actually change the original.You have to include the binding code's c++ header file if the struct type is not blueprint type.

    If the struct ins is defined in c++ stack, Don't keep it in lua for later use. You can call Copy to create the same one in heap memory

  • FString,FText,FName will change to lua string, passed by value copy.
  • For pointer, only supported UObject Pointer,lua will figure out the pointer's actually class.

  • If Paramter's type is container,Such as TArray, TMap or TSet.If you haven't bind such type to lua,They will be changed to lua table.otherwise will pass the ref to lua.You have to include the binding code 's c++ header file.

    change the table in lua won't change the container in c++, while change the ref will.But if you change the inner's value, The rule is same as before.

3. LuaCallr(ReturnType, "luafunctionname", UObjectPointer, paramter1, parameter2, ...)

Same as LuaCall.But have return value. Return value is passed by value copy.

4. LuaStaticCall("luafunctionname", paramter1, parameter2, ...)

call a global lua function by name

5. LuaStaticCallr(ReturnType, "luafunctionname", paramter1, parameter2, ...)

call a global lua function by name, and have return value