Can some help with my GDscript

trying to make a variable but when i add the if it doesn’t work.

var direction = Input.get_vector(“left”, “right”, “up”, “down”)

if direction.x == 0 and direction.y == 0:

can someone tell me and help me understand why?

i don’t know the reason, but try this; if direction == Vector2i(0, 0):

i will try

it didn’t work :disappointed_relieved:

Do you get any errors in the editor?

Also, is your if statement in a function or standalone?

1 Like

this dose not work because you do’nt use the process or physics process functions.

func physics_process(delta):
    if direction.x == 0 and direction.y == 0:
        #more codes
1 Like

What exactly doesn’t work? Can you post the whole script and format it properly?

func _physics_process(delta): pass
var direction = Input.get_vector(“left”, “right”, “up”, “down”)

if direction.x == 0 and direction.y == 0

The godot error says;

Unexpected if in class body.

put a : next of if direction.x == 0 and direction.y == 0

Your indentation is probably wrong.

The code below should work.

func physics_process(delta):
    var direction = Input.get_vector(“left”, “right”, “up”, “down”)
    if direction.x == 0 and direction.y == 0:
        # more code here 

Notice the extra spaceing when moving from the process line to the var direction line.
Then again from the if statement to the comment ‘inside’ the if statement.

Gdscript uses indentation to separate code into blocks, when you get an error like Unexpected SOMETING in class body, this means that your indentation is wrong so check around where the error tells you maybe a line or two before or after that is where you made an error

Hope this helps

You might want to delete the ‘pass’ right after the ‘:’…

Anything after ‘pass’ won’t be executed. Ever.

1 Like

Turns out I was wrong! Pass does not exit a function. Funnily enough. Use ‘return’ for that.

In fact you were correct:

# this function will print test
func test()->void:
    pass
    print("test")

# this function immediately returns and you will get an error if you uncomment the print line
func test2()->void:pass   
    #print("test2)

Maybe it is better to say that you were originally technically correct.
It isn’t the pass that causes the immediate return, it seems to be the GDScript rules for indentation.

# this will immediately return after printing "test" and will also produce and error if the print statement is uncommented.
func test3()->void:print("test")
	#print("test3")