Function Running in If Statement Condition...?

Godot Version

V4.7.2

Question

My first question on the forum! Hopefully I'm doing it right haha

I’ve got a rather simple top-down character movement script in the works for a tutorial I’m following. The tutorial said to place move_and_slide() inside the argument of an if statement. I’m unsure why this works. Do functions normally run automatically if presented in the condition of an if statement? Would this apply to loops and such as well, assuming they return indexes? Hopefully this is a simple question, I just want to be sure.

Thank you!!!

extends CharacterBody2D

## Speed in pixels per second
@export_range(0, 1000) var speed := 60.0

func _physics_process(_delta: float) -> void:
	get_player_input()
	if move_and_slide():
		resolve_collisions()

func get_player_input() -> void:
	var vector := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
	velocity = vector * speed

func resolve_collisions() -> void:
	print("Boink!")

if you type a function name with parentheses it will always be called, no matter where it is, assuming that the syntax is correct.
Functions return values so that returned value will be used if the function call is a part of an expression.

In your case move_and_slide() return a boolean value which will be true if the body collided and false otherwise.

Many functions return a value, you can use this value like any other as part of the if, it’s not particularly special.

Maybe this clarifies what is happening, this code is exactly the same

var slide_hit: bool = move_and_slide()
if slide_hit == true:
    print("bonk!")

godot is actually a little bit messy with types because it uses higher level language, but if you would ever see other languages with more organization you’ll see stuff like scopes for many code blocks and function types

#include <stdio.h>

int a = 5;

int main(){ // it works like _ready() in godot
  if (a > 5){
    resolve_collisions();
  }
}

void resolve_collisions(){
  printf("Collided");
}

here you can see that each function and if statement have brackets, these are scopes, and scopes are like sections of code, but you’ll learn that later, what you must know is that if statement or loop will run their scope when some condition is meet, e.g in this case if a is greater than 5

back to gdscript

func _ready():
  get_player_input()
  if move_and_slide():
    resolve_collisions()

func resolve_collisions():
  print("Collided")

we check move and slide in order to see if you’re touching ground (at least from what i saw it’s used for) so it’s just safety in this case, but yeah in summary - functions will run in if statement because that’s how gdscript works

Functions are reusable blocks of code. In order to actually use the function, we need to use the function call operator, which looks like a pair of open and close parentheses next to a function’s name.

An if statement is provided a body to evaluate to a “true” or “false” value. In order to evaluate the body of the if statement, it executes the code inside. This would mean that if we call a function from within the body of an if statement, that function does get called when that if statement is evaluated.

Here are some examples of functions being used:

# Declaring a function named "foo" that always returns the integer 1
func foo() -> int:
  return 1

# Somewhere else, we can use foo.
# This is "calling" or "invoking" the function.

foo() # call foo normally

var bar: int = foo() # bar's value is 1

# We can also call foo within other contexts:
if foo(): # This if statement will always be true because foo() returns 1
  pass

while foo(): # This is an infinite loop!
  pass

Do functions normally run automatically if presented in the condition of an if statement? Would this apply to loops and such as well, assuming they return indexes?

Quick answer → yes. (if withing a code block that is executed)

By that I mean, for example in your code the _physics_process() is called many times a second. Meaning, the code block within it is executed everytime (unless there is an early return, but don’t worry about that now).

On each of those code block executions, the if statement is checked. In order to do that, the condition move_and_slide() needs to be invoked.

move_and_slide() is a very useful built in Godot method (read the docs here). It returns true if the body collides with some other body, otherwise it returns false

Taking that into consideration, resolve_collisions() only makes sense to call when there is a collision. That is why it needs to run under a certain condition. Otherwise it would run all the time (which would be a waste of compute or a program crash)

This same behavior is true for any other loop statement as well. If the statement is within an executed code block, then it will be processed.

Thank you for all the very helpful replies. I’m loving this forum already! <3

Is the best :slightly_smiling_face: