Godot Version
4.3
Question
This is not by any mean necessary, but it happens every time I write grid movement.
(It’s more complicated in the real project, please don’t fixate too much on the example.)
func _input(event: InputEvent) -> void:
if event.is_action_pressed("move_up"):
position += Vector2.UP
if event.is_action_pressed("move_down"):
position += Vector2.DOWN
if event.is_action_pressed("move_left"):
position += Vector2.LEFT
if event.is_action_pressed("move_right"):
position += Vector2.RIGHT
Which, completely functional. But I hate it more every time I write it.
Just now, I’ve decided to try another way.
func _input(event: InputEvent) -> void:
for dir in ["UP", "DOWN", "LEFT", "RIGHT",]:
if event.is_action_pressed("move_" + dir.to_lower()):
position += Vector2.?????
How do I get that?
It’s a constant under the Vector2 class, a class that…I just realized I had no idea how it works.
Vector2.get(dir) doesn’t work, Vector2.new().get(dir) either, not Vector2[dir], nor Vector2.keys()[dir], Nothing gets me the result.
Is it even possible? Or do I have to prepare another script just to use the get() function?
Thanks for reading.