Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

default values for properties added by _get_property_list() #30440

Closed
IsjaslawEisenstadt opened this issue Jul 8, 2019 · 4 comments · Fixed by #85450
Closed

default values for properties added by _get_property_list() #30440

IsjaslawEisenstadt opened this issue Jul 8, 2019 · 4 comments · Fixed by #85450

Comments

@IsjaslawEisenstadt
Copy link

As of now (3.1.1 stable) it's not possible to assign default values to properties added via _get_property_list(). My current workaround is to use the same variable and property name and set the default value on the variable, but this doesn't work for any variable that needs custom _set() code.

There's also the problem that you can't revert properties added by _get_property_list() back to their defaults in the inspector (the revert arrow doesn't show up).

It would be neat if it was possible to add a "default_value" field to the returned dictionary from _get_property_list().

@kidinashell
Copy link
Contributor

I noticed that as well (v.3.2.3.stable.official). So I played around a little a bit and saw, that the inner workings would be ready to be able to revert properties that were added with the _get_property_list() method back to their default values.
Just for fun, I exported the same variables that I have already defined in the _get_property_list() method and voila, the revert button shows up as soon as you change the value in the inspector.
Downside of this is of course, that the variable shows up twice in the inspector. But it tells me, that the inner workings are there.

Example:

... // export the variable as well
export (bool) var use_gradient_tint = false setget _set_use_gradient_tint

... // inside the _get_property_list() method
property_list.append({
    "hint": PROPERTY_HINT_NONE,
    "usage": PROPERTY_USAGE_DEFAULT,
    "name": "use_gradient_tint",
    "type": TYPE_BOOL
})
...

Screenshot from 2021-02-01 10-46-38

@AnidemDex
Copy link

AnidemDex commented Nov 27, 2021

@Razoric480 (and anyone that is looking to add default values to custom properties, like me, from the future)
Looking a bit through the code, seems like something possible, using (as I like to name it) dark magic

# implement property_can_revert() and property_get_revert()
# assuming that we added "test_property" in property list and it has a custom getter/setter

func property_can_revert(property:String) -> bool:
    if property == "test_property":
        return true
    return false
    
func property_get_revert(property:String): # -> Variant() type
    return null # Here we may verify the property and what can be its default value

Relevant information:

if (p_object->has_method("property_can_revert") && p_object->call("property_can_revert", p_property)) {

Just two observations:

  1. You can not return null. No idea why you can't use null as default value, the EditorInspectorRevert doesn't let you use null as default value and doing that will hide the revert button
  2. You can't override get_property_default_value from Script.

Hope someone manages to open a PR that let you use null as default value and document those functions (wich I think are not exposed and we are just using dark magic)

Relevant line to enable null as an option of default value:

if (revert_value.get_type() == Variant::NIL) {

AnidemDex added a commit to AnidemDex/Godot-DialogPlugin that referenced this issue Nov 27, 2021
- Added a method to set default values for properties
See godotengine/godot#30440 (comment)
AnidemDex added a commit to AnidemDex/Godot-DialogPlugin that referenced this issue Dec 6, 2021
- Added a method to set default values for properties
See godotengine/godot#30440 (comment)
@Jayman2000
Copy link
Contributor

  1. You can not return null. No idea why you can't use null as default value, the EditorInspectorRevert doesn't let you use null as default value and doing that will hide the revert button

I recently ran into this problem. I have a property who’s type is Texture and I was able to get it to revert to null by doing this:

func property_get_revert(property):
    if property == "overhead_texture":
        return Reference.new()

I think that an Object could be used instead of a Reference, but I didn’t want to accidentally leak memory. When I revert the overhead_texture property, it seems to be reverted to null instead a new Reference, but I’m not sure why.

@AnidemDex
Copy link

I think this issue is already solved? Mentioned methods are now exposed and documented

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants