The last thread I made had no bites, probably because of how confusing it was. No problem, I wrote my own level editor in lua. At the moment I can parse levels but do nothing with them. Take this test level for example:
-- name of the level
name = "testlevel"
-- dimensions of the level
width = 20
height = 13
-- tile placement in the level (please keep proportional to the dimensions)
level =
{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
}
This lua script is the level, the numbers inside my level array represent tiles. I would have it parsed like so in my game:
int parse_level(const char* filename) {
/* initialize Lua */
L = lua_open();
/* load Lua base libraries */
luaL_openlibs(L);
/* load the Lua file */
if(luaL_loadfile( L, filename) || lua_pcall(L, 0, 0, 0)) {
printf("ERROR: something is wrong with the file!\n");
}
/* parse level */
open_level( "level.lua" );
/* parse information */
lua_getglobal(L, "name");
const char *name = lua_tostring(L, -1);
lua_getglobal(L, "width");
int width = lua_tonumber(L, -1);
lua_getglobal(L, "height");
int height = lua_tonumber(L, -1);
/* print information */
printf("LEVEL INFORMATION\n");
printf("name: %s\n",name);
printf("width: %d\n",width);
printf("height: %d\n",height);
/* parse the level */
lua_getglobal(L, "level");
int y, x;
int level[height][width];
printf("level: \n");
for (y = 1; y <= height; y++)
{
lua_rawgeti(L, -1, y);
for (x = 1; x <= width; x++) {
lua_rawgeti(L, -1, x);
level[y-1][x-1]=lua_tonumber(L, -1);
/* print the level to console */
if( x < width )
printf("%d,",level[y-1][x-1]);
else
printf("%d\n",level[y-1][x-1]);
lua_pop(L, 1);
}
lua_pop(L, 1);
}
/* close Lua */
lua_close(L);
}
My level parses just fine, console will tell me the name, width, height, and display the numbers properly for level. However my issue now is getting these variables outside of my parse_level() function. It's easy to do that with the width and height, but I have an issue with the name because if I make it a char, then I get errors when I try to make it equal the const char * created in my function. The biggest issue though, is moving my two-dimensional level array outside of the function, I am gonna need help on that one.
Any ideas?
Thanks