The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

–2 votes

I have a button and i want to variable e to 0 when the button is not toggled and 1 when it is, also I have many buttons on this same script. Heres the code:

var e 

func process(delta):
print(e)
func _on
Poisiontoggled(buttonpressed):
if buttonpressed == true:
$Poision.play("Active")
e = 0
elif button
pressed == false:
$Poision.play("default")
e = 1
When the button is pressed, the button continues to be null instead of a number

Godot version 3.4
in Engine by (60 points)

2 Answers

+1 vote
Best answer

I think I know What wrong with it.
I update the answer, maybe you can check this one.
You say that there are many buttons on the same script.

I think the probelem is:
Suppose you have 5 buttons on the same script, when you pressed one of the buttons, in fact the function onPoision_toggled(buttonpressed) runs 5 times.
(Every button runs 1 time, tutal 5 times)
So you got print like this:
1
null
null
null
null

The 1 above is print by that button you actually pressed, the other nulls is the remain 4 buttons prints.

To solve this problem, I will set an unique variable id for every buttons, and then you can use this unique id to check which one is the button you relly pressed.
Suppose your script is on Button, then code like this follwing:
(If you don't mind, this way have to write connect in code.)

[In Button script]

extends Button

onready var id: String = get_name()
var e

func _ready():
    # Note: The fourth parameter [id] is important in this case
    connect("toggled", self, "_on_Button_toggled", [id])


func _on_Button_toggled(button_pressed:bool, Pid:String):
    if Pid == id: #check the button is you actually pressed one
        if button_pressed == true:
            $Poision.play("Active")
            e = 0
        elif button_pressed == false:
            $Poision.play("default")
            e = 1

This example in my test is ok, hope this help.

by (526 points)
selected by

Thank you for your response I checked and it is definitely on. Any other ideas?

I think I know What wrong with it.
I update the answer, maybe you can check this one.

I tried this in my code and came up with a similar problem. When I print the id var it gives all of the button names instead of just the one pressed. This means Pid always equals id because every button pressed has to be one of the buttons in id. I feel like this answer is really close but its not quite solved. when I print e it still looks like before.

did you duplicate one button to make five buttons, or did you create them seperately?

try giving different names

I duplicated them and gave two of the eight different names, should I make them individually? Also I think the problem is that the on button pressed func is still running for each button, or var e is giving a separate value for each button that is connected to the script.

I think there are two question to know.

  1. Were those buttons have same parent?
    If The answer is yes, then variable id = get_name() is unique, since you can't have same names in one parent.

  2. Where were you print those ids?
    If you print it in function _process(delta), then you have to label then, otherwise you can't know the print is from which button.
    (Since all buttons will print no matter you pressed or not If you print it in function _process(delta).)

for example you can print and test like this:
Just print it in "if Pid==id" condition, i.e.:

func _on_Button_toggled(button_pressed:bool, Pid:String):
    if Pid == id: #check the button is you actually pressed one
        if button_pressed == true:
            $Poision.play("Active")
            e = 0
        elif button_pressed == false:
            $Poision.play("default")
            e = 1
        print("name: ", get_name(), ", id: ", id,", e: ", e) # label print

then you will got print result like this folling:

name: Button, id: Button, e: 0 # first time when I pressed Button
name: Button2, id: Button2, e: 0 # first time when I pressed Button2
name: Button, id: Button, e: 1 # second time when I pressed Button
name: Button3, id: Button3, e: 0 # first time when I pressed Button3
name: Button, id: Button, e: 0 # third time when I pressed Button again
name: Button2, id: Button2, e: 1 # second time when I pressed Button2

you will find that each variable e in each button is seperate.

Alright I see how this worked. Thank you for your time and help. Have a great day ;]

0 votes

are you sure the function is even working? try print something when button is toggled.

i would suggest use: func _poison_pressed(button):instead.

or

create your own buttonpressed variable instead, like this:

var button_pressed = false
var e 
func process(delta):
  print(e)
  if button_pressed == true:
     $Poision.play("Active")
     e = 0
func _on_Poision_toggled(buttonpressed):
   button_pressed = true

#create a function that set button_pressed to false

by (447 points)
edited by

I did try printing the var and I got the weirdest print I've ever seen:
null
null
null
null
null
null
null
1
null
null
null
null
null
null
1
null
null
null
null
null
null
1
and kept going and when I toggled the button the number turned to 0 but had the same look

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 Frequently asked questions and 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 [email protected] with your username.