0 votes

Hello, I'm still new to the godot engine, so I came across a following paradigm in which I'm stuck, does anyone have the solution for this code other than redoing the entire code?: <
Because I'm trying to structure a game based on my advance in the programming itself, and the command is very flawed and perhaps gigantic, but what it basically does is to rotate Position2D's position so there is an instance with the bullet fired, At first it was all right when I triggered with if and elif conditions, but when I took out the elif started firing on the diagonals as well.
And the disadvantages of using the elif is that the character begins to no longer walk the diagonals.
Here the code:
Ps: Sorry I'm still developing my English
Direx =Direction x Direy = Direction y

if key_right:
    $ShotLocation.position = Vector2(40,0)
    direx = 1
elif key_left:
    $ShotLocation.position = Vector2(-40,0)
    direx = -1
else:
    $ShotLocation.position.x = 0
    direx = 0
if key_down:
    $ShotLocation.position = Vector2(0,40)
    direy = 1
elif key_up:
    $ShotLocation.position = Vector2(0,-40)
    direy = -1
else:
    $ShotLocation.position.y = 0
    direy = 0
asked Jul 28 in Projects by Amigoimaginario (29 points)
edited Jul 28 by Amigoimaginario

My objective is: shot just four directions (w,a,s,d)

Is there a question?

Yes, I'm sorry if you got confused, but what I wanted to know is if I can use some calculation to make the vector2 not add to the other so there are not 8 possible shooting positions

Do you mean like this?

if key_right:
    $ShotLocation.position = Vector2(40,0)
    direx = 1
elif key_left:
    $ShotLocation.position = Vector2(-40,0)
    direx = -1
elif key_down:
    $ShotLocation.position = Vector2(0,40)
    direy = 1
elif key_up:
    $ShotLocation.position = Vector2(0,-40)
    direy = -1

if !key_up and !key_down:
    $ShotLocation.position.y = 0
    direy = 0

if !key_right and !key_left:
    $ShotLocation.position.x = 0
    direx = 0

So the inputs don't overlap?

1 Answer

+1 vote

You could add all of the vectors together to create a result vector using if statements, no elif or else.

You could then use the angle method to check this result vector and see which way it is pointing or you could take the simpler approach and get the x and y atributes of the vector and check them.

if result.y == 0:
    if result.x == 1:
        $ShotLocation.position = Vector2(40, 0)
        direx = 1

etc...

The benefit of calculating a resulting vector is that you can handle situations such as if the player has the left and right keys down. In addition, you usually use this resultant vector to help you save time. In this example, I could set direx or direy to be result.x or result.y, respectfully!

answered Aug 1 by greencloversguy (21 points)

Thank you, hadn't thought of that :3

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.