Custom data in tiles spawned via SetTile

Hello,

I am spawning my tiles randomly on start, here’s a simplified version of what I’m doing.

         for (int x = 0; x < gridSizeX; x++)
         {
             for (int y = 0; y < gridSizeY; y++)
             {
                         tileMap.SetTile(new Vector3Int(x, y, 0), groundTileBase);
             }
         }

I would like to have custom data in the groundTileBase tile, walk speed would be a good example.

How can I go about putting custom data into the groundTileBase?

I see many tutorials on custom tile scriptable objects, but they all seem to presume the use of a tile palette, which I am not using since I spawn the entire world through code.

Can anyone direct my on how to put custom data into my tiles when spawning them in this way?

Thanks!

Instead of using Tile (which actually IS derived from a ScriptableObject), you can create your own derivation of Tile such as RazputinTile and give it extra data fields.

Now you can create instances of those on disk, OR… you can use the single “base” one from the disk and at runtime inject fresh information into it.

The catch is that as you create the field of tiles, or rather RazputinTiles, you would need to be sure to do these things:

  • have a reference to your groundTileBase (now a RazputinTile)
  • instantiate a fresh copy of it:
var copy  = Instantiate<RazputinTile>( groundTileBase);

copy.customData1 = 123;
copy.customOtherData2 = “Hello!”;[/code]

  • and use that to SetTile()

Those copies will go away when you change scenes, generally, or you can keep a reference to them and destroy them all yourself.

1 Like

As KD said, you can put whatever data fields and logic into a tile but when the scene is saved the values in the fields aren’t saved with the scene.

there are ways to get around that and have data saved in scenes but it was an immense amount of coding to get it to work properly on a systems level.

Thanks for the response, this will be good for standard scriptableobject stuff, like the walkspeed I mentioned.

However I’m thinking more for data thats editable during runtime, so changing one scriptable would change them all no? And wouldn’t be good for what I guess I actually want

I think a better example of what I want is the ability to flag the number of times a tile has been changed during world generation

Yes, but not if you do what I suggested: instantiate fresh copies for each cell, which could make sense in your usecase.

Oh I see, okay I’ll give it a try, thank you!

It’s pretty cool… those copies can still be double-clicked on to see and edit them in the editor, but they die when you stop the game, unless of course you do standard savegame stuff, in which case this tile data just becomes part of your saved state that you collect and write to disk and read back in.

1 Like

I ended up just throwing walking speed and the x/y coordinates in there as a test and yup, that’s pretty much what I wanted. Thanks :slight_smile:

1 Like