How to use an array to store per-tile data with a tilemap?

0 favourites
From the Asset Store
Each tile 64 by 64 pixels - Ultimate Tile Set for your awesome games
  • I've had trouble using arrays before and found a lot of information on how to use it with a Tilemap and what it can do.

    I think it can store information relating to a position? But that's about it.

    Could a array make each tile act as a unique object when it comes to information? For example, can each tiles store separate variables?

    If yes, how can I set an array with my tilemap if the tiles are not place uniformly:

    Thank you,

  • There are several ways to do this.

    How many values do you need to store for each tile? Will the set of values be the same for all tiles, or different? For example, water tiles will have "depth" value, ground tiles will have "walk_speed" and "solid" values etc.

    Will there be many tiles without any values? You can think of a Tilemap as a 2D array, which stores tile type (ground, sand, water, rock). So you don't have to store this info in the array.

    Do you need to store the array with all these values in the project, or will it be randomly generated in runtime?

  • You just match the x and y tile id to the x y position in the array, and use the z slots 0, 1, 2 etc to store the values. You just need to keep track of what the values relate to for each slot.

  • You just match the x and y tile id to the x y position in the array, and use the z slots 0, 1, 2

    If one tile has 10 values and others have 0-2, this means the array will need to be 10 elements deep for just that one tile. It will be a waste of resources. In this case I would store the list of values as text ("1,2,3"), or as a JSON string.

  • Do empty array slots take up resources? I dont know.

  • Just tested - an empty 1000x1000x10 array uses ~100MB extra RAM. And it increases save size by about 2.5MB

  • wow crazy.

  • 100 mb doesn’t sound too bad actually. If the default type is a 64bit number it would be 1000x1000x10x8 = ~80mb

    Js also stores the type per value, and typically resizable arrays are made to allocate a bit more memory to make resizing faster.

    Presumably you could use the binary object which uses typed arrays under the hood to be more memory efficient since they don’t resize and share the same type for all the values.

    Anyways, in regard to the op I’d still just use an array the same size as the tilemap. It’s the simplest solution.

  • There are several ways to do this.

    How many values do you need to store for each tile? Will the set of values be the same for all tiles, or different? For example, water tiles will have "depth" value, ground tiles will have "walk_speed" and "solid" values etc.

    Will there be many tiles without any values? You can think of a Tilemap as a 2D array, which stores tile type (ground, sand, water, rock). So you don't have to store this info in the array.

    Do you need to store the array with all these values in the project, or will it be randomly generated in runtime?

    1. I would need to store about 4 variables For each tiles that would change independently during the game on certain condition

    2.The values won’t be random

    I want to do something line this for the tiles variable:

    (Ownership: unowned or country1 or country2)

    (State: Incorporated or non-incorporated)

    (And finally a variable that update if for exemple a blue tile is near an other tile mark as Unowned)

    🙏

    Also if I set an array the same size if the tilemap and sub-divide it by each tiles will it store these values as if they where « in »the tiles?

  • I mean if you have a tilemap with 100x100 tiles and have an array of size 100,100,1 you can consider them related even though they aren’t. That would give you an array location for every tile.

    The array would have whatever value you put in it. As an example look at only one tile, what value would you want in that location, and how do you want that set? Once you have that you can do it for all the tiles by looping over all the tiles and doing the same logic for each.

    You could make a different array for each variable you want per tile, or make the size 100x100x4. I’d go with whatever was easier to understand and read.

  • My tilemap is iregular its not a 100x100 (like the picture above), Also are there tutorial about using arrays with tilemaps, i haven<t found any and it owuld really help.

    Ok thank you a lot, I will try something again.

  • Make the array the same size as your tilemap (width and height). And if you need to store 4 values per tile - set array depth to 4.

    Then, for example, if you need to get values for tile (x:30, y:50), you can use these expressions:

    Array.at(30,50,0)

    Array.at(30,50,1)

    Array.at(30,50,2)

    Array.at(30,50,3)

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I tried doing something that would pick up the position of each tiles at start of the layout.(doing it manually would be way too long) but it doesnt work, only the "set size of the array" event works. I think there's something wrong with my loops too.

    I have never worked with arrays before and tilemaps so I am completly lost.

  • First, you need to use "AJAX On Completed", or at least insert "Wait for previous action to complete" after AJAX request. Your array is empty because of that.

    Tilemap has expressions MapDisplayWidth and MapDisplayHeight which return number of horizontal and vertical tiles.

    And I don't understand why you are saving loopindex("x") and loopindex("y") in the array. If your array is the same size as the tilemap, then you can use its X/Y index for addressing.

    Tilemap.TileAt(X, Y) - tile at X/Y

    Array.at(X, Y) - value for that tile

  • Thanks a lot for the help, I understand It better now. 🙏

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)