SOL / Lua + C++无法访问字段

68de4m5k  于 2023-03-25  发布在  其他
关注(0)|答案(1)|浏览(177)

SolC ++嵌入,无法访问用户类型字段(& C)
首先,我对sol还很陌生,但这是我目前为止的实现

namespace lua
{
    namespace lua_client
    {
        valve::ClientClass* GetAllClasses( )
        {
            auto client_class = csgo::base_client->GetAllClasses( );

            if ( !client_class )
                return nullptr;

            return client_class;
        }

        valve::ClientClass* GetClientClass( )
        {
            auto client_class = csgo::base_client->GetAllClasses( );

            while ( client_class )
            {
                if ( !strcmp( client_class->m_pNetworkName, "CBaseEntity" ) )
                    return client_class;

                client_class = client_class->m_pNext;
            }

            return nullptr;
        }

        void init( )
        {
            // setup our usertype(s)
            auto recv_prop_usertype = sol_state.new_usertype< valve::RecvProp >( "RecvProp", sol::constructors< valve::RecvProp( ) >( ) );
            recv_prop_usertype.set( "m_pVarName", sol::readonly( &valve::RecvProp::m_pVarName ) );
            recv_prop_usertype.set( "m_RecvType", sol::readonly( &valve::RecvProp::m_RecvType ) );
            recv_prop_usertype.set( "m_Flags", sol::readonly( &valve::RecvProp::m_Flags ) );
            recv_prop_usertype.set( "m_StringBufferSize", sol::readonly( &valve::RecvProp::m_StringBufferSize ) );
            recv_prop_usertype.set( "m_bInsideArray", sol::readonly( &valve::RecvProp::m_bInsideArray ) );
            recv_prop_usertype.set( "m_pExtraData", sol::readonly( &valve::RecvProp::m_pExtraData ) );
            recv_prop_usertype.set( "m_pArrayProp", sol::readonly( &valve::RecvProp::m_pArrayProp ) );
            recv_prop_usertype.set( "m_pDataTable", sol::readonly( &valve::RecvProp::m_pDataTable ) );
            recv_prop_usertype.set( "m_Offset", sol::readonly( &valve::RecvProp::m_Offset ) );
            recv_prop_usertype.set( "m_ElementStride", sol::readonly( &valve::RecvProp::m_ElementStride ) );
            recv_prop_usertype.set( "m_nElements", sol::readonly( &valve::RecvProp::m_nElements ) );
            recv_prop_usertype.set( "m_pParentArrayPropName", sol::readonly( &valve::RecvProp::m_pParentArrayPropName ) );

            auto recv_table_usertype = sol_state.new_usertype< valve::RecvTable >( "RecvTable", sol::constructors< valve::RecvTable( ) >( ) );
            recv_table_usertype.set( "m_pProps", sol::readonly( &valve::RecvTable::m_pProps ) ); // BUG!!!: attempt to index a nil value (local 'prop')
            recv_table_usertype.set( "m_nProps", sol::readonly( &valve::RecvTable::m_nProps ) );
            recv_table_usertype.set( "m_pNetTableName", sol::readonly( &valve::RecvTable::m_pNetTableName ) );

            auto client_class_usertype = sol_state.new_usertype< valve::ClientClass >( "ClientClass" );
            client_class_usertype.set( "m_pNetworkName", sol::readonly( &valve::ClientClass::m_pNetworkName ) );
            client_class_usertype.set( "m_pRecvTable", sol::readonly( &valve::ClientClass::m_pRecvTable ) );
            client_class_usertype.set( "m_pNext", sol::readonly( &valve::ClientClass::m_pNext ) );
            client_class_usertype.set( "m_ClassID", sol::readonly( &valve::ClientClass::m_ClassID ) );

            // setup our table(s)
            auto client_table = sol_state.create_table( "client" );
            client_table[ "GetAllClasses" ] = GetAllClasses;
            client_table[ "GetClientClass" ] = GetClientClass;
        }
    }
}

尝试访问它

-- example script to dump netvars

local client_class = client.GetAllClasses( )

-- is clientclass still valid?
while client_class do
    local table = client_class.m_pRecvTable

    -- do we have a valid table?
    if table ~= nil then
        print( "" .. table.m_ClassID )
    end

    client_class = client_class.m_pNext
end

这是产生的以下错误:[sol2]发生错误,已将其传递给错误处理程序:解决:运行时错误:test.lua:11:尝试连接nil值(字段“m_ClassID”)堆栈回溯:test.lua:11:在主春
在c++端调用相同的函数可以正常工作
有什么想法吗?谢谢
使用lua 5.4和sol2

yc0p9oo0

yc0p9oo01#

这里的解决方案,我写的脚本匆忙,我试图访问“m_ClassID”从表,其中没有该成员
因此,切换“table.m_ClassID”-〉“client_class.m_ClassID”就可以了
无论如何谢谢大家

相关问题