|
|
|
 |
Reply From: |
ponponyaya |
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 _on_Poision_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.
Thank you for your response I checked and it is definitely on. Any other ideas?
DaZel77 | 2022-01-12 04:02
I think I know What wrong with it.
I update the answer, maybe you can check this one.
ponponyaya | 2022-01-12 05:23
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.
DaZel77 | 2022-01-12 17:09
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.
DaZel77 | 2022-01-12 18:02
I think there are two question to know.
-
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.
-
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.
ponponyaya | 2022-01-12 23:50
Alright I see how this worked. Thank you for your time and help. Have a great day ;]
DaZel77 | 2022-01-13 04:05