How do I add a tileset to a tilemap in code?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Socrates

I’d like to add an existing tileset to a tilemap in code. How would I go about doing this? I’ve tried preload() but I don’t see any way to create an instance of the tileset so I can’t refer to it later. This is my code right now, which doesn’t seem to do anything.

tilemap.tile_set = preload("res://scenes/tilesets/tileset_001.tres")

Oh, thank you, it’s working now. My error was elsewhere (didn’t have the underscore in front of my _ready() function :frowning: )

Socrates | 2018-06-11 17:38

:bust_in_silhouette: Reply From: Zylann

The code you used should basically do that, I don’t understand what else you really expect…
Contrary to nodes, a TileSet is a resource, so there is no need to make an instance of it. Loading it already give you the instance, and if it’s already loaded then Godot will just give you the same instance without reloading it.