Sign in to follow this  
Bennyrebirth

LUA : Read a table of objects

Recommended Posts

Hi all! I'm new here :), i've some problems to use Lua into my games for loading my levels... I just want to read a table of object like this : -------- Level1.lua objects = { { type = 6, x=16, y=16, }, { type = 6, x=16, y=16 }, { type = 6, x=16, y=16 }, ... ------------- But i don't realy understand how a table like this work to use it into a c++ project. Here's my main.cpp : -------- Main.cpp typedef struct{ int type, x, y; } SPRITE; void LoadMap() // Chargement d'un niveau { lua_State * L = lua_open(); luaL_openlibs(L); if (luaL_dofile(L, "Level1.lua")!=0){} lua_settop(L,0); lua_getglobal(L,"objects"); SPRITE tmp_spr; int tableindex = lua_gettop(L); lua_pushnil(L); while(lua_next(L,tableindex)) { lua_pushvalue(L,-2); lua_gettable(L, -2); lua_pushstring(L, "type"); lua_gettable(L, -2); tmp_spr.type=(int)lua_tonumber(L, -2); lua_pop(L, 1); lua_pushstring(L, "x"); lua_gettable(L, -2); tmp_spr.x=(int)lua_tonumber(L, -2); lua_pop(L, 1); lua_pushstring(L, "y"); lua_gettable(L, -2); tmp_spr.y=(int)lua_tonumber(L, -2); lua_pop(L, 1); MakeObject(tmp_spr); lua_pop(L, 1); } } ----------------- With this code, i optain a black screen and the application freeze. Anyone can help me? Thk's in advance!

Share this post


Link to post
Share on other sites
You should have a lot more assertions in that code (offhand, I can see about ten places for that). That should uncover your problem easily.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this