Page 1 of 1
How to generate a map with Adv. Tiled Loader?
Posted: Thu Aug 23, 2012 2:18 pm
by kirilz
Hey,
I am working on a level generator that procedurally generates tile-based levels. I wanted to use Adv. Tiled Loader, as I like it's API, but I cannot figure out how to populate a Map at runtime.
The basic process that I am using is:
1. Create a Map object, populate it with the map name, measurements and properties.
2. Create a tileset from an image
3. Create a layer and populate its cells
Step 3 is what is giving me a problem. How can I set the data of individual tiles in Adv. Tiled Loader? My code right now is:
Code: Select all
local layerName = "groudLayer"
level:newTileLayer(layerName, 1.0, nil, nil)
--- base ground layer coveres everything
for x=0,level.tileWidth do
for y=0,level.tileHeight do
level.tl[layerName].tileData:set(x,y,1)
end
end
But when I call level:draw(), ATL complains that the tile data is a number. I tried looking through the innards of ATL, but it is difficult to understand what data type and in what format is used to populate the tiles.
Thanks in advance!
Re: How to generate a map with Adv. Tiled Loader?
Posted: Thu Aug 23, 2012 6:36 pm
by dreadkillz
Might be best to pm Kadoba or send a message to his github for advance questions as he is the author of the lib.
Re: How to generate a map with Adv. Tiled Loader?
Posted: Thu Aug 23, 2012 7:34 pm
by Roland_Yonaba
dreadkillz wrote:Might be best to pm Kadoba or send a message to his github for advance questions as he is the author of the lib.
That's a good advice.
Actually, what you pass to
set() is a table.
There are useful resources on AdvTiledLoader
wiki. See
Grid class. That might help.
To figure out, I basically cloned the entire repository, attached a console , and tried to output the properties of the layer object inside
desertExample.lua
Code: Select all
-- Setup
local loader = require("AdvTiledLoader.Loader")
loader.path = "maps/"
local map = loader.load("desert.tmx")
local layer = map.tl["Ground"]
-- function dump is a self-made function ...
dump(layer.tileData:get(1,1)) --> returns a table.
So I assume that you need to pass the same kind of table to
Grid:set(x,y,value).
The output I got is attached below.
Some parts are obvious, but there are some values I can't explain, as I never used AdvTiledLoader.
Re: How to generate a map with Adv. Tiled Loader?
Posted: Thu Aug 23, 2012 9:34 pm
by Kadoba
A TileLayer's tileData is a
grid, which is just a 2d array with extra functionality built into it.
If you're making your own maps inside ATL then here are the steps you'll need to take:
- Use Map.newTileSet() to create a TileSet from an image. The tiles are automatically created and inserted into the map.
- Use Map.newTileLayer() to create a new TileLayer.
- Put your tiles from map.tiles into your new TileLayer's tileData
You can use the
ATL wiki for reference.
Here's a quick example:
Code: Select all
-- Load the module
local ATL = require "AdvTiledLoader"
-- Create a new map
local map = ATL.Map:new("My Map", 100, 100, 32, 32, "isometric")
-- Load the TileSet image
local image = love.graphics.newImage("MyTileSet.png")
-- Create a new TileSet. The last parameter is the starting gid
map:newTileSet(image, "My TileSet", 32, 32, image:getWidth(), image:getHeight(), 1)
-- Create an empty TileLayer
local myLayer = map:newTileLayer("My TileLayer")
-- Set tile (1,1) to the first tile cut out of the TileSet
myLayer.tileData:set(1, 1, map.tiles[1])
-- Set all tiles in a rectangle from (1,1) to (5,5) to the second tile cut out from the TileSet
for x, y, tile in myLayer.tileData:rectangle(1,1,4,4) do
myLayer.tileData:set(x, y, map.tiles[2])
end
-- Flip tile (3,3)
myLayer.tileData:flipTileX(3,3)
One thing you need to be aware of is the gid of the tiles. The gid is a number assigned to the tile so you can retreive it with map.tiles[gid]. Whenever you create a TileSet you must also give it a starting gid. When the TileSet cuts out the tiles it will assign gids to each tile.
See this wiki article for more information.
Basically if you have multiple TileSets inside of a map you need to make sure they don't cut out tiles that have clashing gids.The easiest way would probably be to increase the starting gid by a certain amount every time you add a new TileSet. For example, your first TileSet could have a starting gid of 1. Your second TileSet could have a starting gid of 10001. The third could be 20001 and so on.
If you need any more help let me know.
Re: How to generate a map with Adv. Tiled Loader?
Posted: Fri Aug 24, 2012 2:32 pm
by kirilz
Thanks, that information really helped, I can see what I was doing wrong now. Now the map generates properly, populated with the ground tile for the ground layer.