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

huangapple go评论66阅读模式
英文:

SOL / Lua + C++ Cannot access field

问题

Sol & C++ embedding, cannot access usertype field

To start off with, I am pretty new to sol, however this is my implementation so far

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 ) );
			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;
		}
	}
}

Attempting to access it as so

-- 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

This is the following error produced:
[sol2] An error occurred and has been passed to an error handler: sol: runtime error: test.lua:11: attempt to concatenate a nil value (field 'm_ClassID')
stack traceback:
test.lua:11: in main chun

Calling the same function in c++ side works fine

Any ideas? Thank you

Using lua 5.4 & sol2

英文:

Sol & C++ embedding, cannot access usertype field

To start off with, I am pretty new to sol, however this is my implementation so far

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

			if ( !client_class )
				return nullptr;

			return client_class;
		}

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

			while ( client_class )
			{
				if ( !strcmp( client_class-&gt;m_pNetworkName, &quot;CBaseEntity&quot; ) )
					return client_class;

				client_class = client_class-&gt;m_pNext;
			}

			return nullptr;
		}

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

			auto recv_table_usertype = sol_state.new_usertype&lt; valve::RecvTable &gt;( &quot;RecvTable&quot;, sol::constructors&lt; valve::RecvTable( ) &gt;( ) );
			recv_table_usertype.set( &quot;m_pProps&quot;, sol::readonly( &amp;valve::RecvTable::m_pProps ) ); // BUG!!!: attempt to index a nil value (local &#39;prop&#39;)
			recv_table_usertype.set( &quot;m_nProps&quot;, sol::readonly( &amp;valve::RecvTable::m_nProps ) );
			recv_table_usertype.set( &quot;m_pNetTableName&quot;, sol::readonly( &amp;valve::RecvTable::m_pNetTableName ) );

			auto client_class_usertype = sol_state.new_usertype&lt; valve::ClientClass &gt;( &quot;ClientClass&quot; );
			client_class_usertype.set( &quot;m_pNetworkName&quot;, sol::readonly( &amp;valve::ClientClass::m_pNetworkName ) );
			client_class_usertype.set( &quot;m_pRecvTable&quot;, sol::readonly( &amp;valve::ClientClass::m_pRecvTable ) );
			client_class_usertype.set( &quot;m_pNext&quot;, sol::readonly( &amp;valve::ClientClass::m_pNext ) );
			client_class_usertype.set( &quot;m_ClassID&quot;, sol::readonly( &amp;valve::ClientClass::m_ClassID ) );

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

Attempting to access it as so

-- 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( &quot;&quot; .. table.m_ClassID )
    end

    client_class = client_class.m_pNext
end

This is the following error produced:
[sol2] An error occurred and has been passed to an error handler: sol: runtime error: test.lua:11: attempt to concatenate a nil value (field 'm_ClassID')
stack traceback:
test.lua:11: in main chun

Calling the same function in c++ side works fine

Any ideas? Thank you

Using lua 5.4 & sol2

答案1

得分: 0

以下是翻译好的部分:

"Here's the solution. I wrote the script in haste and I was attempting to access m_ClassID from table, which doesn't have that member.

Switching table.m_ClassID to client_class.m_ClassID did the trick

Thanks anyways everyone."

英文:

Here's the solution. I wrote the script in haste and I was attempting to access m_ClassID from table, which doesn't have that member.

Switching table.m_ClassID to client_class.m_ClassID did the trick

Thanks anyways everyone.

huangapple
  • 本文由 发表于 2023年3月21日 02:44:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794156.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定