Skip to content

Taking Advantage of Properties

ldurniat edited this page Jan 8, 2018 · 1 revision

Properties allow you to create all kinds of gameplay, for instance perhaps you wanted to have health pickups in your game you could do so by adding a "healthBonus" property to your tile or object with a value of "50".

To keep Berry as generalised as possible I have decided to not implement any game specific features so in order to actually take advantage of properties you will have to write the code to deal with them.

I will be releasing tutorials over time that provide sample code for using properties in lots of interesting ways so stay tuned.

Step 1: Understanding what a property is

A property is simply a pair of data items, a Name and a Value. Everything in Tiled can have as many properties as you like. Possible uses are that you could give your Map a property that holds the level name or you could give a tile a property to be used as a health pickup.

Step 2: Adding a property

In this tutorial we will add a tile property just to show it working, but I will also list how to add properties to everything else as well.

With your map loaded up in Tiled you can add a property to a tile by left cliking on button Edit Tileset. A new window with selected tileset appear. Next select a tile and click on button Add Property located in the bottom right of Tiled to bring up the Property window

![Property Window]https://i.imgur.com/2vciLv0.png)

Now simply set a Name and Value for each property you want to add. You also need specified a Type but since Lua is a dynamically typed language there is no need to use it at all. I have just created one but you can create as many as you like:

New Property

With your property created make sure to add at least one of those tiles to your map and then save it.

Step 3: Using the property in your game

In this tutorial I will show a very basic, and pretty boring, use for properties however you will get an understanding of how they work and one of the ways you can access them while in later tutorials we will see more interesting things we can do with them.

Firstly you will need to load up your map as we did in the first tutorial and then get access to the tile layer that your new tile is on:

local layer = map:getTileLayer( 'Tile Layer 2' )

With your layer stored off you can now get a list of all tiles that are on it like so:

local tiles = layer.tiles

What we now need to do is simply loop through all the tiles printing off any properties they have, like this:

-- Loop through our tiles
for i=1, #tiles, 1 do

    -- Get a list of all properties on the current tile
    local tileProperties = tiles[i]:getProperties()

    -- Loop through the properties, if any
    for key, value in pairs( tileProperties ) do

        -- Get the current property
        local property = tileProperties[key]

        -- Print out its Name and Value
        print( property:getName(), property:getValue() )
    end
end

With the code saved just run your game, you will see something similar to the following in the Terminal:

Complete