How does the match(Switch) work for Input

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

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
:bust_in_silhouette: Reply From: Eric Ellingson
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.

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.

lavaduder | 2019-02-07 20:25