setTile function

I needed a setTile function in Lime in order to open and close doors on my map, and thought I’d put the function up here for anyone who wants it.

The function doesn’t take into account physics properties, and doesn’t throw property notifications if the new tile contains them. It wouldn’t be difficult to add either to the function though. I’m just not using them right now, so I didn’t.

To use it just put this function in your main.lua, or wherever, and the call it with the layer to modify, the gid to set the tile to and the coordinates of the tile to change.

Here’s an example that will set the tile at 5,10 on layer “Overlay” to the second tile in your tile sheet:
[lua]local layer = map:getTileLayer(“Overlay”)
tile = layer:getTileAt({row=tile.row-1, column=tile.column})
setTile(tile.layer, 2, 5, 10)[/lua]

And, here’s the function:
[lua]function setTile(layer, gid, tx, ty)
local index = layer.map.width*(ty-1)+tx

local oldTile = layer.tiles[index]
if oldTile and oldTile.sprite then layer.group:remove(oldTile.sprite) end

local tile = Tile:newFromGid(gid, layer.map, layer)

local data = {}
data[“Attributes”] = {}
data[“Attributes”].gid = gid

data[“Attributes”].gid = tonumber(data[“Attributes”].gid)
tile = Tile:new(data, layer.map, layer)
layer.tiles[index] = tile

tile:create(index)
if(tile) then

if(tile.gid ~= 0) then

if(not layer.tileGrid) then
layer.tileGrid = {}
end

if(not layer.tileGrid[tile.column]) then
layer.tileGrid[tile.column] = {}
end

if(not layer.tileGrid[tile.column][tile.row]) then
layer.tileGrid[tile.column][tile.row] = {}
end

layer.tileGrid[tile.column][tile.row] = tile

end
end

if(tile.sprite) then

if(tile.tileSet) then

local sprite = tile.sprite

sprite.alpha = tile.alpha or 1
sprite.isVisible = utils.stringToBool(tile.isVisible) or true
sprite.isHitTestable = utils.stringToBool(tile.isHitTestable) or true
sprite.x = (tile.x or sprite.x) + (tile.xOffset or 0)
sprite.y = tile.y or sprite.y + (tile.yOffset or 0)
sprite.xOrigin = tile.xOrigin or sprite.xOrigin
sprite.yOrigin = tile.yOrigin or sprite.yOrigin
sprite.xReference = tile.xReference or sprite.xReference
sprite.yReference = tile.yReference or sprite.yReference
sprite.xScale = tile.xScale or 1
sprite.yScale = tile.yScale or 1
sprite.rotation = tile.rotation or 0

if tile.xTileOffset then
sprite.x = sprite.x + (tile.xTileOffset * sprite.width)
end

if tile.yTileOffset then
sprite.y = sprite.y + (tile.yTileOffset * sprite.height)
end

layer.group:insert(tile.sprite)
end

end
end[/lua]

Enjoy! [import]uid: 3401 topic_id: 5904 reply_id: 305904[/import]

Bah. Ignore the second line in the first code sample. It’s a carry over from my project when I copy and pasted, but the forum won’t let me edit it out. [import]uid: 3401 topic_id: 5904 reply_id: 20256[/import]

Awesome! This is now added and will be included in 2.7

To call it you just use:

  
tileLayer:setTileAt(4, position)  
  

The first parameter is the GID as specified in Tiled, essentially the index into your tileset and as the name implies this is global so if you have multiple tilesets then the first one starts at 1 and the second starts at 1 + the amount of tiles in the previous tileset and so on. If you look in your .tmx file in a text editor you will see that each tileset has a firstgid value.

The second parameter is a position table which is either a world position or a grid position. A grid position is just a tile position (row/column) however I have renamed them in 2.7 as “tile position” was a bit silly considering they were all tile positions really :slight_smile:

This will also build the tile for you thus brining over any physical properties and fire any property listeners that you have registered.

Thank you Matt for this great addition and reminding me that I need to clean up the code, for instance all the sprite properties and row/column stuff you had to copy over to your function are now simply in the tile:create() function where they should be. [import]uid: 5833 topic_id: 5904 reply_id: 20273[/import]

Damn forum, triple post :slight_smile: [import]uid: 5833 topic_id: 5904 reply_id: 20274[/import]

See above. [import]uid: 5833 topic_id: 5904 reply_id: 20275[/import]

Awesome! Now that’s service :slight_smile: [import]uid: 3401 topic_id: 5904 reply_id: 20292[/import]

Service is my middle name :slight_smile:

Any more ideas etc just give me a shout. [import]uid: 5833 topic_id: 5904 reply_id: 20294[/import]

This is great! I was thinking of using the animation capabilities to open & close doors, boxes etc but this makes life so much easier! Thank you. [import]uid: 11904 topic_id: 5904 reply_id: 20359[/import]