#How to add an image to a TextureButton programmatically?

5 messages · Page 1 of 1 (latest)

prime coyote
#

Ok, so I know I can drag an image onto a texture button, and get the image showing up correctly, but I want to programmatically swap out the images. I tried making a new Texture2D add setting the .TextureNormal of the TextureButton to the image, but it only shows a white square when I do it this way. I suspect I have to call the .Draw(Rid canvasItem, Vector2 position) method, but I don't understand Rid very well, so not sure how to make this happen. Am I doing this wrong? What am I missing?

prime coyote
#

Bump

tiny scaffold
#

it seems you're creating a Texture, but you're not filling the image with anything. I guess you should reference the image you want to swap, and then assign it with .TextureNormal

tiny scaffold
#

something like this

`[Export] private Texture2D textureToChange;
[Export] private TextureButton textureButton;

public override void _Ready() {
textureButton.TextureNormal = textureToChange;
}`

prime coyote
#

The big issue was actually how I was creating it. I needed to use GD.Load instead of how I was trying to create it. The draw method is not how you make a texture instance. I learned things.