This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+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
in Engine by (275 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.

by (1,663 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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.