lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

I've posted this once before, and didn't recieve any replies, so I thought
I'd try one more time, seeing as I really have no idea what to do here.

I'm trying to integrate Lua with a game engine I'm writing in C++, and I'm
having a little trouble with passing userdata variables to methods. The
table whose method I'm calling is set up like this:

symbol_world = {}
symbol_world.num_sprites = 0
symbol_world.sprites = {} my_avatar = Avatar() -- Avatar storage (idea taken
from life.lua example)
my_avatar.viewport = create_viewport() -- Viewport pointer
my_avatar.pitch, my_avatar.yaw, my_avatar.roll, my_avatar.velocity = 0, 0,
0, 0
symbol_world.sprites[symbol_world.num_sprites] = my_avatar
symbol_world.num_sprites = symbol_world.num_sprites + 1

The C++ code calls this function:

function process_frame(ticks_per_frame)
 for c = 0, symbol_world.num_sprites - 1 do

The my_avatar:animate() function looks like this:

function _Avatar:animate(ticks_per_frame)
 -- Relatively translate the viewport in the direction the avatar is facing
 -- by the current velocity times the current ticks_per_frame.
 viewport_translaterd(self.viewport, self.pitch, self.yaw, self.roll,
self.velocity * ticks_per_frame)

And finally, veiwport_translaterd() looks like this:

int api_viewport_translaterd(lua_State *client_state) // Relatively
translate a viewport directionally
 // viewport_translaterd(userdata viewprt_pointer, number pitch, number yaw,
number roll, number distance)
  if ((lua_gettop(client_state) >= 5) && lua_isuserdata(client_state, 1))
  Viewport *this_viewport = (Viewport *) lua_touserdata(client_state, 1);
  int distance = (int) lua_tonumber(client_state, 5);   // Must use trig to
find x/y/z values
  // Program segfaults right here
  this_viewport->camera_x += sin_lookup[(int) lua_tonumber(client_state, 3)]
* distance; // sin yaw * distance
  this_viewport->camera_y += sin_lookup[(int) lua_tonumber(client_state, 4)]
* distance; // sin roll * distance
  this_viewport->camera_z += sin_lookup[(int) lua_tonumber(client_state, 2)]
* distance; // sin pitch * distance
  return 0;

The program gives a segmentation fault at the first attempt to access
this_viewport after it is initialized. Anyone have any ideas? I'm new to
Lua, so it's probably just a stupid mistake. Thanks, Philip Bock.