I have an Autoload node that needs to notify others whenever one of its properties changes. It has a lot of internal properties, and I would like to avoid writing an identical setter for each property.
I tried the following code in the Autoload.
extends Node
signal property_changed(property: String)
func _set(property: StringName, value: Variant) -> bool:
if !(property in self):
return false
else:
property_changed.emit(str(property))
return true
However, the setter override never seems to actually run when a property changes. The bottom comment on this issue thread says
As of 4.1.2-stable and 4.2beta4, _set can no longer be used to override parent properties. You can use it to add side effects to setting the property, but attempting to change the actual property value in set will not do anything.
They say that adding side effects using _set() is still possible, but I can’t seem to figure it out. Does anyone have any insight?
I would like to extend the set (StringName property, Variant value) method in the base Object class, but this doesn’t seem to be possible. My structure is more like this:
extends Node
class_name DataBase
signal property_changed(property_name: String)
var property1
var property2
var property3
...
var property100
and I would like the database to notify some others when one of its properties has changed, also sending the name of the property. The properties track global progression, and actually have names like “second_chest_opened” etc.
This is part of my metaprogression + dialog/story event triggering system. I can use a dictionary, but I was just trying to use object properties instead of string keys to get editors hints and avoid typos. Thank you for your suggestions. I will probably go to using a dictionary or custom class_name inside the autoload.
You could still use a public enum to avoid typos. Just create a new folder and call it enums, then make new scripts and delete the class name or extends node. Make the entire script an enum list and save them in the folder.
EnumList.Reference
enum Chest_Enum
{
Lv1_Chest1,
Lv1_Chest2,
}
enum Quest_Enum
{
Ch1_Quest1,
Ch1_Quest2,
}
class_name Database
static var Chest_List = {Chest_Enum.Lv1_Chest1: false,
Chest_Enum.Lv1_Chest2: true}
class_name Chest extends Node
@export var Chest_ID : Chest_Enum
Basically, you’re turning any String Key into an Enum. You will get editor hints and help you see where typos exist. The name looks somewhat longer, but this does help prevent you from making any mistakes.