PA c++ Lua新建插件注册传值

  下面要说到的前提是在Havok官网有了解的前提下进行的。官网这里教了如何新建一个自己的插件,我是在这基础上修改的。

  思路是,在插件中准备好变化的值,注册后,在PA运行中,用lua语言OnThink调用值。

  参考了官网下载下来的案例,案例地址“F:HavokAnarchySDKSourceVisionRuntimeEnginePluginsRemoteInputEnginePlugin”,这是一个安卓远程输入的引擎插件,在安卓手机屏幕上点击即可点。

  1.注册引擎插件名

  在GameManager.cpp文件中,加入

extern "C" int luaopen_RemoteInput(lua_State *);
void MyGameManager::OnHandleCallback(IVisCallbackDataObject_cl *pData)
{
    if(LuaSet)//在程序加载时注册Lua  RemoteInput      Vision::IsInitialized() || Vision::IsInitializing()&&
    {
        LuaSet=0;
        IVScriptManager* pScriptManager = Vision::GetScriptManager();
        if (pScriptManager)
        {
            lua_State* pLuaState = static_cast<VScriptResourceManager*>(pScriptManager)->GetMasterState();
            if(pLuaState)
            {
                lua_getglobal(pLuaState, "RemoteInput");
                int iType = lua_type(pLuaState, -1);
                lua_pop(pLuaState, 1);

                if(iType!=LUA_TUSERDATA)
                {
                    luaopen_RemoteInput(pLuaState);
                    int iRetParams = LUA_CallStaticFunction(pLuaState,"RemoteInput","IVRemoteInput","Cast","v>v", this);
                    if (iRetParams==1)
                    {
                        if(lua_isnil(pLuaState, -1))
                        {
                            lua_pop(pLuaState, iRetParams);
                        }
                        else
                        {
                            lua_setglobal(pLuaState, "RemoteInput");
                            Vision::Message.Print(1, 400, 420, "lua_RemoteInput setCompleted");

                            //return;
                        }
                        
                    }
                }
                else
                {
                    //return; //already loaded
                    Vision::Error.Warning("can not creat lua FMOD,lua_state  null or error!");
                }
            }

            //Vision::Error.Warning("can not creat lua FMOD,lua_state  null or error!");
        }
        //return;
    }
    if (pData->m_pSender==&Vision::Callbacks.OnBeforeSceneLoaded)
    {
        //在场景加载你可以在这里添加你的特定的代码

        client.addTuioListener(&dump);
        client.connect(false);
        return;
    }

    if (pData->m_pSender==&Vision::Callbacks.OnUpdateSceneBegin)
    {
        //This callback will be triggered at the beginning of every frame
        //您可以添加您自己的每帧的逻辑
        // [...]
        if (m_bPlayingTheGame)
        {
            //Vision::Message.Print(1, 200, 100, "testrunning 140208");
            //在场景加载你可以在这里添加你的特定的代码

            /*time_t nowtime;
            time(&nowtime);
            tm* t_tm=localtime(&nowtime);

            KinectTestX=10*t_tm->tm_sec;
            KinectTestX=10*t_tm->tm_sec;*/

            //监听端口
            /*TuioObject *frameObject = NULL;
            TuioObject *tobj = (*iter);
            frameObject = new TuioObject(currentTime,tobj->getSessionID(),tobj->getSymbolID(),tobj->getX(),tobj->getY(),tobj->getAngle());*/

            //Vision::Mouse.SetPosition(x,y);
            //Vision::Mouse.IsLeftButtonPressed();//if Input:IsMouseButtonPressed(Vision.BUTTON_LEFT) then
        }
        return;
    }

    if (pData->m_pSender==&Vision::Callbacks.OnEditorModeChanged)
    {
        // 当vforge开关从editormode_playing_in_game,关闭我们的游戏模式
        if (((VisEditorModeChangedDataObject_cl *)pData)->m_eNewMode != VisEditorManager_cl::EDITORMODE_PLAYING_IN_GAME)
            SetPlayTheGame(false);
        return;
    }
    if (pData->m_pSender==&Vision::Callbacks.OnAfterSceneLoaded)
    {
        //被触发时,玩游戏vforge启动或外vforge加载后的场景
        if (Vision::Editor.IsPlayingTheGame())
            SetPlayTheGame(true);
        return;
    }
    Vision::Input.Update();
    Vision::Mouse.SetPosition(100,100);
    if (pData->m_pSender==&Vision::Callbacks.OnWorldDeInit) 
    {
        // 这是重要的当在外面跑vforge
        SetPlayTheGame(false);
        return;
    }
}
注册插件以及每帧的逻辑

  2.在RemoteInputModule_wrapper.cpp中修改代码(此文件从上面提到的RemoteInputEnginePlugin中引用)

SWIGINTERN int _wrap_IVRemoteInput_GetKinectPoint(lua_State *L)
    {

        SWIG_CONVERT_POINTER(L, 1, IVRemoteInput, pSelf)

        //float x=KinectTestX, y=KinectTestY;
        //Vision::Mouse.IsLeftButtonPressed();
        //pSelf->GetKinectPoint(x, y);

        lua_pushnumber(L, (lua_Number)KinectTestX);
        lua_pushnumber(L, (lua_Number)KinectTestY);

        return 2; //in every case we leave two values at the stack
    }

  

3.之后再vForge中可调用

local x,y = RemoteInput:GetKinectPoint()

这里只是大线路写了新建插件,调用传值,有疑问联系。

原文地址:https://www.cnblogs.com/bkycjj/p/3533932.html