Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问可否提供静态绑定 Demo 及生成静态绑定代码工具 #565

Open
ZichaoNickFox opened this issue Nov 13, 2023 · 1 comment

Comments

@ZichaoNickFox
Copy link

ZichaoNickFox commented Nov 13, 2023

Is your feature request related to a problem? Please describe.
您好,项目在使用 unlua,unlua 使用元方法回调到 c++ 的 CallFunc,再 GetInstance 得到对象,再使用反射调用函数,这种方式效率太低,在考虑是否转型使用 slua,技术上希望使用静态绑定+代码生成的方式进行 lua、c++ 间的调用

Describe the solution you'd like
请问 slua 官方可以提供静态绑定的使用方式,以及生成静态绑定代码的生成工具

Describe alternatives you've considered
目前项目还在使用 unlua,其静态绑定支持不好

Additional context
另外,静态绑定+代码生成可能在读取成员变量时也要使用函数,比如 myStruct.a = 1 需要写成 myStruct.SetA(1),不知道技术上可否 myStruct.a = 1;再如 print(myStruct.a) 需要写成 print(myStruct.GetA()),不知道技术上可否写成 print(myStruct.a)

@zjhongxian
Copy link
Collaborator

zjhongxian commented Nov 14, 2023

1、静态绑定demo,仅供参考:

class BASIC_API FTimeTicker
{
public:
    FTimeTicker();
    ~FTimeTicker();

    // constructor function for lua
    // LuaOwnedPtr will hold ptr by lua and auto collect it;
    static NS_SLUA::LuaOwnedPtr<FTimeTicker> create();

    void SetTickFunction(const NS_SLUA::LuaVar& LuaFunction);

    void AddTimer(int index, float InFirstDelay);

    void Tick(float DeltaTime);
}

// 静态绑定代码
namespace NS_SLUA
{
    DefLuaClass(FTimeTicker)
        DefLuaMethod(SetTickFunction, &FTimeTicker::SetTickFunction)
        DefLuaMethod(AddTimer, &FTimeTicker::AddTimer)
        DefLuaMethod(Tick, &FTimeTicker::Tick)
    EndDef(FTimeTicker, &FTimeTicker::create)
}

2、静态绑定代码生成工具是这个:
https://github.com/Tencent/sluaunreal/tree/master/Tools/lua-wrapper.exe

另:对于反射方面的性能的顾虑,我觉得大可不必,一般都是够用的了,项目实践中我们更多的是利用反射功能。性能方面的顾虑可参考官网readme最后的benchmark表格。欢迎使用slua!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants