Hello Guest

Author Topic: Tiled Map Editor Help  (Read 6670 times)

Lavaflyer

  • Newbie
  • *
  • Posts: 9
    • View Profile
Tiled Map Editor Help
« on: August 12, 2014, 02:05:17 am »
So i know these questions might be rather noobish, but I'm creating a topdown rpg tactics game and i was hoping i could use the tiled map editor to lay out the levels then import them into unity with the tk2d tilemap. 

First I was wondering if the tk2d tilemaps support individual colliders.  It looks like to me the only way you can add colliders is through the sprite collection and then the tk2d tilemap just meshes all the colliders that touch eachother into one collider (this could be useful for a sidescroller where the collider(s) is the ground).  I want the user to be able to click on a tile and get its coordinates on a grid.

I was also wondering if anyone knew the best way to get other information from the tmx file.  For example, if some of my tiles are walls and i want them to be considered a blocker then i set that as a tile property in the tiled map editor.  Now what's the best way for me to find that information via unity and c#.  I read somewhere that a tmx file is the same as xml so could i just read that as an xml to get individual tiles properties.

Thanks for the help, if you guys have any other suggestions they are greatly apprechiated.  My game is going to have many levels/scenarios so i want to lay this out the best i possibly can.  Thanks!


LaserDinosaur

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Tiled Map Editor Help
« Reply #1 on: August 12, 2014, 05:07:28 am »
I want the user to be able to click on a tile and get its coordinates on a grid.

You could try using tk2dTileMap.GetTile (int x, int y, int layer). Once you get the tileID you can use GetTileInfoForTileID.
http://www.unikronsoftware.com/2dtoolkit/docs/latest/html/classtk2d_tile_map.html#a36ad0e3e4d3db0276d16c52f4438b5f6

Although you will probably have to convert your mouse to world coordinates first:
tk2dCamera.Instance.camera.ScreenToWorldPoint(Input.mousePosition);

« Last Edit: August 12, 2014, 05:10:16 am by LaserDinosaur »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tiled Map Editor Help
« Reply #2 on: August 12, 2014, 10:35:03 am »
Once you get the world point from your mouse ray, you can also use GetTileIdAtPosition to get the tile at a world position, if you dont care about tile positions.

Lavaflyer

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Tiled Map Editor Help
« Reply #3 on: August 12, 2014, 05:39:50 pm »
EDIT: now i got GetTileInfoForTileId to print out tk2dRuntime.TileMap.TileInfo with
print (GameObject.Find("TileMap").GetComponent<tk2dTileMap>().GetTileInfoForTileId(505));
but when i try print (GameObject.Find("TileMap").GetComponent<tk2dTileMap>().GetTileInfoForTileId(505).stringVal); it returns an empty string.  If you could provide an example of how your supposed to use GetTileInfoForTileId that'd be great, thanks!

I tried using GetTileInfoForTileID and that function looks very promising but I put tile properties on all my tiles (in tiled map editor) and nothing printed out after i ran this code:

Code: [Select]
for (int nIndex = 0; nIndex <1300; nIndex ++) {
if (GameObject.Find("TileMap").GetComponent<tk2dTileMap>().GetTileInfoForTileId(nIndex) != null) {
print (GameObject.Find("TileMap").GetComponent<tk2dTileMap>().GetTileInfoForTileId(nIndex));
}
}
Am I doing or understanding something wrong here.


Finally is there any way to get a tile's gameobject or is that lost in the tilemapping?
« Last Edit: August 12, 2014, 06:10:14 pm by Lavaflyer »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tiled Map Editor Help
« Reply #4 on: August 13, 2014, 11:26:42 am »
The tileinfo is per tile in your spritesheet, not per painted tile... Where are you getting 1300 and 505?

Lavaflyer

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Tiled Map Editor Help
« Reply #5 on: August 13, 2014, 04:04:50 pm »
The tileinfo is per tile in your spritesheet, not per painted tile... Where are you getting 1300 and 505?

505 is the index of a tile i gave a property (in tiled map editor) in my sprite collection and ya I understand its per tile in spritesheet but I need an example of how to reference it and find its respective properties (info); thanks!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tiled Map Editor Help
« Reply #6 on: August 13, 2014, 04:48:37 pm »
Ok... The tileid is the id of the tile. i.e. what you get from tile.GetTile(x,y,layer). Its also the index of the sprite in the sprite collection. How you reference it is exactly how you've used it, but your IDs must be wrong if you're not getting the correct values back.

Lavaflyer

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Tiled Map Editor Help
« Reply #7 on: August 13, 2014, 05:11:17 pm »
Just to make sure we're on the same "page" I attached a picture of the property i set to one of my tiles in tiled map editor.  And this is the link to the tiled map editor i'm using http://www.mapeditor.org/ Now here's my code:

Code: [Select]
int nCol;
int nRow;

if (GameObject.Find("TileMap").GetComponent<tk2dTileMap>().GetTileAtPosition(Camera.main.ScreenToWorldPoint(Input.mousePosition),
out nCol,out nRow)== true) {
print (nCol+","+nRow);
int nSpriteId = GameObject.Find("TileMap").GetComponent<tk2dTileMap>().GetTileIdAtPosition(Camera.main.ScreenToWorldPoint(Input.mousePosition),0);
print (nSpriteId);
print (GameObject.Find("TileMap").GetComponent<tk2dTileMap>().GetTileInfoForTileId(nSpriteId).stringVal);
}
else {
print ("Invalid");
}

and the final print (for GetTileInfoForTileId) prints out an empty string ""
      
« Last Edit: August 13, 2014, 06:33:47 pm by Lavaflyer »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tiled Map Editor Help
« Reply #8 on: August 13, 2014, 08:47:09 pm »
Oh joy, I have been reading this as Tile map editor, missing the d.
So yeah, we haven't been on the same page!

The tiled import doesn't import that data from Tiled as there isn't an equivalent in tk2d. The thing to do will be to create your own class that it imports the data into in parallel to the tk2dTileMap data. The importer is in tk2dTileMapImporter.cs and is pretty much standalone.