#Button signal, antiquated data

17 messages · Page 1 of 1 (latest)

stone atlas
#

button.pressed.connect(a_function.bind(some_data))

If I update the data, the press still returns the old data.
What do I look up to understand when and why this type of referencing will take place?

It feels wrong to just disconnect and reconnect the buttons any time I edit the data.

keen crystal
#

Just to confirm what you're saying— you initialized some_data to something (i.e. 1), clicked the button, got 1, set some_data to something else (i.e. 2), clicked the button again, and got 1 still?

stone atlas
#

Yeee

slate isle
#

Youd need to pass a variable that is passed by reference

#

If it is an integer for example you just pass a copy of the value at the time of the binding

stone atlas
#

Upon further inspection it seems like this same phenomenon is occurring at all the points where the data is being used. It's in a separate script which is added to autoload. All of my code is referencing it directly

slate isle
#

What is the specific goal actually ? How youd set the signals and callbacks up depends on it kinda

stone atlas
#

Hard to say with all my spaghetti I have made, but If I had to say one goal I am trying to figure out how to do it would just be: How to avoid passing by copy, if I want by reference. In general

#

This is pretty confusing because the thing I am passing is a dictionary so I was assuming it would be by reference in gdscript

slate isle
#

If you use a dictionary then you could assign a key with bind and read the value that can change inside the function

#

Weird though that you pass a dict and it doesnt change.. Maybe the problem lies somewhere else

#

But youd have to show what exactly you do for me to figure that out 😅

stone atlas
#

I think it probably is somewhere else. Do you know if passing as an argument would make something a copy instead of reference normally?

slate isle
#

It´s also a common error if you think an array is a copy you assign it to another variable and change it, Both variables will have a reference to the same array.
To pass a copy for those youd need to use the .duplicate() method

var my_array = [1,2,3]
var my_other_array = my_array
var my_other_other_array = my_array.duplicate()
my_other_array[0] = 999
my_other_other_array[0] = 123
prints(my_array,my_other_array,my_other_other_array ) # //[999, 2, 3] [999, 2, 3] [123, 2, 3]
stone atlas
#

Oh this is interesting, I shifted the passing of the dictionary to outside of the function parameter and it now works by reference and not a copy. Maybe there's a weird bug happening

keen crystal
#

This might be a case of "closures" / anonymous functions.