# Is it possible to generate a TileMap in an extension?

**URL:** https://forum.makecode.com/t/is-it-possible-to-generate-a-tilemap-in-an-extension/19187
**Category:** Help
**Tags:** extension
**Created:** [April 14, 2023, 5:12pm UTC](https://forum.makecode.com/t/is-it-possible-to-generate-a-tilemap-in-an-extension/19187 "2023-04-14T17:12:26Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![EpicKitty](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/epickitty/32/8358_2.png) [@EpicKitty](https://forum.makecode.com/u/EpicKitty)
#### Post date: [April 14, 2023, 5:12pm UTC](https://forum.makecode.com/t/is-it-possible-to-generate-a-tilemap-in-an-extension/19187/1 "2023-04-14T17:12:26Z")

</div>

I made a block that converts a TileMap to stringified JSON (For saving TileMaps to the device with the Settings extension), but now that I need to write a block that converts the stringified JSON back to a Tilemap I need to generate a new TileMap, the closest built-in function I found was the `tiles.createTilemap` function which takes in the arguments `data: Buffer, layer: Image, tiles: Image[], scale: TileScale` but I don’t know how to use data, layer, and scale. They’re used to generate a TileMap from assets but I’m generating one from JSON not from assets.

An example of the stringified JSON of a TileMap:

 ![tilemappp](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/5/5657c800ca188b0cb16496e2363fb825b4852961.png)  
Converts to:

```auto
{"size":[3,3],"tiles":[[{"id":1,"wall":true},{"id":0,"wall":false},{"id":1,"wall":true}],[{"id":0,"wall":false},{"id":0,"wall":true},{"id":0,"wall":false}],[{"id":1,"wall":false},{"id":0,"wall":true},{"id":1,"wall":false}]]}

```

---

<div class="post-metadata">

### Author: ![EpicKitty](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/epickitty/32/8358_2.png) [@EpicKitty](https://forum.makecode.com/u/EpicKitty)
#### Post date: [April 16, 2023, 10:07pm UTC](https://forum.makecode.com/t/is-it-possible-to-generate-a-tilemap-in-an-extension/19187/2 "2023-04-16T22:07:14Z")

</div>

I’m trying to create a new empty TileMap with `tiles.createTilemap(hex`, img`, [], TileScale.Sixteen);` but I cant change the width and height because they’re constant values. So I can’t generate a TileMap like this:

```auto
const data = {
    "size": [3, 3],
    "tiles": []
}

const tilemap = tiles.createTilemap(hex``, img``, [], TileScale.Sixteen);

tilemap.width = data.size[0]; // Error, 'width' is a constant value.
tilemap.height = data.size[1]; // Error, 'height' is a constant value.

```

---

<div class="post-metadata">

### Author: ![EpicKitty](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/epickitty/32/8358_2.png) [@EpicKitty](https://forum.makecode.com/u/EpicKitty)
#### Post date: [April 24, 2023, 2:13pm UTC](https://forum.makecode.com/t/is-it-possible-to-generate-a-tilemap-in-an-extension/19187/4 "2023-04-24T14:13:57Z")

</div>

> [@EpicKitty](#):
>
> I’m trying to create a new empty TileMap with `tiles.createTilemap(hex`, img`, [], TileScale.Sixteen);`

Oops, markdown error! It’s suppose to be `tiles.createTilemap(hex​`​`, img​`​`, [], TileScale.Sixteen);` and not `tiles.createTilemap(hex`, img`, [], TileScale.Sixteen);`. And as I expected, the code doesn’t work. Does anyone know how to generate an empty TileMap with a specific width and height? Thanks!

---

<div class="post-metadata">

### Author: ![Samsano](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/samsano/32/8307_2.png) [@Samsano](https://forum.makecode.com/u/Samsano)
#### Post date: [April 24, 2023, 5:37pm UTC](https://forum.makecode.com/t/is-it-possible-to-generate-a-tilemap-in-an-extension/19187/5 "2023-04-24T17:37:53Z")

</div>

can you not change:

```auto
const data = {
    "size": [3, 3],
    "tiles": []
}

```

to:

```auto
data = { // Change from constant to normal
    "size": [3, 3],
    "tiles": []
}

// Set "size" to something higher in an 'on game update' or 'while True' 
// or some other code

```

---

<div class="post-metadata">

### Author: ![EpicKitty](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/epickitty/32/8358_2.png) [@EpicKitty](https://forum.makecode.com/u/EpicKitty)
#### Post date: [April 24, 2023, 6:00pm UTC](https://forum.makecode.com/t/is-it-possible-to-generate-a-tilemap-in-an-extension/19187/6 "2023-04-24T18:00:00Z")

</div>

It’s an example, and I can’t define variables without var/let/const (This is JavaScript). The low size is also for an example.

---

<div class="post-metadata">

### Author: ![Samsano](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/samsano/32/8307_2.png) [@Samsano](https://forum.makecode.com/u/Samsano)
#### Post date: [April 24, 2023, 6:33pm UTC](https://forum.makecode.com/t/is-it-possible-to-generate-a-tilemap-in-an-extension/19187/7 "2023-04-24T18:33:33Z")

</div>

then could you not do

> let data = { …

---

<div class="post-metadata">

### Author: ![EpicKitty](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/epickitty/32/8358_2.png) [@EpicKitty](https://forum.makecode.com/u/EpicKitty)
#### Post date: [April 24, 2023, 8:10pm UTC](https://forum.makecode.com/t/is-it-possible-to-generate-a-tilemap-in-an-extension/19187/8 "2023-04-24T20:10:31Z")

</div>

The variable doesn’t get changed, so it can be const instead of let. Const also uses less memory.

---

<div class="post-metadata">

### Author: ![yoxd707](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/yoxd707/32/8506_2.png) [@yoxd707](https://forum.makecode.com/u/yoxd707)
#### Post date: [April 25, 2023, 4:30pm UTC](https://forum.makecode.com/t/is-it-possible-to-generate-a-tilemap-in-an-extension/19187/9 "2023-04-25T16:30:40Z")

</div>

The buffer must have a size of the number of tiles in x \* the number of y + 4 bytes. Since the first two bytes are used to specify the x size of the tilemap and the second two for the y size.

```auto
let buffer = Buffer.create((xSize * ySize) + 4);
buffer.setNumber(NumberFormat.UInt16LE, 0, xSize);
buffer.setNumber(NumberFormat.UInt16LE, 2, ySize);

```

and the image should be the same size as the tilemap

`image.create(xSize, ySize)`

it would fit you like this…

```auto
const xSize = 16; // tilemap width
const ySize = 16; // tilemap height
const buffer = Buffer.create((xSize * ySize) + 4);
buffer.setNumber(NumberFormat.UInt16LE, 0, xSize);
buffer.setNumber(NumberFormat.UInt16LE, 2, ySize);
const tilemapImg = image.create(xSize, ySize);
const tilemap = tiles.createTilemap(buffer, tilemapImg, [], TileScale.Sixteen);

```

---

<div class="post-metadata">

### Author: ![Samsano](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/samsano/32/8307_2.png) [@Samsano](https://forum.makecode.com/u/Samsano)
#### Post date: [April 25, 2023, 5:54pm UTC](https://forum.makecode.com/t/is-it-possible-to-generate-a-tilemap-in-an-extension/19187/10 "2023-04-25T17:54:50Z")

</div>

I was thinking more in this direction:

```auto
let SizeVar: number 

SizeVar = 3 // size as a variable

let data = { // The list / Dictionary
    'size' : [3, 3]
}

game.onUpdate(function () {
    data.size[0] = SizeVar // Assign different positions to the 'size' variable
    data.size[1] = SizeVar
})

game.onUpdateInterval(1000, function () {
    SizeVar += 1 // Change SizeVar by 1 every second
})

```

The code assigns ‘size’ to to a variable in addition to appending it to our ‘data.size’ list

---

<div class="post-metadata">

### Author: ![EpicKitty](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/epickitty/32/8358_2.png) [@EpicKitty](https://forum.makecode.com/u/EpicKitty)
#### Post date: [April 25, 2023, 6:23pm UTC](https://forum.makecode.com/t/is-it-possible-to-generate-a-tilemap-in-an-extension/19187/11 "2023-04-25T18:23:18Z")

</div>

What? I’m not trying to increase the size of the tilemap, I’m trying to generate a new empty tilemap with a specified width and height. What would I need to use this code for?

---

<div class="post-metadata">

### Author: ![EpicKitty](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/epickitty/32/8358_2.png) [@EpicKitty](https://forum.makecode.com/u/EpicKitty)
#### Post date: [April 26, 2023, 5:59pm UTC](https://forum.makecode.com/t/is-it-possible-to-generate-a-tilemap-in-an-extension/19187/12 "2023-04-26T17:59:27Z")

</div>

Hmm, no errors but the tilemap is empty even after I call `tilemap.setTile(x, y, id)`. The only parameter that you left empty was `tiles: Image[]`, maybe I have to give it the image of every tile in the assets? Wouldn’t that use too much memory? I can access `tiles: Image[]` with `tilemap.getTileset()` and the output is `Image[]` . But when I log the variable (Either converted to JSON or not), it just logs `[object Object], [object Object], [object Object], ...` or `{{}, {}, {}, ...}`. Maybe I have to pass through every image in `getTileset()` into the converted JSON from a tilemap, like this (Look in the tileset key):

```auto
{
    "size": {"width": 3, "height": 3},
    "tiles": [
        // Array of ID and walls
    ],
    "tileset": [
        [
            id: 0,
            image: img`22221919999995888888888888333329855555`
            // Just an example, hex doesn't work like that in JSON
        ]
    ]
}

```

---

<div class="post-metadata">

### Author: ![AqeeAqee](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/aqeeaqee/32/5424_2.png) [@AqeeAqee](https://forum.makecode.com/u/AqeeAqee)
#### Post date: [April 26, 2023, 8:31pm UTC](https://forum.makecode.com/t/is-it-possible-to-generate-a-tilemap-in-an-extension/19187/13 "2023-04-26T20:31:54Z")

</div>

To get a clue of build a tilemap with code, see the “tilemap.g.ts” file in your projet, after created a tilemap.

---

<div class="post-metadata">

### Author: ![yoxd707](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/yoxd707/32/8506_2.png) [@yoxd707](https://forum.makecode.com/u/yoxd707)
#### Post date: [April 26, 2023, 10:32pm UTC](https://forum.makecode.com/t/is-it-possible-to-generate-a-tilemap-in-an-extension/19187/14 "2023-04-26T22:32:19Z")

</div>

To place tiles you must add the images to the empty array, and the id of setTile(x, y, id) would be the index of the image in the array.

```auto
const tilemap = tiles.createTilemap(buffer, tilemapImg, [/* Fill this with your images */], TileScale.Sixteen);

```

Also you must call tiles.setTilemap to set the tilemap on the scene, otherwise you won’t see the changes you make to the tilemap when you call setTile.

```auto
tiles.setTilemap(tilemap);

```

---

<div class="post-metadata">

### Author: ![jwunderl](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jwunderl/32/5308_2.png) [@jwunderl](https://forum.makecode.com/u/jwunderl)
#### Post date: [April 26, 2023, 10:33pm UTC](https://forum.makecode.com/t/is-it-possible-to-generate-a-tilemap-in-an-extension/19187/15 "2023-04-26T22:33:43Z")

</div>

One minor point, you should use `tiles.setCurrentTilemap(tilemap)` instead of `tiles.setTilemap(tilemap)` as setTilemap is deprecated / doesn’t play nice with variables and decompiling to blocks ([https://github.com/microsoft/pxt-common-packages/blob/stable10.3/libs/game/tilemap.ts#L600](https://github.com/microsoft/pxt-common-packages/blob/stable10.3/libs/game/tilemap.ts#L600))
