playMaker

Author Topic: Tilemaps: is there any easy way to store data in a tile for a PM user?  (Read 4636 times)

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 824
A tilemap that could store values would prove to be a perfect 2D matrix simile, with as many columns and rows as needed.
This can be useful to give attributes to tiles as varied as the type of a tile, its economical worthiness, if it's occupied or damaged, etc.

Unfortunately the 'Tile' is an object of type Tile, it's not a mere Game Object, and it's not visible in the hierarchy as far as I know which means it cannot be called and can't be isolated to harbor a FSM that would contain variables.

Cells are the placeholders on the grid where tiles fit. The tilemap is divided and scaled at specific values. It's possible to get some basic data for cells but it's just stuff that Unity provides like position, size, etc.

Rules for tiles would have been interesting if it were possible to expand this utility beyond what it does, which is telling how tiles behave next to other tiles. Rules are useful during the filling process of a tilemap, the level design so to speak, but it is certainly not meant to be used at runtime.

The Arraymaker extension doesn't allow users to create an infinite amount of columns because the type of variables are logically only capable of holding a limited amount of slots. For the example, the largest type of variable, the quaternion, allows us to store four values in a row. We would need a special var type, a special row in fact, where there can be as many values to be stored in.
Since it's not available and would probably ruin the UI anyway, it's out of the picture.

We are left with Scriptable Objects as the only available solution it seems, but unfortunately that falls beyond the scope of Playmaker. See video here.

But there may be another solution:
TArray - Ultimate Serializable Array [C# / Unity] (this is installed under Packages, not Assets).
The user can then make this array stack over the tilemap so the cells in that array will correspond to occupied cells, therefore tiles, in the tilemap.
Each cell in the TArray can contain only one value in its most basic use but it's not a problem because it can store GameObjects.
Which means that for each type of tile in your game, especially those with different values from others, you can create a list of GameObjects acting like a database.
Create for example a TArray parent, have the TArray script on it and shove as children all different types of tiles you will have under the form of GameObjects. You can store prefabs that way. Each GameObject can have a FSM that stores data.

Then, when the game picks a given cell, checks if it has a tile, then it can look into the array at the corresponding coordinates for column and row, pick the GameObject and extract its values.

For this however you would need actions that can work with this extension so it can parse the TArray. Like get and set value at {x;y}, at the very least.

It's also annoying to have to populate the TArray manually.
You would need to run some kind of script that can populate the TArray automatically by parsing every single tile of the tilemap successively, row by row. For each tile, it would check its type (usually its name) and then fetch the correspond GameObject and store it as a GameObject value at the corresponding coordinates, so that say tilemap{32;6} finds a tile "wet dirt", then picks the GameObject WetDirt and stores it at TArray{32;6} coordinates.
The WetDirt GameObject would contain a bunch of values like speed, slipperiness, humidity, cover...

The issue with this extension is that it's not optimized for the Inspector's UI but doesn't use its own either. Too many columns or rows will bring Unity to a crawl.

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 824
Re: Tilemaps: is there any easy way to store data in a tile for a PM user?
« Reply #1 on: September 19, 2024, 10:46:45 AM »
Regarding TArray, It's also worth a trie to simply have several arrays on several GameObjects, each one dedicated to a specific parameter.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15620
  • Official Playmaker Support
Re: Tilemaps: is there any easy way to store data in a tile for a PM user?
« Reply #2 on: September 20, 2024, 07:37:02 AM »
Hi,

 A tile is a gameobject so you can add component to it and thus provide custom data.

Are you worried about the editor UI management of these tiles or the runtime handling of it?

Odin inspector might be the solution for the editor UI.

have you watch the other video from that guy you mentionned? I think it's more what you want:


Bye,

Jean

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 824
Re: Tilemaps: is there any easy way to store data in a tile for a PM user?
« Reply #3 on: September 20, 2024, 07:27:14 PM »
A tile is a gameobject so you can add component to it and thus provide custom data.

But the data is attached to the whole tilemap GameObject, while what I'm looking for is a way to have each tile carry its own data, and thus in a dynamic way so that some values can change, which requires being able to find the tile and read and edit the values at runtime.

Say you have a tilemap of 6 rows and 12 columns. The idea is to create a 2D array that has the same quantity of rows and columns, so that each tile gets its own dedicated slot at {x;y} in this array (imagine a [,] array here), where x and y are identical position references between the tilemap and the array. For the tilemap it says where the tile is located, for the array it says where its corresponding cell is found.

Thus far the most Playmaker-ish solution I can handle is the one I alluded to by using Arraymaker where I have one parent GameObject with an Array List Proxy that is titled "Rows". This parent has several children. All these children are listed in the Array List Proxy "Rows", with each child corresponding to a row.
Each child is name Row 0, Row 1, Row 2, etc.
Each child has an Array List Proxy of its own called "Columns", where each row in the array corresponds to a column. So for example if I have a grid with 12 columns, the ALP called "Columns" will have 12 fields, starting at 0 up to 11.

With this convoluted and ungainly system I can store any type of data that is supported by the Array List Proxy component, but it is not easy to read at all, contrary to TArray that plainly displays a 2D array.

It's also annoying because it is not practical to use negative values, so a cell can't have coordinates that drop below for both x and y, which means this "lowest" cell has to be on the bottom left of the tilemap, preventing anyone from deciding that the {0,0} cell would be in the center of the tilemap instead.

Quote
worried about the editor UI management of these tiles or the runtime handling of it?

Neither. I'm merely looking for a 1:1 "tilemap to 2d array" solution.

The video is much closer to what I'm looking for. Amusingly it's from the same author.
I'll see what I can do with that, I intuit that it's much closer to what I need but still needs some code, which I try to avoid. I do not know of an extension on the Asset Store that provides a 2D array that I could link to a tilemap all the while having Playmaker support with its own actions to change the values stored in each tile on the fly.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15620
  • Official Playmaker Support
Re: Tilemaps: is there any easy way to store data in a tile for a PM user?
« Reply #4 on: September 21, 2024, 05:12:10 AM »
Hi,

A 2d array is very easily achievable is you combine basic c# skills with Odin inspector for all the inspector UI. Odin is an amazing tool, you should get it if you plan on making such editor tools. I use it in every of my projects...

Let me know and I can give you a small example to get you started on 2d array.

Bye,

Jean

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 824
Re: Tilemaps: is there any easy way to store data in a tile for a PM user?
« Reply #5 on: September 23, 2024, 07:25:01 AM »
Mm okay, I hesitated acquiring the Odin tool for several years because to me it seemed perhaps overengineered for my simpler needs.

I will stick to my bizarre solution based on Arraymaker for now, it does work which is what matters.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15620
  • Official Playmaker Support
Re: Tilemaps: is there any easy way to store data in a tile for a PM user?
« Reply #6 on: September 24, 2024, 03:57:56 AM »
Hi,

Odin is probably the best editor assets there is currently imo.

Bye,

Jean