Hello Guest

Author Topic: Is it possible to get at prefab data in a tk2dTileMap?  (Read 6927 times)

Porthos

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Is it possible to get at prefab data in a tk2dTileMap?
« on: September 05, 2012, 12:17:46 am »
I have a Final Fantasy style overworld. and I use a layer of the tile map to paint zones. (zone with lots of fire monsters in it, zone with few land monster in it, etc). I access the data I painted using the players intended position and   GetTileIdAtPosition GetTileInfoForTileId.

This works great for zones, but I also have specific towns and dungeons that need specific information on what level to load etc. I assume the ability to paint prefabs is used for this, but is there any way to get at the instantiated prefab using through the tk2dTileMap?

I realize that I could make the prefab a collider or I could just place my own prefabs and check the player position against my own array of towns and dungeons, but being able to do all the map stuff through the tile map seems way cleaner.

Is there any way to do this? (even just being able to uniquely identify a tile would be enough. 

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Is it possible to get at prefab data in a tk2dTileMap?
« Reply #1 on: September 05, 2012, 10:49:00 am »
GetTileIdAtPosition should do this exactly. It returns the tileId under the position & layer. It ignores the fact that this tile could be a prefab and all of that stuff - this is the raw data you want here.

Porthos

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Is it possible to get at prefab data in a tk2dTileMap?
« Reply #2 on: September 05, 2012, 07:08:15 pm »
Thanks for all the support. Your tool is great. Sorry I'm being a bit thick.

So GetTileIdAtPosition returns an int. I'm trying for say a string from a script on the relevant prefab.

GetTileInfoForTileId  returns a TileInfo which, looking at the code, has three members: a string, and int, and a float. All are general to any instance of that tile, I need the prefab instance associated with the specific tile id.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Is it possible to get at prefab data in a tk2dTileMap?
« Reply #3 on: September 05, 2012, 07:54:57 pm »
Ahaa. Now your question makes sense :)

There is no way to map this information right now. It wouldn't be practical to serialize this information. However, its not all that bad. You can either keep a catalog of all instanced prefabs and match them up by position (its trivial to get the position) - a dictionary initialized at startup with the position as the key should do the job.

Will that work for you?



What I'm going to do moving forward is add an additional behaviour per prefab instance, where you can tie in whatever data you'd like - as long as the behaviour derives from a particular base class, it will automatically populate it with specific info, like the X,Y & Layer values.


Porthos

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Is it possible to get at prefab data in a tk2dTileMap?
« Reply #4 on: September 05, 2012, 08:07:41 pm »
Yea, that should work just fine. I'll just tag the prefabs and get those objects by tag whenever a level loads. I'll keep an eye on future updates to the tilemap.

Thanks much.

ajk

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Is it possible to get at prefab data in a tk2dTileMap?
« Reply #5 on: May 18, 2014, 12:09:03 pm »

What I'm going to do moving forward is add an additional behaviour per prefab instance, where you can tie in whatever data you'd like - as long as the behaviour derives from a particular base class, it will automatically populate it with specific info, like the X,Y & Layer values.


Hey.

That's exacly what I would need for my game. I need my prefabs to gets somehow notified, what tile position they occupy.

Could you share mentioned base class, which would get autopopulated with tile data?

Cheers

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Is it possible to get at prefab data in a tk2dTileMap?
« Reply #6 on: May 18, 2014, 06:55:47 pm »
I used that method in one game but decided it wasn't going to be part of the toolkit - it was too game specific for it to be worth putting in there. It also added getcomponent overhead when used with any large numbers of sprites, so I have abandoned that approach as a general solution.

This may be a better solution that is easier to implement - create a behavior to the prefab that contains whatever string you need. In the awake function, make it add the value to a static dictionary OnEnable and remove it in OnDisable. Now when you need to find data at a particular tile, you can use this dictionary to find if it exists at a particular coordinate, and get the behaviour, and indirectly, the value.

ajk

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Is it possible to get at prefab data in a tk2dTileMap?
« Reply #7 on: May 20, 2014, 09:16:11 pm »
Thx for your reply.

Unfortunetely I need actual coordinates, not just some string to distinguish the object. But I already managed to handle that.

Well, I had another idea. Maybe if you would like it, you could consider implementing it someday.
What about sending message to instantiated prefab objects in tileMap? Something like

Code: [Select]
void OnTiled(int x, int y)
That way, if someone would want to perform some coordinate-specific logic, he would implement mentioned method.
« Last Edit: May 24, 2014, 11:33:02 am by ajk »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Is it possible to get at prefab data in a tk2dTileMap?
« Reply #8 on: May 21, 2014, 11:06:34 am »
Messaging would be too slow, which is why we planned that other method. I'll have to revisit this at some point if I get a chance.