Skip to content

Using Objects to Do Cool Stuff

ldurniat edited this page Jan 9, 2018 · 1 revision

Objects are a very important part of Tiled, they allow you to create things such as checkpoints, spawnpoints, pickups and much more. In this tutorial we will see how easy they are to use and then in the next tutorial we will see how they can be used to help set up your physical world.

Step 1: Creating an object layer

Creating an object layer is straight forward, simply right click the Layers panel and select New >> Object Layer, this will create a new blank layer much like we did with tile layers:

New Layer

Step 2: Creating your objects

Creating object is pretty the same as tile. You need to select tile from tileset or collection od images and drag it on Object layer. There is no limition where object can be placed. We will add more than one object

New Object

Step 3: Giving it some properties

To give santa object some properties you will need to once again left click on button Add Property in Custom Properties panel after selecting object.

Custom Properties

As you can see in the image below, you are able to give each object a Name and Value as well as as many properties as you like. You are also able to set its x and y position as well as its width and height.

For now we will just skip the position and size data and just add a Name and Value like so:

Object Name & Type

Step 4: Using it in your game

With our object created and setup it is now time to use it in our game, now we could do as we have previously done and get access to this object by looping through all the objects but we can do this a much more interesting way with a lot less code, namely using object listeners.

To do this we must first load up our map but then before we create the visual we need to place the following code. It is vital that this is done before the visual is created.

-- Create our listener function
local onObject = function( object )
    -- Show message
    print( 'Do you want a present from ' .. object.name .. '?' )
end

-- Add our listener to our map linking it with the object type
map:addObjectListener( 'santa', onObject )

What we have seen is that it is very easy to do stuff with objects, obviously what I have shown you is a very basic idea but the feature itself can be used for so much more.