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’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}
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.