ivansafrin/Polycode — Issue #78: Polymorphism lost at lua boundary
Author: mcclure
Created: 2011-12-27 10:23:14+00:00
State: open

I have a C++ method, called by Lua. It is typed as returning a Screen *. In fact, it returns a PhysicsScreen * (ie a subclass of Screen). When I check the .__class.__info.__name of the Screen returned by this object, it is claimed to be of type Screen (although it is actually of type PhysicsScreen). When I try to call setTransform on the returned object, I get a "LUA ERROR: [string "e:setTransform(d, Vector2(10,10), 0)"]:1: attempt to call method 'setTransform' (a nil value)".

If I simply change the return type of the function from Screen * to PhysicsScreen *, suddenly the __class.__info.__name becomes PhysicsScreen and setTransform becomes available again.

I realize this is a vague description-- I have here example code, screenshots etc I can post if it's helpful but won't bother if you get the idea just from this description :)

Basically it appears that C++ objects passed into lua-land become the type of their pointer cast, and lose the identity of the underlying object. This means you are unable to call any method not part of the parent class interface, and there's no way to do something like dynamic_cast at the lua end to fix this...