0 votes

The documentation says that when the change to the variable is made by an external source the setter function will be called and that this happens before the value is changed.

But it doesn't seem that way with arrays and dictionaries.
The only way I get the setter to work as intended is when I duplicate the entire array/dict make the changes and then set the value to the original variable:

 #fruits = ["Pineapple","Pear,"Grapes"]

#This calls the setter function properly
var fruits_dup = $Child.fruits.duplicate()
fruits_dup[0] = "Orange"
$Child.fruits = fruits_dup

#This  will set the value and then call the setter
$Child.fruits[0] = "Orange"

#Array/Dict Methods will bypass the setter completely but getter is called
$Child.fruits.append("Banana")
$Child.fruits.erase("Pear")

My goal was to check if the array had changed and update my inventory only in the slots that have changed instead of having to emit a signal every time the values are changed.

There's a changed() signal for resources but I've read that it's bugged and it doesn't work at all anyone knows something about this?

in Engine by (235 points)
edited by

2 Answers

0 votes
Best answer

The setter should only called when you set the value of the variable to a different value or object (so, the containing array or dictionary in your case). It's not intended to be called when the contents of the containing object are changed, so the reported behavior is expected.

Also, that's how similar functionality works in other languages as well, so there is some precedent for the behavior.

by (9,044 points)
selected by

If it's not intended to be called why is it called once I already changed the values of the array by index?

And do you know if the change signal for custom resources also works like this? Is it still bugged in 3.2 ?

Thanks

Sorry, I don't know the answer to either of those additional questions...

It may have to do with the COW (Copy-in-write) idiom. Calling operator[] on a COW vector will create a copy before modifying it. It is likely unintended that the setter gets called.

Seems related to this issue (will be fixed in 4.0): https://github.com/godotengine/godot/issues/28720

Edit: also this issue: https://github.com/godotengine/godot/issues/17310

0 votes

Arrays and dictionaries in Godot are reference-types, which means that a variable of array/dictionary type stores only a reference to an array/dictionary, and this reference isn't changed by just changing its contents, and thus setter doesn't fire.
Duplicating it and setting the duplicate to the original variable - does fire the setter because you're changing a reference itself.

The behavior you'd like to have is only possible in languages where arrays and dictionaries are value-types, like e.g. Swift.

by (32 points)

Thanks for the explanation I wished the documentation mentioned this even if it's just a short mention so I can google the rest.

But I have to ask:

why does the setter gets called when I change an index manually like in my second example:

#This  will set the value and then call the setter
$Child.fruits[0] = "Orange"

the setter will fire and print the array with the first index already changed. So what is exactly happening the setter knows something has changed but it is called too late?
What you have described explains the array methods like append,erase, etc which bypass completely the setter function. So what's the difference when you set the index manually?

Actually the documentation does mention this:
https://docs.godotengine.org/en/stable/classes/class_array.html

Note: Arrays are always passed by reference. To get a copy of an array which can be modified independently of the original array, use duplicate.

Regarding your second question - I have no idea, most likely it's caused by some aspects of Array's internal implementation, and this is a behavior I wouldn't rely on too much.

I meant an explanation about the variable type I did read the note.
What I understand as reference as a complete beginner with no programming knowledge is something like a path to grab the information from. So I thought the setter would know when the values changed even if it's not the original it has to check at some point.

I'm missing some programming basic notions I will admit.so what I meant with the mentions in the documentation would be something like links to a wiki page or something that explain the general concept otherwise i will think of reference as a non programming term with my own paradigm of what that means if that makes sense.

I also had problems with gui_input() not telling you it only admits mouseEvents and the documentation telling you it retrieves an InputEvent which is true, but it doesn't tell you which of all of the InputEvents it retrieves I could only confirm it when I saw a GDquest video about inputs where he explicitly said so otherwise I didn't know if it was just me been dumb.

Thanks for the answers.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories