Nested Resource created within editor is not recognized as script

Godot Version

v4.3

Question

I have a Resource class called StaticTile as a wrapper for a tile of a custom TileMap, and it has a property of type TextureAsset, which is a wrapper class for the visual representation of the tile (i.e. the Texture2D object and the offset).

When I create both nested objects within the Godot inspector, I cannot access a method within TextureAsset, because the object of that type that is assigned to the property in StaticTile is only recognized as a Resource, not as a TextureAsset.

Here ist the error message:
Invalid call. Nonexistent function 'get_converted_texture' in base 'Resource (TextureAsset)'.

When I print out texture_asset.get_class() right before the line that throws the error, I get “Resource”.

I don’t understand what I need to do to make my TextureAsset resource created within the editor being recognized as such class. I even made the object unique and saved it as a .tres resource file. When I inspect that file, the upper row in the Inspector says TextureAsset and the Script row says texture_asset.gd.

Here is the (pretty simple) code:

class_name StaticTile

extends GameEntityType


@export var texture_asset: TextureAsset
class_name TextureAsset
extends Resource


@export var texture: Texture2D
@export var offset: Vector2i = Vector2i(0,0)


func get_converted_texture() -> Texture2D:
	if texture:
		var image := texture.get_image()
		image.convert(Image.FORMAT_RGBA8)
		return ImageTexture.create_from_image(image)
	return null

The get_class method in Godot only returns the base class:

This method ignores class_name declarations. If this object’s script has defined a class_name, the base, built-in class name is returned instead.

Could you post the call you are making that shows the error?

Oh, I see. ChatGPT suggested it to me, but it’s definitely not the first time it has based its debugging suggestions on false assumptions.

This is the code that causes the error message:

for texture_asset in tile_textures:
	if texture_asset == null:
		continue
	print("Create tile")
	var sprite2D = Sprite2D.new()
	print("Class of texture_asset: ", texture_asset.get_class())
	sprite2D.texture = texture_asset.get_converted_texture()

Your code makes me suspect two possibilities for the behavior you are seeing:

The tile_textures data structure is incorrectly defined. Could you post that variable definition please?

You may be shadowing another variable. What happens when you rename texture_asset in your for loop?

Good idea! But that call is within another class that doesn’t have another texture_asset property.
I tried renaming the local variable to texas and I am still getting the error.

tile_textures is a parameter of the function at hand and passed down through several method calls:

func _get_tileset_source_from_tile_types(tile_textures: Array[TextureAsset]) -> TileSetAtlasSource:

The value of this array that we are working with here is probably the one created by this method:

func _get_textures_from_tiles() -> Array[TextureAsset]:
	var texture_assets: Array[TextureAsset] = []
	for tile in tile_types:
		if tile != null:
			texture_assets.append(tile.texture_asset)
	return texture_assets

Nothing in your code jumps out at me as wrong.

That being said, I am looking at Array[TextureAsset] and this in your initial post …

… and thinking that there may be something in conflict there.

How did you make the resource unique?

What happens if you remove that uniqueness?

By clicking the dropdown menu next to the field in the Inspector and hit “make unique”.

I am not sure how to “remove the uniqueness”. I deleted the resource and created new ones (both for the StaticTile class and the nested TextureAsset) and saved it as a .tres file. The method in question is triggered when a new value is assigned to the StaticTile field. When I assigned the new .tres file I got the same error again.

Thanks for your support btw, it is highly appreciated!


EDIT:
Nevermind, I fixed it - the resources were actually just missing the @tool annotation, so the script couldn’t be loaded when working in editor mode. :man_facepalming:

Such a stupid oversight of mine. On the other hand, Godot’s error messages could really be more helpful in finding the root of an issue. And I really don’t understand why ChatGPT didn’t verify that the annotation is present…

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