英文:
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->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;
}
}
}
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
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论