How do I properly do a runtime generated texture on AnimatedSprite?

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

In AnimatedSprite script, I have it like this but it only shows a black texture.

var txt
var img

func _ready():
	img = Image.new()
	txt = ImageTexture.new()
	img.create(512,512,false,Image.FORMAT_BPTC_RGBA)
	txt.create(512,512,Image.FORMAT_BPTC_RGBA,0)
	img.fill(Color(255,255,0,255))
	img.lock()
	txt.create_from_image(img)
	self.frames.set_frame("canvas",0,txt)
:bust_in_silhouette: Reply From: dicethrower

Hey OrlanJRod, your post was helpful to me, I was trying to achieve something similar. I found a fix for the black texture. If you use Image.FORMAT_RGBA8 instead of the BTPC_RGBA the image will not be black. Also the call to txt.create is unnecessary since txt.create_from_image is taking the size and format from img.

Here is the code

var img = Image.new()
var txt = ImageTexture.new()

img.create(128,128,false, Image.FORMAT_RGBA8)
img.fill(Color(255,0,0))
img.lock()

txt.create_from_image(img)