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

Setter function of exported property only called when the value is different from its default #83375

Closed
acgc99 opened this issue Oct 15, 2023 · 9 comments

Comments

@acgc99
Copy link

acgc99 commented Oct 15, 2023

Godot version

v4.1.2.stable.official [399c9dc]

System information

Godot v4.1.2.stable - Windows 10.0.22621 - Vulkan (Forward+) - dedicated NVIDIA GeForce GTX 1050 (NVIDIA; 31.0.15.3742) - Intel(R) Core(TM) i5-8300H CPU @ 2.30GHz (8 Threads)

Issue description

The setter function of an exported property is only called when the value is different from its default.

I think it should be called even if its value is the same as the default one, because the setter function might contain logic considering both cases. If the setter is not called when initializing the class, it would be skipped, giving unexpected results.

Steps to reproduce

Attach this script to a Control and run the scene with different exported property values (default and not default):

extends Control


@export var title: String:
	set(title_):
		title = title_
		print("set_title")

Minimal reproduction project

bug.zip

@AThousandShips
Copy link
Member

AThousandShips commented Oct 15, 2023

This isn't because the setter isn't called for default, it's because the default value isn't stored in the scene so it isn't called when the scene is loaded

Not a bug, but by design, you need to instead handle the default value manually if you need to detect it, relying on the default value being set isn't reliable and you shouldn't do it

You should instead handle this in _init or _ready

@acgc99
Copy link
Author

acgc99 commented Oct 15, 2023

I realized this thing when exporting horizontal alignment of a child Label. I set the default to HORIZONTAL_ALIGNMENT_CENTER but then, when running the scene, the text was on the left (center selected on Inspector).

To solve this, on the _init function of the parent, I had to write horizontal_alignment = horizontal_alignment to force calling the setter (or label.horizontal_alignment = horizontal_alignment more directly).

@AThousandShips
Copy link
Member

Yes, that's to be expected

You are expected to have your code be configured based on the default values, that's what them being default values means IMO

@acgc99
Copy link
Author

acgc99 commented Oct 15, 2023

So although I would like to use HORIZONTAL_ALIGNMENT_CENTER as default, I'm forced to use HORIZONTAL_ALIGNMENT_LEFT to get the right behaviour.

@AThousandShips
Copy link
Member

That's not a bug though you need to make sure that your script and scene is set up so it works correctly when used, you need to make sure your label has the same default as your property

@acgc99
Copy link
Author

acgc99 commented Oct 15, 2023

I understand, it's not a bug. You have to design the node from default values. This behaviour should be commented at docs at least.

@AThousandShips
Copy link
Member

Sure this can be added to some of the documentation, unsure how to make it clearer though

@acgc99
Copy link
Author

acgc99 commented Oct 15, 2023

I opened the corresponding issue at docs.

@AThousandShips
Copy link
Member

Closing in favour of godotengine/godot-docs#8246

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

No branches or pull requests

2 participants