class KeyBoardHandler { public: virtual onKeypressed(const KeyEventArgs& e); virtual onkeyreleased(const KeyEventArgs& e);}
现在,我想将此扩展到Lua,以允许Lua利用并在脚本中注册KeyboardHandler.
到目前为止,这是原型.
class ScriptKeyboardHandler : public KeyboardHandler{public: ... prevIoUs methods omitted static voID createFromScript(lua_State* L); bool createCppData();private: ScriptKeyBoardHandler(lua_State* L); int mSelf; int mKeyPressFunc; int mkeyreleaseFunc; lua_State* mpLuaState;}
现在,我知道实现将是这样的:
ScriptKeyboardHandler::ScriptKeyboardHandler(lua_State* L) : mpState(L){ }ScriptKeyboardHandler::onKeypressed(...) { // check if mKeyPressFunc is a function // call it,passing in mself,and the event args as params} // On Key Release omitted because being similar to the key pressedScriptKeyboardHandler::createFromScript(lua_State* L){ auto scriptKeyboardHandler = new ScriptKeyboardHandler(L); if (scriptKeyboardHandler->createCppData()) { // set the light user data and return a reference to ourself (mSelf) }}ScriptKeyboardHandler::createCppData() { // get the constructor data (second param) and find the keypressed and keyreleased function,store those for later usage // any other data within the constructor data will apply to object}-- Syntax of the lua codeMyScriptHandler = { }MyScriptHandler.__index = MyScriptHandlerMyScriptHandler.onKeyPress = function(self,args)endhandler = createScriptHandler(handler,MyScriptHandler)registerKeyHandler(handler)
我只是不知道如何在函数作为表中的参数传入时找到这些函数.
我这样做了吗?我希望我是,这是一个痛苦的屁股因为tolua不容易支持虚拟课程,而不是你可以从脚本中得到的.
我不担心其他功能,我怎么从我的C代码中找到这些变量(按键功能等)
解决方法 这大致是我对onKeypressed的实现.voID ScriptKeyboardHandler::onKeypressed(){ //Get the table corresponding to this object from the C registry lua_pushlightuserdata(mpLuaState,this); lua_gettable(mpLuaState,LUA_REGISTRYINDEX); //Now get the function to call from the object lua_pushstring(mpLuaState,"onKeyPress"); lua_gettable(mpLuaState,-2); //Now call the function lua_pushvalue(mpLuaState,-2 ); // Duplicate the self table as first argument //Todo: Add the other arguments lua_pcall(mpLuaState,1,0 ); // Todo: You'll need some error checking here and change the 1 to represent the number of args. lua_pop(mpLuaState,1); //Clean up the stack}
但是,您还需要更新构造函数以将表示处理程序的lua对象存储到注册表中.
总结以上是内存溢出为你收集整理的c – 在C中存储对Lua值的引用,如何才能完成?全部内容,希望文章能够帮你解决c – 在C中存储对Lua值的引用,如何才能完成?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)