Programmatically create an AtlasTexture in Godot

2min read
Godot Engine Tutorial Game Dev Open Source

December 4, 2018

GodotEngine AtlasTexture thumbnail

I’m currently working on a card game tutorial (that will soon be open sourced) and I needed to create an AtlasTexture resource programmatically, a task that I didn’t find documented anywhere in the Godot reference so I thought about creating this short post to describe how to do it.

What I tried

At first I tried to create an AtlasTexture with var t = AtlasTexture() but this was not working at all:

Invalid call. Nonexistent function 'AtlasTexture' in base 'TextureButton ('

The right way of doing it

So I went to the Godot Discord channel and I was told to use

var t =

And this worked, but I was a little bit disappointed.

The new() method is not listed in any of the reference docs in Godot so I coulnd’t know that it was even possible to create a resource in this way.

However after creating the texture, you can just assign the correct properties and then use it with your game nodes; here it is what I have in my script to create an AtlasTexture and assign it to a TextureButton node:

extends TextureButton

var CARD_HEIGHT = 256
var CARD_WIDTH = 256

func _ready():
    # create a new AtlasTexture resource and assign it to a variable
	var t =

	# load a texture resource into the "AtlasTexture.atlas" field
	t.atlas = load("res://assets/cards/cards_atlas0.png")

	# set the region (customize this code accordingly to your assets)
	t.region = (
        (card_index % CARDS_PER_ROW)*CARD_WIDTH, 
		(card_index / CARDS_PER_ROW)*CARD_HEIGHT, 

    # now you can assign the AtlasTexture resource to a "texture" field on any object
    # eg: for a TextureButton, like in my case you can write
    self.texture_normal = t 

Contributing to the doc

I also opened an issue the Godot docs repository, so that we can improve the documentation and ease this workflow for other Godot programmers (hopefully).

I didn’t propose changes directly because I don’t know the internals of Godot and I prefer to discuss the issue with more expert developers before:

Update [15 December 2018]: I contributed to the docs, it should be easier now for new users! I’m so glad to improve other programmers workflow! And the Godot people were also super welcoming, even if I didn’t read properly the contributing guidelines and made a mistake with my first pull request.

comments powered by Disqus