Image empty error after .create_from_data(...)

Godot Version

Godot 4.x

Question

I’m having some trouble generating an ImageTexture. I’m getting an “Invalid image: image is empty” error.
Based on my testing, the issue appears to be at dyn_image.create_from_data(). I’m passing in a valid width, height, and PackedByteArray (I’m printing them to the terminal), but when I print the height and width of dyn_image, they’re both 0. I must be fundamentally misunderstanding something about how create_from_data works.
I thought it could be a format error but I verified the format enum of b.gradient.get_image() matched the one I was passing into create_from_data. I’m really not sure where else to go from here.

You might need to wait for one frame for image to be rendered. Try adding this before image_texture.create_from_image(dyn_image):

await RenderingServer.frame_post_draw

If that doesn’t help you could try replacing dyn_image.create_from_data() with dyn_image.load_png_from_buffer(data)

Thanks for the response! I figured out the issue, it was a silly mistake on my part. The resource I was referencing while writing some of this was for Godot 3. Between Godot 3 and Godot 4, create_from_data and create_from_image were changed to be static functions. Weirdly I didn’t get a warning or an error, but after fixing those, it worked as expected.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.