Simple Tiled Implementation - STI v1.2.3.0

Showcase your libraries, tools and other projects that help your fellow love users.
kinderalpha
Prole
Posts: 4
Joined: Mon Dec 23, 2019 7:52 pm

Re: Simple Tiled Implementation - STI v1.2.3.0

Post by kinderalpha »

ingsoc451 wrote: Mon Dec 23, 2019 10:00 pm Why don't you use objects if it is something like a door? Changing the tilelayer feels somewhat strange

and did you try:

Code: Select all

local tile = layer.data[8][5]
I did try that to no avail.

If I could use objects that would be great but I cant draw objects with a tile image (right?). On tiled I can only place a door tile in a tile layer, not object layer. Now that I'm typing this though. I could probably draw a tile image separately to the object in my code and do it that way.
User avatar
master both
Party member
Posts: 262
Joined: Tue Nov 08, 2011 12:39 am
Location: Chile

Re: Simple Tiled Implementation - STI v1.2.3.0

Post by master both »

kinderalpha wrote: Mon Dec 23, 2019 9:21 pm The way you layed this out makes a lot more sense to me compared to what I've read elsewhere. I understand it now, I just need to know one more detail.
master both wrote: Mon Dec 23, 2019 8:56 pm To get the tile instance, you first need to get the it's tile from it's position:

Code: Select all

local layer = map.layers["layer_name"]
local tile = layer.data[y][x]
What type of x and y value is expected for the layer.data index? On tiled, the door which I want to manipulate is at position (5,8) which correlates to tile position. So in my code I'm doing this.

Code: Select all

            local tile_x, tile_y = tilemap.map:convertTileToPixel(5, 8)
            local layer = tilemap.map.layers['Door']
            local tile = layer.data[tile_y][tile_x]
However, it's giving me an error for indexing a nil value for

Code: Select all

	local tile = layer.data[tile_y][tile_x]
So how should I go about getting the tile x and y if this isn't working?
Thank you so much for the help though, you have no idea how excited I am to tackle this issue.
To get the tile from the layer, you should use tile positioning, and since lua indexes start as 1, it should be as follows:

Code: Select all

local tile = layer.data[8+1][5+1]
But if data[8][5] gives you an error, then I don't know what the problem may be. Maybe you're already adding a 1 and the position should be (4, 7)?

Also I just realize I didn't specify the positioning that should be used when searching fot the instance, which is pixel positioning. I have edited my previous post to be more clear on this.
User avatar
Karai17
Party member
Posts: 930
Joined: Sun Sep 02, 2012 10:46 pm

Re: Simple Tiled Implementation - STI v1.2.3.0

Post by Karai17 »

Object layers support Tile Image Objects as part of the TIled spec, so you could use an object.
STI - An awesome Tiled library
LÖVE3D - A 3D library for LÖVE 0.10+

Dev Blog | GitHub | excessive ❤ moé
kinderalpha
Prole
Posts: 4
Joined: Mon Dec 23, 2019 7:52 pm

Re: Simple Tiled Implementation - STI v1.2.3.0

Post by kinderalpha »

Thank you so much for the help guys! I'm so thrilled to have figured this out. Here's what I ended up doing, and some things I didn't understand. So below is the full code for opening a door and closing a door.

Code: Select all

-- x, y is the tile location of the door
  local layer = tilemap.map.layers['Door'] -- Layer containing door tiles
  local tile = layer.data[y+1][x+1] -- Tile data for the door we're interacting with
  local door_closed = 357 -- GID for closed door image
  local door_open = 358 -- GID for open door image

  local instance = false
  local px, py = tilemap.map:convertTileToPixel(x, y) -- get exact x and y of tile.
  for _, ti in pairs(tilemap.map.tileInstances[tile.gid]) do
    -- Search through tileInstances by our specified tile GID
    if ti.x == px and ti.y == py then -- if instance matches tile location
      instance = ti
      break
    end
  end

  -- if instance is not nil or false from before
  if instance ~= false then
    -- handle which image to show
    if door.closed then
      door.closed = false -- door is closed, lets open it
      local new_tile = tilemap.map.tiles[door_open] -- opened door tile
      instance.batch:set(instance.id, new_tile.quad, instance.x, instance.y)
    elseif not door.closed then
      door.closed = true -- door is open, let's close it
      local new_tile = tilemap.map.tiles[door_closed] -- closed door tile
      instance.batch:set(instance.id, new_tile.quad, instance.x, instance.y)
    end
  end
I ended up using

Code: Select all

instance.batch:set(instance, quad, x, y)
Because swapTile seemed to change the position of the instance and made it unable to swap between one and another. I'm not sure why that was happening, or what exactly batch:set does differently but it works and I seem to have no issue with my usage of it. Thank you everyone for the help, and I posted the full code incase somebody in my situation needs a reference. You guys rock!
OtherCannon
Prole
Posts: 8
Joined: Mon Dec 30, 2019 1:09 am

Re: Simple Tiled Implementation - STI v1.2.3.0

Post by OtherCannon »

I'm a bit of a newbie here, so forgive my inexperience.
I was using the tutorial to implement the player object, but when I attempted to run it, I got this:
Error

main.lua:24: attempt to index local 'player' (a nil value)


Traceback

main.lua:24: in function 'load'
[C]: in function 'xpcall'
[C]: in function 'xpcall'

Code: Select all

local sti = require "sti"

function love.load()
  --Load map file
  map = sti("res/map/map.lua")
  
  --Create new dynamic data layer called "Sprites" as the 2nd layer
  local layer = map:addCustomLayer("Sprites", 2)
  
 --Error is in this chunk, lets figure it out shall we? 
  --Get player spawn object
  local player
	for k, object in pairs(map.objects) do
		if object.name == "Player" then
			player = object
			break
		end
	end

  --Create player object
  local sprite = love.graphics.newImage("res/img/sprite.bmp")
  layer.player = {
    sprite = sprite,
    x      = player.x,
    y      = player.y,
    ox     = sprite:getWidth() / 2,
    oy     = sprite:getHeight() / 1.35
  }
  
  --Draw player
  layer.draw = function(self)
    love.graphics.draw(
      self.player.sprite,
      math.floor(self.player.x),
      math.floor(self.player.y),
      0,
      1,
      1,
      self.player.ox,
      self.player.oy
    )
    
    --Temporarily draw a point at our location so we know
    --That our sprite is offset properly
    love.graphics.setPointSize(5)
    love.graphics.points(math.floor(self.player.x), math.floor(self.player.y))
  end
  
  --Remove unneeded object layer
  map:removeLayer("Spawn Point")
end

function love.update(dt)
  --Update world
  map:update(dt)
end

function love.draw()
  --Draw world
  map:draw()
end
I be developing stuff.
User avatar
Karai17
Party member
Posts: 930
Joined: Sun Sep 02, 2012 10:46 pm

Re: Simple Tiled Implementation - STI v1.2.3.0

Post by Karai17 »

it would seem that the object's name is not the exact same in your code and your map. strings are case sensitive.
STI - An awesome Tiled library
LÖVE3D - A 3D library for LÖVE 0.10+

Dev Blog | GitHub | excessive ❤ moé
OtherCannon
Prole
Posts: 8
Joined: Mon Dec 30, 2019 1:09 am

Re: Simple Tiled Implementation - STI v1.2.3.0

Post by OtherCannon »

That seemed to do the trick. Thanks!
I be developing stuff.
pauljessup
Party member
Posts: 356
Joined: Wed Jul 03, 2013 4:06 am

Re: Simple Tiled Implementation - STI v1.2.3.0

Post by pauljessup »

Hey all, I started working on a platformer, and thought that I'd use this rather than update the Green Tea in game map editor system. Mostly cause TileD rocks, etc. etc.

Anyway, I think I might end up doing a small tutorial for people that would walk through how to use this with Bump, and implementing gravity and that sort of thing.
User avatar
Karai17
Party member
Posts: 930
Joined: Sun Sep 02, 2012 10:46 pm

Re: Simple Tiled Implementation - STI v1.2.3.0

Post by Karai17 »

additional tutorials and plugins are very much welcome! feel free to make a pull request if you'd like the tutorial added to the main repo. credit yourself and all that too~
STI - An awesome Tiled library
LÖVE3D - A 3D library for LÖVE 0.10+

Dev Blog | GitHub | excessive ❤ moé
pauljessup
Party member
Posts: 356
Joined: Wed Jul 03, 2013 4:06 am

Re: Simple Tiled Implementation - STI v1.2.3.0

Post by pauljessup »

Sounds good, will do! I was like giving back to the tools I use, when I can
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests