ImageTexture Strangely Changes to StreamTexture

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

So, i decided to make simple app with godot that need a lot of HttpRequest. When i put this code, it work as it should be, but sometimes it error and failed to creating image. Here’s the code:

var img = ImageTexture.new()
var errorImg = load("res://errorImage.png")
var raw_image = Image.new()
    
func request_completed(result, response_code, headers, body):
     var error1 = raw_image.load_jpg_from_buffer(body)
     if error1 != OK:
          img = errorImg.duplicate()
     else:
          img.create_from_image(raw_image)    <----- ERROR


#Error: "Invalid call. Nonexistent function 'create_from_image' in base 'StreamTexture'."

I hope you guys can help me out :wink:

Stick a print inside your if statement to see if this line is being run:

img = errorImg.duplicate()   

That line will change img into StreamTexture.
By the way, this error can be avoided by using strict typing:

extends Node2D
var img:ImageTexture = ImageTexture.new()
func _ready(): 
	img = load("res://icon.png")
	print(img)   

This brings up the error:

Trying to assign value of type 'StreamTexture' to a variable of type 'ImageTexture'.

LeslieS | 2022-12-29 22:55

I’ve solved the problem by the way…the main problem is because the resource of “res://imageError.png” is part of StreamTexture, the solution is changed the type of ImageError.png to Image in the import tab.

AXB | 2022-12-31 06:18