Hello Guest

Author Topic: Tilemap duplicating prefab  (Read 5378 times)

woca

  • Newbie
  • *
  • Posts: 12
    • View Profile
Tilemap duplicating prefab
« on: April 30, 2015, 07:56:39 pm »
I have run into an issue where the tilemap is duplicating the prefabs associated with tiles.

If I destroy a tile/prefab by hitting it with a raycast, I have a switch/case area that then deletes the prefab associated with that tile and chooses the case based on the tiles ID. If the tile ID matches the door, turn off the door script and remove the meshcollider before deleting the prefab, then set the tile to the floor tile and build the tilemap.

Another case is for a generator where it just needs to delete the game object then set the tile to the floor tile then build the map.

Now when I delete a tile, regardless of whether a prefab is tied to it, there is a chance that all the prefabs in that chunk will be duplicated.

I saw this post seemed to have this issue http://2dtoolkit.com/forum/index.php/topic,4426.msg20784.html#msg20784
however you addressed it in emails.

Was anything figured out? Was it the way they were handling setTile and Build?

Some help would be great!
« Last Edit: April 30, 2015, 08:01:24 pm by woca »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tilemap duplicating prefab
« Reply #1 on: May 03, 2015, 12:22:54 pm »
I'm guessing this is at runtime? The system doesn't keep track of prefabs at runtime after its created them.
The best way to avoid this is to not modify layers with prefabs, and keep the modifications isolated to a different layer.

woca

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Tilemap duplicating prefab
« Reply #2 on: May 03, 2015, 09:31:24 pm »
Yea this is at runtime. I only have one layer actually, the default layer 0.

And I am Ok with the system not keeping track of the prefabs, I have my own Dictionary that maps the tiles coordinates as a Vector2 to the prefab that was made for that tile. Although that seems strange. Why then does it sometimes duplicate the prefabs, and other times it doesn't? There is no consistency. And for one prefab, if it gets duplicated and one of them gets destroyed they all disappear. For another type of prefab, when it gets duplicated and one is destroyed the duplicates remain.

Putting my prefabs in a separate layer though would  complicate things far more than I really want.


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tilemap duplicating prefab
« Reply #3 on: May 03, 2015, 11:01:12 pm »
So internally, the system splits up tilemap into chunks. Only dirty chunks are ever rebuilt, and only prefabs in a dirty chunk will get duplicated. You can see the division if you expand the render data in the hierarchy...

You could just write a script to spawn the prefabs elsewhere, or... at runtime, never instantiate prefabs after the first time? A simple source change can make that happen.

woca

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Tilemap duplicating prefab
« Reply #4 on: May 03, 2015, 11:15:59 pm »
"So internally, the system splits up tilemap into chunks. Only dirty chunks are ever rebuilt, and only prefabs in a dirty chunk will get duplicated. You can see the division if you expand the render data in the hierarchy..."

Yea I know. My point is that you claim that once the prefabs are set "The system doesn't keep track of prefabs at runtime after its created them."

Well that just seems strange considering sometimes it duplicates prefabs, other times it doesn't. If the system didn't keep track of prefabs at all I would expect the prefabs to be duplicated every single time a Dirty chunk was rebuilt. This is not the case though. It is ostensibly random. And why deleting one of prefab A removes the all the duplicate prefab A's is also strange. Extra strange is that Prefab B can be deleted and all the duplicate Prefab B's will stay.

Right now I am considering instantiating the prefabs myself like you suggest, so at runtime if a tile has a tile ID I add the prefab at that tiles location and add it to my Dictionary mapping the Tile coordinates to the prefab object. I already do most of this anyway.

"at runtime, never instantiate prefabs after the first time? A simple source change can make that happen."  seems interesting, however, long term I think adding the prefabs myself would be better.


So I guess this is just a bug report. I was hoping in the thread I linked that turned into emails some fix was found.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tilemap duplicating prefab
« Reply #5 on: May 03, 2015, 11:39:21 pm »
Quote
Well that just seems strange considering sometimes it duplicates prefabs, other times it doesn't.
It doesn't duplicate them, it just recreates the ones in that chunk that it considered dirty and you end up with duplicates. This is what it should do by design, if you think its random submit a bug repro and I'll take a look.

Quote
And why deleting one of prefab A removes the all the duplicate prefab A's is also strange. Extra strange is that Prefab B can be deleted and all the duplicate Prefab B's will stay.
This is very suspicious. I can only think of one convoluted case where this could happen, if you can create a repro I'll be interested in taking a look.

woca

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Tilemap duplicating prefab
« Reply #6 on: May 04, 2015, 12:00:38 am »
"It doesn't duplicate them, it just recreates the ones in that chunk that it considered dirty and you end up with duplicates."
I didn't mean duplicate in the literal get object then copy that object sense, just in the sense the prefab is there and then another is made.

What makes it consider a prefab is dirty? You said the system doesn't keep track of prefabs so how would it know if one were dirty?
I was under the impression that a chunk was dirty if a tile in a chunk changed, so then on the next build all dirty chunks, AKA all chunks where a tile changed, are rebuilt. I don't get how a prefab can be dirty.

And I will try and get a case for you because it does happen randomly(ostensibly). I can have a 10 by 10 chunk with 1 tile that has a prefab tied to it and the other 99 tiles are just plain tiles. I can randomly set any of the 99 plain tiles to another plain tile and never mess with the 1 tile mapped to the prefab and randomly their will be another prefab.