TILED/LUA - Integration with C

Started by
6 comments, last by NicholasLopez 9 years, 9 months ago

So in Tiled Map Editor, I notice that you can export a map as lua

[attachment=22295:tiled_export_to_lua.PNG]

I had made a thread earlier asking how I integrated a .tmx with c and xml, got no bites, so I worked on my own level in lua, and got it parsed in c. Now I am interested in seeing how this can be integrated into my code. This is a sample level I made of what a .tmx exported to lua looks like:


return {
  version = "1.1",
  luaversion = "5.1",
  orientation = "orthogonal",
  width = 20,
  height = 15,
  tilewidth = 16,
  tileheight = 16,
  properties = {},
  tilesets = {
    {
      name = "tile",
      firstgid = 1,
      tilewidth = 16,
      tileheight = 16,
      spacing = 0,
      margin = 0,
      image = "tile.PNG",
      imagewidth = 32,
      imageheight = 32,
      properties = {},
      tiles = {}
    }
  },
  layers = {
    {
      type = "tilelayer",
      name = "Tile Layer 1",
      x = 0,
      y = 0,
      width = 20,
      height = 15,
      visible = true,
      opacity = 1,
      properties = {},
      encoding = "lua",
      data = {
        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, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 4, 4, 4, 4, 4,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 4, 4, 4, 4, 4,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 4, 4, 4, 4, 4,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
      }
    }
  }
}

If I understood more of this lua script, I may be able to create something. I couldn't find anything on return {}. I know I can use get_global() for grabbing things from the script, in the thread I had made in game programming thats what I used for my level, does the same principal apply (i.e. should I disregard the curly brackets)? Thanks

Advertisement
That appears to be a simple Lua table definition.

not sure how I would retrieve the information from this kind of table though, not sure what the return means or how it's used. I tried


lua_getglobal(L, "version"); 
const char *version = lua_tostring(L, -1);
printf("%d\n",version);

but it was not able to able to get it

Using the lua export only makes sense if you actually integrate and use the lua engine for gamelogic etc. In this case you can load it like this (lua code):


-- load map
local my_map  = dofile("my_tiled_map_export.lua")
-- get version
local version  = my_map.version
...

If you just want to use lua to load your map into your engine, then using xml or an other format would be much better. E.g. use a xml lib like tinyxml(C++) to load and parse xml directly.

not sure how I would retrieve the information from this kind of table though, not sure what the return means or how it's used. I tried



lua_getglobal(L, "version"); 
const char *version = lua_tostring(L, -1);
printf("%d\n",version);
but it was not able to able to get it

Setting aside Ashaman73's reasonable argument, "version" will not be a global variable. It would be your job to store the returned table somewhere (maybe a global variable, maybe somewhere else). You can then query the value associated with the key "version" inside the table you stored.


E.g. use a xml lib like tinyxml(C++) to load and parse xml directly.

There's already a lot of libs to load tiled maps: https://github.com/bjorn/tiled/wiki/Support-for-TMX-maps

To respond to Ashman73, BitMaster, and vstrakh. None of those are for ANSI C. So I have no problem making my own, and what I have done so far has helped me understand lua functions more. However I am stuck yet again. Wanna know how version is printed? I would call the function print_string(), here is what it looks like:


void print_string(lua_State *L, const char *key) {
	/* table[key] lookup */
	lua_getfield(L, 1, key);
	/*	should checkstring here, but for brevity 
		get the value (from getfield) off the stack */
	const char *result = lua_tostring(L, -1);    
	printf("%s = %s\n", key, result);
	/* remove it from the stack */
	lua_pop(L, 1);
}

and it will work, the same principal applies for the numbers, I just changed lua_tostring(L,-1) to lua_tonumber(L,-1). Now I am stuck at getting into the tables, the first one being tilesets, which I will post again down here


tilesets = {
	{
		name = "tile",
		firstgid = 1,
		tilewidth = 16,
		tileheight = 16,
		spacing = 0,
		margin = 0,
		image = "tile.PNG",
		imagewidth = 32,
		imageheight = 32,
		properties = {},
		tiles = {}
	}
},

I thought doing


lua_getfield(L, 1, "tilesets"); 
lua_pushnil(L);
print_string(L, "name");

would help, but it did not. I need help on what to do next, as well as go to the next table down the list when i'm done with the tilesets table. Thanks

So I have made these:


void print_string(lua_State *L, const char *key) {
	/*	assumes there's a table at -1
		this puts the table[key] value at -1 on the stack */
	lua_getfield(L, -1, key); /* -- (top) table[key] table ... */

	/* get the value at -1 as a string */
	const char *result = lua_tostring(L, -1);    
	printf("%s = %s\n", key, result);

	/* remove it from the stack */
	lua_pop(L, 1);
}

void print_number(lua_State *L, const char *key) {
	/*	assumes there's a table at -1
		this puts the table[key] value at -1 on the stack */
	lua_getfield(L, -1, key); /* -- (top) table[key] table ... */

	/* get the value at -1 as a number */
	int result = lua_tonumber(L, -1);    
	printf("%s = %d\n", key, result);

	/* remove it from the stack */
	lua_pop(L, 1);
}

int get_number(lua_State *L, const char *key) {
	/*	assumes there's a table at -1
		this puts the table[key] value at -1 on the stack */
	lua_getfield(L, -1, key); /* -- (top) table[key] table ... */
	/* get the value at -1 as a string */
	int result = lua_tonumber(L, -1);    
	/* remove it from the stack */
	lua_pop(L, 1);	
	/* return the value */
	return result;
}

and I can parse the level like so:


/* parse information */
	printf("\n");
	/*	negative indexing means (lua_gettop(L) - n) + 1
		push table to -1 */
		lua_pushvalue(L, -1);
	/*	look up and print infog */
		print_string(L, "version"); 
		print_string(L, "luaversion");
		print_number(L, "width");
		print_number(L, "height");
		/*	grab the width and height */
			lvl_width = get_number(L, "width");
			lvl_height = get_number(L, "height");
		print_number(L, "tilewidth");
		print_number(L, "tileheight");
	printf("\n");
	/*	puts the field, tilesets (table["tilesets"]) at -1 */
		lua_getfield(L, -1, "tilesets");
	/*	puts the value of tilesets[1] at -1
		(tilesets is an array, value is a table) */
		lua_rawgeti(L, -1, 1);
	/*	look up and print info */
		print_string(L, "name");
		print_number(L, "firstgid");
		print_number(L, "tilewidth");
		print_number(L, "tileheight");
		print_number(L, "spacing");
		print_number(L, "margin");
		print_string(L, "image");
		print_number(L, "imagewidth");
		print_number(L, "imageheight");
	/*	pops the array and tilesets off the stack */
		lua_pop(L, 2);
	printf("\n");
	/*	before we go to finish parsing, set up the level */
		level = malloc(sizeof(int) * lvl_width * lvl_height);
	/*	you are back in the "root" area, go to the next field */
		lua_getfield(L, -1, "layers");
	/*	do the same routine */
		lua_rawgeti(L, -1, 1);
	/*	look up and print info */
		print_string(L, "type");
		print_string(L, "name");
		print_number(L, "x");
		print_number(L, "y");
		print_number(L, "width");
		print_number(L, "height");
		print_number(L, "opacity");
		print_string(L, "encoding");

Up until I get to this point of the lua file:


data = {
        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, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 4, 4, 4, 4, 4,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 4, 4, 4, 4, 4,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 4, 4, 4, 4, 4,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
      }

which is inside the layers table. I've tried:


lua_getfield(L, -1, "data");
int i;
for( i = 1; i < lvl_width * lvl_height; i++ ) {
	lua_rawgeti(L, -1, i);
	level[i] = lua_tonumber(L, -1);
}
lua_pop(L, 1);	

but the game crashes upon that rawgeti inside the for statement. what do?

EDIT:


lua_getfield(L, -1, "data");
int i;
for( i = 0; i < lvl_width * lvl_height; i++ ) {
	lua_rawgeti(L, -1, i + 1);
	level[i] = lua_tonumber(L, -1);
	lua_pop(L, 1);
}
lua_pop(L, 1);

works, so I guess I figured it out. I hope this information helps anyone in the future when it comes to C, lua, and tiled, thank /g/ for it

This topic is closed to new replies.

Advertisement