fredreichbier / ooc-lua

lua binding for ooc
MIT License
8 stars 1 forks source link

Downcasting ooc objects from Lua? #14

Open nddrylliog opened 10 years ago

nddrylliog commented 10 years ago

Practical case: using ooc-tiled, get an instance of 'Layer' from list.get, we want to access maps from 'ObjectGroup' (a subclass of 'MapLayer').

I tried all combinations of ffi.cast / typeof that I could find but I couldn't do it. You can look it up on lemon-stache I have a util method that just does pointer casting.

fredreichbier commented 10 years ago

Heh, good point! It works with:

objectGroup = ffi.cast(ffi.typeof("$*", tiled_ObjectGroup.ObjectGroup), tlayer)

because tiled_ObjectGroup.ObjectGroup is a struct type.

I don't think we can do this in an easy way using only ffi, so a convenience function would be good. We could have a howling.cast(type, value) which just runs the type parameter through to_ctype, which already exists as a convenience function. What do you think?

nddrylliog commented 10 years ago

But I tried exactly that! And I swear luaffi was complaining.

fredreichbier commented 10 years ago

I just added a howling.cast function. The above line can be rewritten as:

 howling.cast(tiled_ObjectGroup.ObjectGroup, tlayer)

What do you think?