Hello Guest

Author Topic: Tilemaps and passing Prefab Data  (Read 8862 times)

Noogy

  • Newbie
  • *
  • Posts: 2
    • View Profile
Tilemaps and passing Prefab Data
« on: July 17, 2014, 05:00:15 am »
Hi all, I'm having trouble figuring out how to use the Data section of a Tilemap.

I know I can have a tile spawn a prefab, but am not sure how to pass or read the 'String/Int/Float' data. For example, if I spawn a coin, I'm assuming I can somehow read the int variable to determine the coin's value.

I'm also a bit confused as to how 2D toolkit determines the scale that a prefab is spawned. Right now it seems to spawn it based on the scale of the tilemap, but I'm wondering if I can have more control over this? Thanks!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tilemaps and passing Prefab Data
« Reply #1 on: July 18, 2014, 02:33:58 pm »
String int and float data are stored per tile TYPE, not per tile.
When you commit the tilemap, you can select individual prefabs and change properties on them. It should remember the changes.

Noogy

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Tilemaps and passing Prefab Data
« Reply #2 on: July 18, 2014, 06:50:21 pm »
String int and float data are stored per tile TYPE, not per tile.
When you commit the tilemap, you can select individual prefabs and change properties on them. It should remember the changes.

I understand it's per type, sorry if I wasn't clear. For example, I have two separate tiles in the palette, a single coin and a coin worth 5. I'd rather just have them both spawn the same prefab, but pass the integer to be either 1 or 5.

I'm not sure how I pass or read this integer though. I'm assuming somewhere in the coincontroller script's constructor I should be able to read the integer from the tilemap, but am unable to figure it out.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tilemaps and passing Prefab Data
« Reply #3 on: July 19, 2014, 04:30:42 pm »
Ok. If you're spawning prefabs, you dont need to use the tile properties on them. Just put your coin script on the prefab, then when committed change the integer on the instance to 1 or 5 or whatever you like. Dealing with the rest of it is exactly the same, you deal with it as though you've plcaed the prefab by hand, the tilemap editor doesn't need to fit into the picture here at all.

mrkake

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Tilemaps and passing Prefab Data
« Reply #4 on: February 10, 2015, 11:26:08 pm »
Sorry.. but what?? I found this post when searching and this is exactly what I want to do but the post doesn't explain how to do it at all.

I have created a prefab, with a script on it. I have added that prefab to the tile. I only see the "string/int/float" boxes... I don't know how to read the data from them,  and there are no boxes for the public properties on the script attached to the prefab.  How does one pass data into the prefab?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tilemaps and passing Prefab Data
« Reply #5 on: February 10, 2015, 11:41:55 pm »
Commit the tilemap, select the prefab instance and modify it. The tilemap editor keeps your prefabs.

mrkake

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Tilemaps and passing Prefab Data
« Reply #6 on: February 11, 2015, 05:19:06 am »
Ok yes I understand that, but my question is how to read the three fields in the Data tab, under the Prefab where it says "String" "Int" "Float".  Is there a way to have the prefab created and these values passed into the prefab ?  For my purposes, I want every tile of that type to create a prefab with the same values, and I don't want to click on every Prefab under the Tilemap Render Data and manually type the value in over and over again, but I also don't want to hard code this data in the prefab itself.

Is it possible to have the string/int/float values passed in to the prefab? And if not,  what is the recommended way to read the string/int/float values after the prefab is created? Will it even be possible since the tile has been replaced by the prefab?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tilemaps and passing Prefab Data
« Reply #7 on: February 11, 2015, 10:57:10 am »
Quote
Is it possible to have the string/int/float values passed in to the prefab? And if not,  what is the recommended way to read the string/int/float values after the prefab is created? Will it even be possible since the tile has been replaced by the prefab?
No it isn't. You'll need some kind of behaviour script to store the data on the prefab instance... The string / int and float values are there to let you find a tile you're on and get some data, eg. find out if its water or lava or something along those lines.

Quote
I want every tile of that type to create a prefab with the same values
Set a unity tag on the prefab?

mrkake

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Tilemaps and passing Prefab Data
« Reply #8 on: February 11, 2015, 01:24:36 pm »
I just want to try to explain why this is not convenient but I'm not sure it will be clear.

In our game, we need to create a prefab at every instance of these tiles, for example, we have a tile where the player starts and a tile where an enemy is. We need to create the player prefab or the enemy prefab,  but,  we are also using StrangeIoC and we need to perform an AddComponent on these objects, to add a specific script to the prefab dynamically, and it needs to be performed in the context of strangeioc otherwise the mediator does not get created.

So, my idea to solve this was to create a single "fake" prefab with a common script on it which contains some info about what the real prefab should be. Then, on startup after tk2d is ready,  a script inside strangeioc can locate all of these "fake" prefabs, and read the data from them , match it to a config value,  and then create all the "real" prefabs and destroy the "fake" prefabs. 

It can't be solved with a tag because you would then need a different fake prefab for every type.

The solution we have created now, is to remove the prefab from the tile in tk2d, put the special tiles on a layer behind so its not visible, store the data in the fields which I mentioned, but when the scene is loaded,  a nested loop through every single tile on the tilemap and check each tile if it contains some interesting data, and then create the prefabs inside our script under the strangeioc context where the mediator will be properly bound to the prefab/view.   Actually to me this isn't a totally terrible solution since the scan happens just once at scene load and it's relatively fast, but it is less than ideal.  (Perhaps another solution is create the "fake" prefabs with a script that lookup the tk2d tile based on transform position, in theory this could be faster than the nested loop?)

If instead the prefabs had that data already in it, things would be faster. But since we want external designers to be able to create levels and export them in .TMX , they need to be able to create these things once - we can't click on every prefab in the render data and set these values, which is why the tilemap data seems to be an ideal place, since that data is set once for every tile of that type.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tilemaps and passing Prefab Data
« Reply #9 on: February 11, 2015, 03:41:53 pm »
tk2d can't realistically write any data into a prefab / instantiated prefab, it'll need to know about the scripts on it to do so. You might as well modify the tk2d codebase to do this - just GetComponent<MyInstanceDataComponent> on the component and set whatever you want on it.