1

我需要使用库“Simple Lua Binder”存储一个Lua函数

typedef ? TLuaFunction;

class Foo
{
public:
    void SetCallback( TLuaFunction f )
    {
        mCallback = f;
    }

    void ExecuteCallback()
    {
        f();    // Calls Lua function
    }

private:
    TLuaFunction    mCallback;
};

// Manager initialization
SLB::Manager scriptManager;

// SetCallback registration into lua
SLB::Class<Foo>( "Foo", &scriptManager )
    .constructor()
    .set( "SetCallback", &Foo::SetCallback )
    .set( "ExecuteCallback", &Foo::ExecuteCallback )
    ;

char* luaCode = 
    "SLB.using( SLB )\n"
    "o = Foo()\n"
    "o:SetCallback( function()                          \n"
    "                   print( 'Callback executed' )    \n"
    "               end                                 \n"    
    ")                                                  \n"    
    "o:ExecuteCallback()                                \n";    

SLB::Script script( &scriptManager );
script.doString( luaCode );

// The result is
> "Callback executed"

我不知道 SLB 库中是否有可以代替的类型?(见第一行代码)。

或者,如果我必须做不同的事情。

4

0 回答 0