godot icon indicating copy to clipboard operation
godot copied to clipboard

Add PropertyListHelper

Open KoBeWi opened this issue 2 years ago • 6 comments
trafficstars

Follow-up to #83888 (partially)

This PR adds PropertyListHelper. It's a helper class for managing dynamic properties added with _get_property_list(). Initialize it with prefix, then register properties with name, default, setter and getter. Afterwards, instead of implementing _get_property_list(), _set() and _get(), just call a method in the helper and it handles everything for you. As a bonus it can also handle reverts.

I added it to ItemList. Property handling is now a bunch of one-liners, while the functionality has improved. Defaults no longer save in the scene and you can revert.

https://github.com/godotengine/godot/assets/2223172/39c4900e-1681-4be4-a292-34e136867eaf

I will do a follow-up to apply it to all relevant classes (TileMap, PopupMenu and probably some others).

UPDATE In the second commit I changed how properties are registered and the 2 implementations are different. idk which one is better.

KoBeWi avatar Nov 08 '23 22:11 KoBeWi

Could it be exposed to scripting, I think this will be of good value if it could be exposed.

sairam4123 avatar Nov 09 '23 01:11 sairam4123

Not in this PR, and that would likely require a proposal. It's easier to maintain a class if it's internal.

KoBeWi avatar Nov 09 '23 02:11 KoBeWi

This feels kind of awkward that some properties would now be "registered" in the constructor. I understand that this needs to happen per instance, but maybe we can figure out a way to register those dynamic indexed properties in ClassDB (so it works in _bind_methods) and resolve them dynamically?

YuriSizov avatar Nov 09 '23 13:11 YuriSizov

They could be added to ClassDB, but not sure how to resolve setters/getters then. I made them use callable_mp(), because it's faster than calling them by name, and it requires instance. Maybe there is a trick to fetch the method pointer from method name, but it would involve some core changes for registering and then assigning these properties to an instance. And that's for something that's basically only useful in the editor inspector, you can't even have them documented, because these properties can't be used without knowing their full name. IMO not worth it.

KoBeWi avatar Nov 09 '23 13:11 KoBeWi

IMO not worth it.

I think adding a third place to define properties is not a great idea. That makes classes harder to maintain. So anything we can do to avoid that is worth it, in my opinion.

YuriSizov avatar Nov 09 '23 14:11 YuriSizov

I added a static PropertyListHelper that registers properties and then instances are created for actual objects. It uses regular Callables though.

KoBeWi avatar Nov 09 '23 15:11 KoBeWi

Thanks!

akien-mga avatar Feb 09 '24 11:02 akien-mga