0

I have a TileSet defined in a file called CountTiles.sks. In that I have a tile group rule that has several variants. In my GameScene.swift file I want to fill a tileMapNode will specific variants, so I thought I'd use the setTileGroup method that includes a definition, but to call it, I need to already have that definition stored in a variable. Is there any way to get the tileDefinition using its name? The only way I can seem to get it is by painting it into a specific tile and getting it from that tile during setup. This is undesirable.

Is there any way to access a tileDefinition that has been set in a .sks file without having a specific instance of that tileDefinition?

Taylor
  • 547
  • 2
  • 7
  • 16

1 Answers1

0

You can access a Tile Definition through the SKTileMapNode e.g. when you add the SKTileMapNode to the Map Editor it has an associated SKTileSet. So if your CountTiles.sks has a structure like this;

  • BackgroundTiles (Tile Set)
    • Tiles (Tile Group)
    • Tile (Tile Group Rule)
      • Tile1 (Tile Definition)
      • Tile2 (Tile Definition)
      • Tile3 (Tile Definition)

You can access a specific Tile Definition e.g. Tile1 with a function that calls into an SKTileMapNode named background like this;

func backgroundTileDefinition(key: String) -> SKTileDefinition {
    guard let backgroundLayer = childNode(withName: "background") as? SKTileMapNode else {
        fatalError("Background node not loaded")
    }

    guard let backgroundTile = backgroundLayer.tileSet.tileGroups.first(where: {$0.name == "Tiles"}) else {
        fatalError("TileSet not found")
    }

    guard let backgroundTileSetRule = backgroundTile.rules.first(where: {$0.name == "Tile"}) else {
        fatalError("Tileset rule not found")
    }

    guard let backgroundTileDefinition = backgroundTileSetRule.tileDefinitions.first(where: {$0.name == key}) else {
        fatalError("Tile definition not found")
    }

    return backgroundTileDefinition
}

Call the function like this;

let backgroundTileDefinition = backgroundTileDefinition(key: "Tile1")
Mark Brownsword
  • 2,287
  • 2
  • 14
  • 23