Skip to content
Minh Ngo edited this page Jun 18, 2013 · 19 revisions

The tmx loader module loads a tmx map and converts it to a drawlist/map/atlas. Only one tileset is allowed per layer. If a layer uses more than one tileset, the loader will error out.

A drawlist can also be saved as a tmx file with the tmx saver module (Only maps/isomaps/objectgroups/imagelayers can be saved).

The names of tilesets are stored with atlas:setName. The names of tilelayers are stored with layer:setName.


To use the tmxloader:

	-- This is the standard way of loading the entire map.
	tmxloader    = require 'tmxloader'
	drawlist,err = tmxloader('map.tmx')
	if err then error(err) end
	
	-- One can also load a map in chunks, which is useful for background loading.
	-- The second argument represents the size of each chunk when calling the chunkloader.
	drawlist,chunkloader = tmxloader('map.tmx',1000)
	if not drawlist then error(chunkloader) end
	
	-- Call the chunkloader to fill in the empty drawlist.
	-- The chunkloader returns true when loading is finished.
	repeat
		local done,err = chunkloader()
		if err then error(err) end
	until done

To use the tmxsaver:

	tmxsaver = require 'tmxsaver'
	tmxsaver(drawlist,'map.tmx')

The following list shows TMX elements converted to the matching classes:

map     --> drawlist
layer   --> map/isomap
tileset --> atlas

The maps/isomaps are placed inside the drawlist as layers. Each map/isomap also has an atlas. The objectgroup and imagelayer are also inserted into the drawlist as raw data and are not drawn; they are there for the user to convert them into something useful.

Objectgroup

The objectgroup layer has the following table structure:

	objectgroup = {
		__element= 'objectgroup',
		name     = name,
		color    = color (hex string),
		x        = x,
		y        = y,
		width    = width,
		height   = height,
		opacity  = opacity,
		visible  = visible,
		objects  = {
			[1] = {
				name    = name,
				type    = type,
				x       = x,
				y       = y,
				width   =width,
				height  =height,
				rotation=rotation,
				gid     =gid,
				visible =visible,
				----------------
				ellipse = {},
					or
				polygon = {points= {x1,y1, x2,y2, x3,y3,...},
					or
				polyline= {points= {x1,y1, x2,y2, x3,y3,...},
			},
			
		},
	}

The above format must be maintained to save an objectgroup. Not all entries are required to load an objectgroup into Tiled (gid,color,...). The __element field is required.

Imagelayer

The imagelayer layer has the following table structure:

	imagelayer = {
		__element= 'imagelayer',
		name     = layer_name,
		trans    = trans (hex string),
		image    = Image,
		source   = source,
		width    = width,  -- imagelayer width
		height   = height, -- imagelayer height
	}

The above format must be maintained to save an imagelayer; the trans field is optional. The __element field is required.

Clone this wiki locally