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
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()
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:
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
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.
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…