How to use _get_property_list for adding more label

Godot Version

Godot 4.2.1

Question

Hello, I’m new to Godot. I have some label nodes that contain data from other scripts. Currently, I create the labels manually to fit the size of that data. How can I use get_property_list so that I don’t have to create a new variable every time the data has a new property?

size of that data? what data? font sizes? or just any properties (var new_data:int=100) you add to the extends Label script?

this looking like you will need to create an extended new script .gd that extends Label something like this

class_name BaseLabel
extends Label

var data1:float=0
var data2:String=""

then in the script of your labels that want to share the same base data, you can just:

extends BaseLabel

instead of extends Label

other way is to create a scene for just the label with @export var data1=0 in the scene script of the label
and instantiate this scene on where you want

The data consists of properties like bools, ints, etc. Currently, I use variables like label_bool: Label , label_int: Label , etc., to display the data. For instance, label_bool.text = otherscript.bool_properties . But, I’d like to automate the process of adding labels. How can I achieve this?

i see, try Autoload with constant variable to get the properties of label you wanted, use it like (LabelValues is an Autoload):

label_bool.text = LabelValues.bool_properties

in the LabelValues autoload:

extends Node
var bool_properties="just a string"
1 Like

I’m not a 100% sure if I understand what you want to do. Do you mean something like this?

extends Node


@onready var v_box_container: VBoxContainer = $VBoxContainer


func _ready() -> void:
	var my_object = MyObject.new()
	var properties = my_object.get_property_list()
	for property in properties:
		# for each property, if it's a script variable
		if property.usage & PROPERTY_USAGE_SCRIPT_VARIABLE == PROPERTY_USAGE_SCRIPT_VARIABLE:
			# Create a new label and add it to the VBoxContainer
			var label = Label.new()
			label.text = '%s : %s' % [property.name, my_object.get(property.name)]
			v_box_container.add_child(label)


class MyObject:
	var string_variable = "hello"
	var bool_variable = true
	var int_variable = 5
	var float_variable = 45.5
	var array_variable = ["a", "b", "c"]
	var dictionary_variable = {"hello": "world", "foo": "bar", "true": false}

Result:

Yes, that’s what I want. However, I have another issue. The variable is an Enum, like this:
var data_enum = EnumClass.enumtype.ENUM1

When I print it, it shows the index 0 instead of ENUM1. How can I make it show the enum name instead?

You’ll need to manually do that. There’s no way to detect if a variable is an enum or not as they are typed as int at runtime. If the enum does not specify any value for an entry you may have some luck with EnumClass.enumtype.keys()[value] where value is the value of the property.

Can you give an example of that? I’m lost.

Actually, you can use Dictionary.find_key() to find the key for the specific value (as long as the value does not repeat in the enum)

# ...

func _ready() -> void:
	# ...
			var value = my_object.get(property.name)
			if property.name == "enum_variable":
				value = MyObject.MyEnum.find_key(value)
			label.text = '%s : %s' % [property.name, value]
			v_box_container.add_child(label)


class MyObject:

	enum MyEnum {
		ENUM_VALUE_1 = 100,
		ENUM_VALUE_2 = 3,
		ENUM_VALUE_3 = 666,
	}

	# ...
	var enum_variable = MyEnum.ENUM_VALUE_2

It worked. Thank you very much.