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

Exported variables don't use their setters on initialisation #62739

Closed
mieldepoche opened this issue Jul 5, 2022 · 2 comments · Fixed by godotengine/godot-docs#7579
Closed

Comments

@mieldepoche
Copy link
Contributor

mieldepoche commented Jul 5, 2022

Godot version

4.0.alpha11, but the behavior is the same in 3.x

System information

Linux

Issue description

Variables that define a setter don't use it on init:

var clamped = 5:
	set(value):
		clamped = clamp(value, 0, 1)

func _ready() -> void:
	print(clamped) # prints '5'
	clamped = clamped # "solved" by doing this very clever trick for each variable
	print(clamped) # prints '1'

This makes for an inconsistent behavior when using exported variables since the setter is triggered when setting the export value:

@export var x := 4:
	set(value):
		x = value
		print("This only prints for modified export values.")

Steps to reproduce

see snippets above

Minimal reproduction project

No response

@Calinou Calinou added this to the 4.0 milestone Jul 5, 2022
@Calinou Calinou moved this to To Assess in 4.x Priority Issues Jul 5, 2022
@KoBeWi
Copy link
Member

KoBeWi commented Jul 5, 2022

This is intended, see #36496
When you export a variable and change it, it will be set during scene load, which will trigger the setter. It doesn't happen for default values, because they are initialized directly in the constructor (this is consistent with what happens internally in C++).

Maybe we could document it better, if it isn't already.

@KoBeWi KoBeWi removed this from the 4.0 milestone Jul 5, 2022
@KoBeWi KoBeWi added documentation and removed bug labels Jul 5, 2022
@Calinou Calinou changed the title GDScript 2.0: variables don't use their setters on initialisation Exported variables don't use their setters on initialisation Oct 10, 2023
@nblackley
Copy link

This is intended, see #36496 When you export a variable and change it, it will be set during scene load, which will trigger the setter. It doesn't happen for default values, because they are initialized directly in the constructor (this is consistent with what happens internally in C++).

Maybe we could document it better, if it isn't already.

I think this is a case where the programmatic intended behavior is at odds with an intuitive user experience from a designer perspective. There seems to be a legitimate programmatic case to be made for this behavior, but for a user with limited understanding of how Godot works internally, this feels like an inconsistency and a surprising behavior that is difficult to diagnose.

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.

5 participants