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.
0 votes

Hi, so I am trying to accomplish a selection from whenever you press an arrow key, it will find the nearest selectable unit on the direction of the pressed key and select that unit.

For example, currently I am at the center position, so whenever I press the UP key, the top position will be selected. But if I press the UP key again, the selection will cycle back to the bottom position. And pressing the UP key again will bring the selection back to the center.

I tried using two-dimensional Array and load each position into the Array, and whenever a key is pressed it will either subtract or addition depending on the direction to make the selection, although this will only work if the position are not moving.

var enemy_party = [[$Enemy1, $Enemy2, $Enemy3], [$Enemy4, $Enemy5, $Enemy6], [$Enemy7, $Enemy8, $Enemy9]

I am planning to make the enemies move so the position will change, how should I approach this?

in Engine by (12 points)

1 Answer

0 votes

Maybe you could calculate it using Vector2()?

Assuming the arrow you said were the keyboard arrows, in theory the way I would try to implement this would be:

onready var Direction = Vector2()

#in your main loop for movement

match Direction: 
if Input.is_action_pressed('Left'):Direction.x = -1
if Input.is_action_pressed('Right'):Direction.x = 1
if Input.is_action_pressed('Down'):Direction.y = 1
if Input.is_action_pressed('Up'):Direction.y = -1
#Note that this is a diagonal direction Vector2(1,1)

Using 1 raycast to follow the current selected unit, on it's center to get cast in the direction of the Vector2(), and return any other unit that has crossed the ray, would be possible to specifying a max distance for the raycast as well by returning it's length.

by (381 points)

Oh and for the switching from up to bottom, you could do Vector2().y = *-1 to invert the direction.

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.