Hey, quick question:

I'm used to the &&-operator only evaluating the first expression if that turns out to be false. Unless I'm mistaken this does not seem to be the case in gdscript, instead evaluating both expressions in any case. This makes accessing members of objects without checking for null beforehand more tedious. Is there an elegant alternative solution?

I think the operator is called "and" in GDScript.

I just did this test:

func _ready() -> void:
	var a: bool = t(1) and t(2)
	var b: bool = f(3) or f(4)
	var c: bool = t(5) or f(6)
	var d: bool = f(7) and t(8)


func t(i: int) -> bool:
	print("t", i)
	return true


func f(i: int) -> bool:
	print("f", i)
	return false

The output is:

t1 t2 f3 f4 t5 f7

which shows that GDScript implements short-circuiting for both the and and or operators. The f(6) and t(8) calls are not performed.

If I replace and with &&, and or with ||, the results are the same.

Unlike languages like Ruby where the operator affects precedence, && and || act identical to and and or. For this reason, && and || are considered deprecated in Godot 4.0 and will emit a warning by default (which you can disable in the project settings).

a year later