Input Event broke my animation of player cause crash for walk and run cicles

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By linuxfree
:warning: Old Version Published before Godot 3 was released.

When i pressed KEY_UP play action animation of a player but cause crash if you continue pressed in this case is for running.

func _ready():
set_process(true)
get_node(“Spatial_Player/AnimationPlayer”).play(“player_stop_with_anim_gun”)
get_node(“Spatial_Player/AnimationPlayer”).set_speed(0.2)
set_process_input(true)

func _process(delta):
pass

func _input(event):
if (event.type==InputEvent.KEY and event.scancode == KEY_UP and event.pressed):
get_node(“Spatial_Player/AnimationPlayer”).play(“running”)
get_node(“Spatial_Player/AnimationPlayer”).set_speed(0.5)
else:
get_node(“Spatial_Player/AnimationPlayer”).play(“player_stop_with_anim_gun”)
get_node(“Spatial_Player/AnimationPlayer”).set_speed(0.2)

I need with key_UP pressed animation continue in loop without crash

Any ideia to solve this?

:bust_in_silhouette: Reply From: Beamer159

I would try something like this for your check:

if event.type==InputEvent.KEY and event.scancode == KEYUP and not event.echo:
    if event.pressed:
        getnode("SpatialPlayer/AnimationPlayer").play("running")
        getnode("SpatialPlayer/AnimationPlayer").setspeed(0.5)
    else:
        getnode("SpatialPlayer/AnimationPlayer").play("playerstopwithanimgun")
        getnode("SpatialPlayer/AnimationPlayer").setspeed(0.2)

The only code I added was “not event.echo.” This will ignore continuous press events, which is actually what you want. Now, when KEYUP is pressed, the first “if” condition will happen. When KEYUP is let go, the second “if” condition will play.

As for why your program crashed, my guess is that your “if” statement runs the second condition for all events that aren’t a KEYUP press. There are a lot of them. To see that, add this line to the beginning of your input function:

print(event)

You will see a ton of events written to the console. Each time, your second “if” condition happened.

not work that

I just declare two variables in main scene and solve that

func _ready():
set_fixed_process(true)
# variable for check Node control of Animatios
anim_player = get_node(“Spatial_Player/AnimationPlayer”)

   set_process_input(true)

func _fixed_process(delta):
if (anim != anim_new):
anim_new = anim
anim_player.play(anim)

func _input(event):
if (event.type==InputEvent.KEY and event.scancode == KEY_UP and event.pressed):
anim = “Running”
anim_player.set_speed(0.5)
else:
anim = “Idle_with_Anim_Gun”
anim_player.set_speed(0.2)
.
.
.
for another keys not working
any idea?

linuxfree | 2016-09-15 20:47

Done!

I changed else: per elif and it worked

func input(event):
if (event.type==InputEvent.KEY and event.scancode == KEY_UP and event.pressed):
anim = “Running”
animplayer.setspeed(0.6)

elif (event.type==InputEvent.KEY and event.scancode == KEY_DOWN and event.pressed):
anim = “idle_with_gun_anim”
animplayer.setspeed(0.2)

elif (event.type==InputEvent.KEY and event.scancode == KEY_SPACE and event.pressed):
anim = “Running”
animplayer.setspeed(0.5)
#Atemption now i used else for default animation
else:
anim = “default”
anim_player.set_speed(0.2)

linuxfree | 2016-09-15 21:37