Toggle button in game is one trick pony

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Et24

Sorry everyone I tried uploading pics but basically I just have a button called runbutton> toggle signal> connected to control> and I have that scene emitting signals to my player scene in the main and when I click the button in game the players move speed is updated to run speed. This works, however when I click the button after, my player doesn’t return to regular move speed, which is what I want. I’ve tried:

Emit_signal(“walk”) in control node script and all sorts of variations

Func _on_runbutton_run();
Speed = run speed
Ok this works, but i want my player to go back to just Speed when I click the button again.

Here is my code and signals I’ve connected
Control node>button
Script:

Extends control
Signal run
Signal walk
Func _on_button_toggled():
Emit signal(“run”) <-stops working here.
Pass
Emit signal(“walk”)<-this is just a guess I don’t know what I’m doing
Ok,
Main scene

2dnode
Player>kinematicbody2d>sprite>collision.
Runbutton->control>button

Player script:
Func _on_runbutton_run():
Speed=run speed
Func _on_runbutton_walk():
Speed = speed
And I have very basic movement functin, if key pressed move, physics process delta, vector2 etc.
Any thought into this matter is very much appreciated.

:bust_in_silhouette: Reply From: johnygames

Of course it doesn’t go back to Speed. You changed Speed into run_speed, remember?

Speed=run speed

Speed is now equal to run_speed and when you press the other button you essentially say:

Speed = Speed

Create another variable named initial_speed or something and change your value using that. What you want is 3 variables:

  1. Speed is the value that controls the characters speed at all times
  2. Initial_speed is the starting value.
  3. Run_speed is the highest value

When button is pressed, if Speed==Initial_speed then turn it into Run_speed.
If Speed==Run_speed then turn it into Initial_speed.

If you found this answer useful, please mark it as best.