+1 vote

Okay I recently found out about the match flow control keyword. I'm wondering how one would implement this into _input. As if else ladders are weary, and look ugly.

if event.is_action_pressed(str(playern)+"charl"):
        p.vel.x = -1
    elif event.is_action_pressed(str(playern)+"charr"):
        p.vel.x = 1
    elif (event.is_action_released(str(playern)+"charl") && p.vel.x == -1) || (event.is_action_released(str(playern)+"charr") && p.vel.x == 1):
        p.vel.x = 0
    if event.is_action_pressed(str(playern)+"charu"):
        p.vel.z = -1
    elif event.is_action_pressed(str(playern)+"chard"):
        p.vel.z = 1
    elif (event.is_action_released(str(playern)+"charu") && p.vel.z == -1) || (event.is_action_released(str(playern)+"chard") && p.vel.z == 1):
        p.vel.z = 0
asked Feb 7, 2019 in Engine by lavaduder (264 points)

1 Answer

–1 vote
func _input(event):
    match event.as_text():
        "A":
              do_something()
        "W":
              do_something_else()

But event.as_text() won't give you the name you've defined in the input map. If you press the W key, it will return "W", so i don't know how useful this would be.

However it looks like you could simplify what you have currently by a little bit:

if event.is_action_pressed(str(playern)+"charl"):
    p.vel.x = -1
elif event.is_action_pressed(str(playern)+"charr"):
    p.vel.x = 1
elif event.is_action_released(str(playern)+"charl"):
    p.vel.x = 0

if event.is_action_pressed(str(playern)+"charu"):
    p.vel.z = -1
elif event.is_action_pressed(str(playern)+"chard"):
    p.vel.z = 1
elif event.is_action_released(str(playern)+"charu"):
    p.vel.z = 0

When setting p.vel.z = 0, i don't think you need to check if it is -1 or 1, because the alternative is that it is 0, and if that is the case, setting it to 0 again won't hurt anything.

answered Feb 7, 2019 by Eric Ellingson (1,581 points)

I do need to check if the input has been released is -1 or 1.

Why?

Say we go with this code. If the right input was released. The vel would remain 1, and the character would keep moving. (Unintended Ice physics.)

The main reason why I compare the vel. is to ensure that when left key is released, and vel is going right. That it won't suddenly stop. Only stop going right when the right key is lifted.

#Sorry I forgot comments

Thanks for trying.

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 How to use this Q&A? before posting your first questions.