Hello Guest

Author Topic: Generating Tilemaps  (Read 5678 times)

BigDaddio

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
Generating Tilemaps
« on: October 07, 2013, 02:31:17 am »
OK I am creating a tilemap in code completely. No drawing, works great so far but there are some issues. I want all my "doors" to be in layer 3 and the collider to be a trigger not a collider. I have seen that people say do this

Quote
create a prefab which has a trigger collider on it, and hook it up to the prefab spawning system. Now when that tile is spawned, it'll act as a trigger.

WTF? This makes no sense to me, like I do not understand what you are even saying.  Are you saying somehow I can create a separate sprite and set it up then insert it into the tilemap data or what?

On top of that the collders are not really attached to a tile, they are a single large thing. finding the tile that has been hit is a real pain.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Generating Tilemaps
« Reply #1 on: October 07, 2013, 11:16:12 am »
It'll probably make sense if you create it using the tilemap editor UI first.
Basically, you create a prefab, attach it to the data (http://unikronsoftware.com/2dtoolkit/doc/latest/tilemap/tutorial.html - part 5) and the tilemap system will spawn the prefab instead of drawing the tile normally.

You create your prefab with the collider, so it wont be baked in to the "world" collider but rather exist in isolation. If you set up your prefab as a trigger, it will behave as one. Once you set it up and commit, you can select it and modify whatever properties you like on the instance - set up links to open doors or whatever you like. Its just a normal prefab, so you can attach your own scripts to it.

But if you're creating it in code, I'm at a loss as to why you'd use this? This system was designed so you could use it as a level editor, paint stuff into the tilemap and have it be replaced with an instantiated prefab. Surely it'll be just as easy to instantiate your trigger prefab wherever you need to if you're doing everything in code? tilemap.GetTilePosition will give you the world position to use. Its not that you can't use it, it just feels like a really roundabout way to get what you need.

BigDaddio

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Generating Tilemaps
« Reply #2 on: October 07, 2013, 10:59:01 pm »
Why would I want to do this as opposed to draw it out? Have you heard of Minecraft or Terreria or any of those?  ;)

I am building a roguelike. If you are familiar the total premise of such is that the playmap is procedurally generated. Diablo all of them have procedurally generated maps.

Anyhow what I need is a way to find the Prefab tile and use Destroy on it. I believe I can just maintain a list. But if you have any better way. When you see my product (and you will) you will totally understand. The only other way I can think of doing it is to create a set of tiled rooms as prefabs and then attach them together into a giant map.

BTW does your licensing say I have to list you in the credits?


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Generating Tilemaps
« Reply #3 on: October 07, 2013, 11:40:33 pm »
I understand  procedurally generating worlds - I just don't get why you'd wanna procedurally generate tiles, link prefabs to that tile type, and in turn spawn prefabs, instead of simply spawning the prefabs at the correct positions. I don't get why you'd want that extra level of indirection. That part of the system was designed for painting tiles in and swapping them out for prefabs, works really well for what it does but it makes very little if any sense when generating stuff in code.

You can create tiled rooms as prefabs and sitch them together too, whatever works best for you. I know a few people who have implemented a terraria style infinite world with tk2d. Definitely doable.

The licensing says nothing about credits, you don't have to list anything anywhere.

BigDaddio

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Generating Tilemaps
« Reply #4 on: October 08, 2013, 08:20:50 am »
I use the tiles I guess I was explaining it wrong, I am generating tilemaps! the tiles are all created in the editor but the maps are made on the fly.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Generating Tilemaps
« Reply #5 on: October 08, 2013, 11:44:13 am »
So I understand this correctly - you're setting up the tilemap object in the editor ready to paint in, and then you want to generate the tilemap content itself at runtime? Even here - I think it would make more sense to generate your "tile" data using tilemap.SetTile(x, y, ...) and then spawn interactive elements explicitly.

Eg.

Switch switch = Instantiate(switchPrefab, tilemap.GetTilePosition(x, y), Quaternion.identity) as Switch;
Door door = Instantiate(doorPrefab, tilemap.GetTilePosition(x + 10, y), Quaternion.identity) as Door;
switch.target = door;

The main thing here is that you need to link up one interactive item to another. You can do that visually in the editor if you were painting in the tilemap, but its counterintuitive using that same mechanism from code - I think something like the above works a lot better - also feels like it fits better into unity workflow.