I was working with a button that I only wanted to be active if certain conditions were met (I don´t think the specific consitions are relevant, because I changed them a lot and I don't remember the original ones) and you held click on it. I changed the code a lot because it didn't fit what a wanted but I wanted to figure this out by myself. After fixing an error that made the project crash, I started to notice that the button only worked sometimes and that the UI didn't detect that I made changes in the code because the asterisk that used to appear by the side of the scene name when changes were made didn't appear anymore when I modified the script (but it appears if I modify something else). I'm writting here because the page of Godot Support says to try in the community pages first. I think I didn't forget anything, but if additional details are needed, just ask.
It’s difficult to tell what the problem is if we don’t have code to look at. Can you share your code (as pre-formatted text), along with your scene tree? Maybe a video recording of the button’s behavior, too, if you have a way of recording that.
this is a beginners problem and I’ve seen it before. you start to code, panic and keep adding random stuff trying to get it to work until all that’s left is spaghetti and unused variables and methods.
you HAVE to do cleanup, this is a very important part of coding, you also have to stop doing things without fully comprehending why.
right now you have 2 options:
1 - start over. create a new project or just the script and try to re-implement the code and think of how you can improve it. this will allow you to realize what parts are useless if you can’t tell your present selfwhy they are there or what they do.
keep the old script open in a text editor and copy by typing (don’t Ctrl+C) each line of code while asking yourself why is this line here and what does it do?.
you can also look at the godot warnings, if you haven’t disabled them. they will tell you when a variable is not used. for unused methods you will have to do that yourself but you can search the methods in the entire project and find where they are used or if they are used at all.
2 - more advanced, follow the code line by line and slowly clean the code, removing the parts that do nothing. add comments on the parts that do something.
and next time, remember to comment everything and plan on paper how the program is supposed to work before jumping into the editor.
I would recommend #1, multiple times until you learn. that’s why it’s the hard way.
it’s good that you want to learn, but understand that it takes some extra work and time.
that’s odd.
could we get more specifics? specifics are very important.
are you saying this is a bug? is the editor frozen? can you replicate it? are there any errors in red?
This is the code related to the button. I tried to make sure I made each different part as independent as possible. Despite being a newbie in Godot, I coded before, so I tried to keep the code divided into separate blocks so I can have an easier time debugging. The boolean variables are set to be wathever they need to be in order for the while to be true as long as you have the mouse over the button and click. I apology in advance for coding using spanish words, my mother language is spanish and this makes it easier to code, but it makes it harder for you to understand (unless you also speak spanish). I can’t record a video, but to describe it in a visual way, the color change of the Color Rect over the button is supposed to change color just by pressing and unpressing the button (the Color Rect is normally green, when you click and hold it should change to red, and when you release, it changes back to green. The second part works perfectly, when the button presses and you let go, it goes back to green. But, when you click the button, sometimes it doesn’t get pressed, that why I print the state of the button. The rest of scenes don’t interract with this one, the small interactions that I have planned are deativated, but if you insist on sending the scene tree, I will. Also, the Color Rect is set to let the clicks through. If you wonder why I used Color Rects over the button, is because when I coded the first button, I didn’t know how to change its color. I think that’s all, if you need something else, just tell me.
First of all, I clean up my code, this happened because of cleanup and optimization, I could have perfectly left this piece of code inside of the _process and let it happen every frame, in fact it worked perfectly when it was there and it didn’t cause any lag (most likely because there is very little things going on and are mostly unrelated). Second, I know I should do it on paper and comment the code, but this is a bad custom I picked in university when I coded like this and it turned out perfectly. I dind’t understand the crashing error either, something about the variablesa and the loop. It was so hard to fix that I gave up and asked ChatGPT, it changed one line of code and it stopped crashing. The thing that happened was that if you clicked the button too much, the instance froze and crashed. Finally, I don’t want to discard anything, so I’m considering all options, even bugs. P.S: Sorry if this message sounds harsh, I’m not used to write in english and I mostly forgot how to write a proper letter (writing a message, posting a comment in a website and writting a letter is practically the same, or so I was taught). Also, it’s the first time I use this page, so I don’t know how to do like you did, I know there is a tutorial for this page, but I can’t find it.
with godot you want to use Nodes and Resources for this, try to make the code in each node as simple as posible.
Unlike in other engines, a node in godot is an object and very simple, and also customizable through inheritance. you can strip it down to the bare minimum or use an existing one as a base. and if you don’t need it to run code during the game loop, you can use a Resource or go even lower down to an Object.
a common mistake is to over complicate things, specially when a solution has already been provided by the engine. I’ll talk about this part later in my comment.
ok let me look at this.
1 - so first, for this you want to use a Tween, tweens are used for creating animations, specially for UI.
with Tweens you don’t need to use any boolean, just create a tween and the engine will handle showing the animations and finishing when the condition happens.
here’s code from my game to show you how to do it:
and here’s how I would code the button being pressed:
var being_pressed : bool = false
func on_button_down() -> void:
if not being_pressed:
being_pressed = true
var tween : Tween = create_tween()
tween.tween_property(self, "color", Color.RED, 1.0)#tween for 1.0 seconds
tween.parallel().tween_property(self, "being_pressed", false, 1.0)
tween.tween_property(self, "color", Color.GREEN, 0.2)
I hope you get the idea.
you should always comment your code, not just for others but also for yourself. when you go back to your code after months or years it will be difficult to understand what the code does, specially bad code.
and for more complex programs you do need at least some kind of plan for how it’s going to work.
In this case it is pretty simple, but you where inexperienced with the engine and writing it down would’ve helped a lot.
it is a good idea to ask real people before ChatGPT, and also to never ask ChatGPT. it will provide garbage.
don’t worry
Edit: another thing, use references to your nodes. don’t use the $ outside the _ready function or if absolutely necessary, as what it does is call get_node().
it will also make the code cleaner.
@onready var generador_boton : Button = $"Generador/Botón Generador"
one more more thing, try using a theme for this, it sounds like you want a simple hover effect, and themes allow you to customize the colors of the button.
specially an instant change like what you were trying to do (maybe unintentionally).
also try the toggle option in button.
Thanks for your time. I gonna explain what I want to do, so you have an idea of what I want and help me better. The idea is that the button is normally green, but when you click it and hold it while being over it, it turns red and do something else. I don’t know if explaining that something else because that would require to explain the entire project and I don’t want to waste your time. Also, when you unpress it or remove the mouse from over it, it has to go back to green and stop that something else. That something else needs to happen once every second while both the button is pressed and the mouse is over the button. Also, I edit my project from the editor, I want to keep coding to the bare minimum because I don’t dare to do just pure coding yet.
all of that is easy with tweens.
just change your variables with the tweens. tweens can also run functions with the tween_callback().
so you want it to change after holding click for a while?
mmhhh.
it sounds complicated. it will be made easier by using tweens, so look them up.
tween_property changes a property over time.
use parallel to run one parallel to the last
use tween_callback to call a function, you can use this to check in the function if the thing has to happen.
given what you explained, your approach wasn’t entirely wrong, I can’t think of a way to do it without at least 3 booleans for each of the states the button has to be on.
but you should think of every combination of situations that can happen with this and use a simple if-else with tweens for calling the method after the time holding the button.
for cancelling the tween, I have never done it but I think it is possible with the kill() method.
Thanks for your time. I’ve been testing these objects, the reason I took long to answer. I will for sure use them in further development, but now, I’m just looking for basic functionality and using tweens will be like using a sledgehammer to crack a nut. During testing, I accidentally solved the button problem, it had an invisible node over it; I was aware of that node, but I did’t knew it reached that far, I’m sorry for wasting your time. The UI thingy, on the other hand, still happens, but dealing with it is easy, I just have to save the code from time to time. Thanks for everything.
no. tweens are a basic and very powerful way of handling SMALL animations, and are used mostly for UI. and the sooner you learn it the better because it will make your code so much cleaner, specially for these changes to the UI.
for bigger animations we have AnimationPlayer, for animating and changing things in code, tween prevents us from making this spaghetti of awaits, bools and timers.
the problem still persists, the problem of your code being messy and ill structured. just because it’s fixed doesn’t mean it’s over, unless you clean it up properly, you are bound to find more bugs in the future, ones that your don’t know about yet, because you don’t understand what the code does.
and you will always want to improve your code, the “it just works” is bad because you can’t touch the code or it will break, so you can’t add new features or tweak it or improve it in any way.
another mystery of the universe we will never learn the answer to I guess.