Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Idleman |
If I want the player to enter a charge state by holding down a key, but while holding this key, any other key pressed will stop the charge state and return to normal state. How do I set up the if condition. I know to set if Input.is_action_pressed("ui_charge") &&:
but not sure what to put as condition for the detection of other key press. Thank you.
could you share some code of your current implementation so far?
p7f | 2020-07-09 12:33
Thanks for responding to my question here and in other threads. Here is the key part of my current code.
The design is to hold down a key and the player goes into a charge state, where a short charging animation will be play. Once the charge animation finish, 1 unit of power will be added to the player. If the player moves during the animation time frame, the charge state will be set to normal state and no power will be added.
I set the trigger in the player scene, I tried the is_action_press but it is not very reliable, so I use is_action_just_pressed to trigger the state and set a is_action_press in the instanced scene of PowerCharge scene.
So far, this code got me the result I want, only problem is that when the charge animation is playing, the player can be moved and the charge animation stay at the player’s original position. Therefore, I thought if I add a if condition to detect when other key is pressed and the charging will stop.
Note: In the Player scene, I put the main function into a AnimationPlayer method track. The reason for that is if I run the function on its own, before getting the signal and return to normal state, the power_charge() function will run many times and cause the unpredictable outcomes such as adding more than 1 power at a time. However, I found that if the same function is called in a method track in AnimationPlayer, it only runs once with no repeat.
In my player scene
#Trigger the charge state
if Input.is_action_just_pressed("ui_charge"):
state = CHARGE
#Once in Charge state, perform function
func charge_state(delta):
movement = Vector2.ZERO
animationPlayer.play("Charge")
# This function was added and run in AnimationPlayer
func power_charge():
var charge = POWER_CHARGE.instance()
charge.connect("charge_finish", self, "set_animation_finish")
charge.position = global_position
get_parent().add_child(charge)
In the instance charge scene
extends Node2D
onready var animatedSprite = $AnimatedSprite
signal charge_finish
func _physics_process(delta):
if Input.is_action_pressed("ui_charge"):
animatedSprite.play("Effect")
else:
animatedSprite.stop()
scene_finish()
func _on_AnimatedSprite_animation_finished():
if PlayerStats.power_count < 8:
PlayerStats.power_count += 1
print(PlayerStats.power_count)
scene_finish()
else:
scene_finish()
func scene_finish():
queue_free()
emit_signal("charge_finish")
Idleman | 2020-07-09 13:43
If i understood you correctly, i think there are a few things you could do. If only movement will stop charging, and lets say you only have “move_left” and “move_right” actions, you could inside charge state ask:
if Input.is_action_just_pressed("move_left") or Input.is_action_just_pressed("move_right"):
#cancel charge
state = IDLE
If you wan’t any key to stop charging, you could use _input(event)
method to set a flag:
func _input(event):
if event.is_action_pressed("move_left") or ... #add all actions that could cancel charge
#cancel charge or set flat to cancel it on process
state = IDLE
Im putting this as a comment because idk if its the best way to do so.
p7f | 2020-07-09 15:22
Thanks for the suggestion, I think we are thinking on the same line, but instead of adding all keys, I feel an any key other than “ui_charge” pressed condition will cover all the grounds, so it will be simple. I am not sure if there is way to set an any other key pressed condition, I will do more research on this.
Idleman | 2020-07-10 04:21
After doing some more exploring, I have found a way to set the if condition of detecting any other key input (the code it is in my comment to another answer). It works ok, but not working in game pad since the code is for key input. If there is something like Input.!is_action_input(“ui_charge”) that will be better, but I don’t think Godot provides such a way for input handling at this moment. I will go with this solution for now, update it down the road for my project if something more efficient comes up. Thank you very much for your help.
Idleman | 2020-07-11 06:27