Best way to add an image property using @tool

Godot Version

4.4

Question

Hey everyone, I’m very new to the @tool annotation, and I don’t seem to see a type for images/textures/sprites, or anything visual. Would I be correct in assuming “TYPE_OBJECT” is the best option available?

I should say that this isn’t the most immediate issue because what I have does seem to work with TYPE_OBJECT, its just so broad I worry about people putting bad data types in there (I would know what to put in, but a big focus of this project for me is making something that’s simple to use, even if you’re not a programmer)

Side question: Is this the most in-depth resource for working with @tool?

Maybe the information I needed was somewhere else but I just couldn’t find what I was looking for. Thanks in advance for any insight!

I just checked how the engine handles it internally and it seems like yes - it uses type =TYPE_OBJECT, but with the combination of class_name you can specify which class it should inherit from.

This code:

extends Control

@export var my_texture: Texture

func _ready() -> void:
	for property: Dictionary in get_property_list():
		if property.name == "my_texture":
			print(property)

generated this output in the consol:
{ "name": "my_texture", "class_name": &"Texture", "type": 24, "hint": 17, "hint_string": "Texture", "usage": 4102 }

You can see it used "type": 24 whlch is TYPE_OBJECT and "class_name": &"Texture"

From the documentation of Object.get_property_list() method, it describes:

class_name is an empty StringName, unless the property is @GlobalScope.TYPE_OBJECT and it inherits from a class;

1 Like

Yes, but not only that. You’ll also need to set its hint to PROPERTY_HINT_RESOURCE_TYPE and its hint_string to the class you want to use. In your case Texture or Texture2D

Now, unless you are doing some dynamic stuff with the exported properties, you probably should stick to the @export annotations. There are multiple ones and will make your life easier. For example, your two exported properties would be:

export Node

@export_category("Panel Image Category")
@export var panelImage:Texture2D

And if you need some specific exported property not covered by the specific annotations then you can use the @export_custom() annotation.

2 Likes

Thank you! That should get me what I need. I actually started with @export but there are a few things I’d like to do dynamically unfortunately, so I’m in the middle of a refactor.

1 Like

I don’t know why I didn’t consider just printing out the property to see what’s there. Thank you, that was incredibly helpful. I didn’t know you could specify the class like that.

1 Like

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