Tilemap creation in script (SOLVED)

Hi all! I wan’t to ask if there’s any way to create tilemap through script. For example do random map generation?
Thanks in advice!

Yes, you can create a tilemap programatically using tilemap.set_tile(). You need to do two things first though:

  1. Create an empty tilemap
  2. Mark the corners of the tilemap to define it’s bounds

Once you have the above two steps done you’ll be able to interact with the tilemap using the set_tile() function. Remember that you cannot go outside the bounds you set in your empty tilemap (use tilemap.get_bounds() to get the bounds and use it when you interact with the tilemap.

2 Likes

hey there.

Is there a piece of sourcecode how to create a tilemap via script?

There are several snippets how to set and read from a tilemap in the docs: http://www.defold.com/ref/tilemap/

Thanks, I’ve read this.

It may be a stupid question or -maybe too late- … how to instanciate the tilemap?
tilemap.set() truly drops an error :smiley: … tilemap not instanciated :smiley:

oh and - if you are already here -
how to “define” or do the bounds?

You put a tilemap component in a game object and put the game object in the main collection.

The bounds are static and have to be drawn on the map.

Note that it’s enough to place one tile at each corner to set the bounds.

ah thats what I needed to know :slight_smile:
Thanks!

1 Like

Can’t set any indexes besides 0 and 1 for my tilemap. (SOLVED) kind of

I’ve just created a tilemap and it’s only logical. So the tile source I use, has only one sprite to set the bounds. Once I try to fill that tilemap with my own object indexes, it says that it has to be 1 or 0. Can I somehow extend that range via script?

Solution Just broke my tilesource into 256 pieces. It did the trick. Still it would be great to have only logical tilemap object. Maybe even with some built-in tricks like pathfinding.