I’m trying to use a variable with a getter to setup a flipper, so everytime I ask for it’s value it returns the opposite of the last value returned. This is the code I’ve wrote.
var flip = false:
get:
flip = !flip
return flip
The problem is apparently the getter is being called by himself, so even though I can get the value and it does not cause any problems to other methods referencing it from outside, the getter itself is in an infinite loop. I’m not sure if this is really the cause. I only noticed this behaviour while checking the variable on the “Remote” tab while the code is running, and it happens even if I do not access the variable at all in the code, so it may be just a response to the “Remote” itself getting it’s value? I really don’t know. Does anyone knows how the variable getter and the “Remote” works to tell me if this is a normal behavior or if I should change my code to prevent bugs? Thanks
Called by himself? Do you have a function elsewhere in this script that’s calling it? Also check to see if anyone else calling it is doing it in a loop. Like a timer that keeps restarting. The remote tab just shows you whats happening during runtime, btw.
I tought it was a function but to test it I’ve created a new project and added this variable with a getter to a global script. Ran the project and checked the global on remote, the behaviour is the same. You can test it yourself if you want.
Since nothing in the code is calling the variable to trigger the code inside the getter, then it most likely isn’t calling itself, but something else definitely is, otherwise the value would’t be flipping. I suspect some internal Godot code on the remote tab? Idk.
Printed once, even as an autoload and If I hit the remote tab. Try it in an empty project. Because, I believe is something you wrote elsewhere in your proyect.
I tried your code on an empty scene and it does flip on a loop just like my own code. Maybe this is a Godot version issue? I’m running this on 4.2.1. Wich one are you using?
Version 4.3. I meant empty project, to make sure there’s no other global or anything proyectwide messing things up. An empty new proyect with a scene that is just a vanilla Node with this code is the best proof that it might be Godot itself.
Yes, I think it’s some internal code. With the “print(sum)” line on the getter I noticed it only starts looping when I open the scene on Remote, so most likely Godot constantly checking on the variable is what’s making it loop. Thanks for the help
Oh I get it now, I was able to replicate it. The reason isn’t just because you are on the remote tab, but because you are clicking on the node. If you click on another node, like root, it will stop.
Must be because when you click on a node on remote, the editor is reading all members so that it can give you what is happening per frame in the properties tab. When it reads this variable it executes the code, clearly because this kind of variable can’t be read without executing the code, even when accessed by the editor.
This could be a good candidate for a future fix, but is something that only will happen on the editor under theses circumstances.