SWIG_NewPointerObj and values always being nil

Posted by Tom J Nowell on Stack Overflow See other posts from Stack Overflow or by Tom J Nowell
Published on 2010-03-09T03:18:42Z Indexed on 2010/03/09 3:21 UTC
Read the original article Hit count: 234

Filed under:
|

I'm using SWIG to wrap C++ objects for use in lua, and Im trying to pass data to a method in my lua script, but it always comes out as 'nil'

void CTestAI::UnitCreated(IUnit* unit){
    lua_getglobal(L, "ai");
    lua_getfield(L, -1, "UnitCreated");
    swig_module_info *module = SWIG_GetModule( L );
    swig_type_info *type = SWIG_TypeQueryModule( module, module, "IUnit *" );
    SWIG_NewPointerObj(L,unit,type,0);
    lua_epcall(L, 1, 0);
}

Here is the lua code:

function AI:UnitCreated(unit)
   if(unit == nil) then
      game:SendToConsole("I CAN HAS nil ?")
   else
      game:SendToConsole("I CAN HAS UNITS!!!?")
   end
end

unit is always nil. I have checked and in the C++ code, the unit pointer is never invalid/null

I've also tried:

void CTestAI::UnitCreated(IUnit* unit){
    lua_getglobal(L, "ai");
    lua_getfield(L, -1, "UnitCreated");
    SWIG_NewPointerObj(L,unit,SWIGTYPE_p_IUnit,0);
    lua_epcall(L, 1, 0);
}

with identical results.

Why is this failing? How do I fix it?

© Stack Overflow or respective owner

Related posts about lua

Related posts about swig