This will not work on export

Godot Version

4.2.1

Question

Hey everyone!

Just a quick question, perhaps easy for some. Below is my code, but I get a warning in the debugger:

W 0:00:03:0021 new_game_menu.gd:324 @ change_country_flag(): Loaded resource as image file, this will not work on export: ‘res://Main/Images/Flags/Afghanistan.png’. Instead, import the image file as an Image resource and load it normally as a resource.
<C++ Source> core/io/image.cpp:2495 @ load()
new_game_menu.gd:324 @ change_country_flag()
new_game_menu.gd:294 @ _on_countries_button_item_selected()

func change_country_flag(country):
	var flag_path = "res://Main/Images/Flags/" + country + ".png"
	if FileAccess.file_exists(flag_path):
		var image = Image.new()
		var error = image.load(flag_path)
		if error == OK:
			var texture = ImageTexture.create_from_image(image)
			countryflag.texture = texture
		else:
			print("Failed to load image: ", flag_path)
	else:
		print("Flag file does not exist: ", flag_path)

Any help would be greatly appreciated!

The error says exactly what you need to know, you should change the import from Texture2D to Image in the import tab when selecting in the file system

You can also access it via load(flag_path).get_image()

Thank you for the quick answer! Could you please go into more detail? I’m completely new to anything image/textures as most of the work I did until now as all text based or just standard themes. I want to learn as much about it as possible.

Read this Importing images — Godot Engine (stable) documentation in English

Alright, I changed it to Image and reimported. Same message appears. Do I need to somehow adjust my code as well?

just load it with load(flag_path), it’ll be an image now

func change_country_flag(country):
	var flag_path = "res://Main/Images/Flags/" + country + ".png"
	if FileAccess.file_exists(flag_path):
		var image = Image.new()
		var error = load(flag_path)
		if error == OK:
			var texture = ImageTexture.create_from_image(image)
			countryflag.texture = texture
		else:
			print("Failed to load image: ", flag_path)
	else:
		print("Flag file does not exist: ", flag_path)

If I do it this way it errors out as Invalid operands ‘Object’ and ‘int’ in operator ‘==’.

No just use:

var image = load(flag_path)
var texture = ImageTexture.create_from_image(image)
countryflag.texture = texture
1 Like

But you should just use the normal import and just go:

countryflag.texture = load(flag_path)

Any reason that won’t work or why do you need to load the image like this?

2 Likes

Thank you so much! Works like a charm.

As for the other suggestion - I don’t know, I guess it was just due to a full morning of coding and not taking a step back to evaluate, haha.

1 Like