Nothing much to add to the post at Spellcaster Studios… Writing for two blogs is kind of a pain from time to time…
Spent the last couple of nights working on scripting internal structures, streamlining some functions and inventing a way to pass arrays/lists from LuaJIT to C++ and vice-versa…
My problem was that the raycasting should return a set of intersections (not just the first one)… The way I found to do this was to create a semi-templated class to handle the array itself, get some functions to access it/destroy it and do the conversion in the raycasting function itself…
The end result is something like this:
ffi = require("ffi") ffi.cdef[[ int LuaIntersectionArray_get_size(LuaIntersectionArray* ar); LuaIntersection LuaIntersectionArray_get(LuaIntersectionArray* ar, int index); void LuaIntersectionArray_free(LuaIntersectionArray* ar); ]] LuaIntersectionArray_mt = { __index = { type = function(a) return "LuaArray<LuaIntersection>" end, log = function(a,log_type,pre_text) std.log(log_type,pre_text..type(a)) end, size = function(array) return ffi.C.LuaIntersectionArray_get_size(array) end, get = function(array,index) return ffi.C.LuaIntersectionArray_get(array,index) end, free = function(array) ffi.C.LuaIntersectionArray_free(array) end, }, } LuaIntersectionArray = ffi.metatype("LuaIntersectionArray",LuaIntersectionArray_mt) function get_lua_array(c_array) local lua_array={} for i=0,c_array:size()-1 do local obj=c_array:get(i) table.insert(lua_array,obj) end c_array:free() return lua_array end
Of course I have to do this for every type of array I want to support on Lua (except the get_lua_array function, that is generic).
On the C/C++ side, I create the LuaIntersectionArray class, with the access methods, using a #define directive that declares all the functions, etc.
Then, on the Lua side, when I want to receive an array of intersections, I can do something like this:
ffi = require("ffi") ffi.cdef[[ LuaIntersectionArray* raycast_static_mesh_array(LuaRay ray); ]] ---------------------------------------------------------------------- -- Shortcut functions function raycast_static_mesh_array(ray) return get_lua_array(ffi.C.raycast_static_mesh_array(ray)) end
It’s not that elegant, can lead to memory leaks (although it shouldn’t happen if I keep things contained like this), but it’s pretty simple to use, since the return of raycast_static_mesh_array (which only raycasts static meshes and returns an array) is a simple Lua array/table (same thing there, really).
On other notes, check out the animation system on the new Max Payne game, it’s fantastic!
I wish I could spend 4 or 5 months just researching IK and animation to do such good looking stuff!