Dynamically change tileset texture

colludiumcolludium Posts: 2Member
in 2D

When using a 47 tile texture for auto-tiling, I'd like to be able to dynamically load the whole texture such that the image for every tile changes at the same time. Is there a way to do this? I only see a method to change each tile's texture individually. Alternatively, I guess, I could copy/paste the .tres TileSet file and then change the resource id, but that seems like a less elegant solution. Thanks.

Comments

  • HaledireHaledire Posts: 30Member
    edited July 14

    A thought - instead of loading the Texture directly, create an ImageTexture resource and re-import the tileset textures as Image

    You can then make the Tileset for the Tilemap use the ImageTexture as the texture for the tiles. Because resources are shared, you can load a reference to the ImageTexture file and use it to swap the image of the tileset.

    So long as your tileset images are constructed the same, perhaps this could work for you?

    The TileTest.tres file here being just an ImageTexture resource, rather than having to switch out the "Tileset of the tilemap", you'd be changing the Image of the "ImageTexture being used by the Tileset" which is already loaded.

    You could probably even extend this to where you make a custom resource, that holds an array of tileset textures and the tileset resource you wish to change. With a setter, you can have it change the Tileset ImageTexture by changing an index variable.

    tool
    extends Resource
    
    class_name TileToggle
    export(Array, Image) var tex_array
    export(int) var index setget update_tileset
    export(Resource) var tileset
    
    func update_tileset(new_index):
        var extent = 0 if tex_array.empty() else tex_array.size() - 1
        index = int(clamp(new_index, 0, extent))
        if tileset is ImageTexture and tex_array[index] != null:
            tileset.image = tex_array[index]
    

    (note: tool here is just to make sure the setter works in the inspector)
    I need to stop editing this script to look nice...

  • colludiumcolludium Posts: 2Member

    @Haledire - That looks perfect. I'll give it a try - thanks!

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file