How to use "or" in an if statement?

4.2.1

Title. On the surface this seems simple enough, “if [variable] == [string] or [string]:” and the like - Godot runs this code without errors. For some reason however, this if statement will accept any value for the variable rather than one of the two listed options. I really don’t understand why this could be and haven’t been able to find any clear documentation on the matter - could someone explain to me how to use “or” in an if statement?

I don’t typically use GDScript, but generally if you want to check for multiple values, you’d do the following:
If (variable == value || variable == differentValue)

It you need to compare multiple things in a similar manner, I recommend using a switch statement instead, and simply grouping multiple possible values together, such as:

switch(variable)
case value:
case differentValue:
code()

2 Likes

Thank you, this works much better. Still unsure why exactly my syntax behaved the way it did, though.

I would normally use if (x ==a) or (x==b) but is there’s a way to check against multiple values I’d love to know! :smile:

if [variable] == [stringA] or [stringB]

The reason you don’t see an error with this is because you aren’t comparing [stringB] with [variable].
[stringB] is compared to ""(empty string) and if it isn’t "" (empty string) the condition will return true.
The comparison you had set up is the equivalent of:
if [variable] == [stringA] or [stringB] != "":

if (conditionA) (logical operator) (conditionB)
The conditions in an if statement are independent of one another.
conditionB has nothing to do with conditionA

3 Likes

it goes like this. and stops godot timers

extends Node2D

var one = 0
var sone = [0,1,2,3,4]
var mas = 0
var can = 0
@onready var rol = $Icon

func _process(_delta):
	if one == 1 or 3: # Odds
		mas += 1
		rol.translate(Vector2(-2, 0))
	if one == 2 or 4: # Evens
		can += 1
		rol.translate(Vector2(2, 0))
	if one > 2 and 3: # Will avoid 1 and 2
		rol.rotation -= 0.1
	if mas == 100: # This must be true!
		mas = 0
		rol.translate(Vector2(200, 0))
	if can == 100: # This must be diffenrent
		can = 0
		rol.translate(Vector2(-200, 0))
	elif one == 0:
		$Timer.start() # I assume this will be true.
func _on_timer_timeout():
	sone.pick_random()
	one = sone

What are we to discern from this? Nearly every if statement is incorrect, like the original question using or incorrectly by assuming it will compare to the previous value.

This does not get odd numbers, it will always evaluate to true. Same issue OP was having, or does not do equivalence.

Again not evens, same issue with above

and has the same error as above, so this is equivalent to if one > 2 and true:, reduced further if one > 2: It does avoid numbers 1 and 2, but the 3 is very confusing.

Not sure why “This must be true!”, given the example mas is increasing every physics tick

Again not sure why “This must be different” can actually will increase at the same rate as mas so they will both be equal to 100 at the same time

1 Like

strings and ints are treated implicitly as booleans if used in logical expressions by themselves. Similar to python

func _ready():
	var x:String="abc" # false
	var y:String="" # true
	var a:int=0 # true
	var b:int=1 # false
	
	prints(!x, !y, !a, !b)
	prints(x.is_empty(),y.is_empty(),!bool(a),!bool(b))
	
	print("not empty" if y else "empty")
	print("not empty" if x else "empty")
		
	pass

prints

false true true false
false true true false
empty
not empty

2 Likes

Right now. I’ll just stick to this method;

extends Node2D

var timer := Timer.new()
var coorporate_ties = 0
var coorporate_tides = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
var low = 0
var wager = 0
var high = false
var you = false
var me = false
var who_sponsored_this = true
signal timeout

func _ready():
	add_child(timer)
	timer.start()
	timer.connect("timeout",self._on_timer_timeout)

func _process(_delta):
	coorporate_ties = coorporate_tides.pick_random()
	timer.wait_time = coorporate_ties * 1
	if who_sponsored_this == false:
		get_tree().quit()
	if wager > 500000:
		if wager > low:
			you = true
	if you == true:
		who_sponsored_this = false
	if me == true:
		who_sponsored_this = false
	if low < 500001:
		me = false
	if low > 500000:
		me = true
	else:
		you = false
		
		if coorporate_ties > 11:
			high = true
		if coorporate_ties < 11:
			high = false
	
func _on_timer_timeout():
	if high == true:
		low += 1
	wager += 1
	print(high)
	print(low)
	print(wager)
	print(you)
	print(me)
	print(who_sponsored_this)

Just comments/suggestions because I don’t get logic here.

	if wager > 500000:
		if wager > low:
			you = true
	if you == true:
		who_sponsored_this = false
	if me == true:
		who_sponsored_this = false

Could above be simplified to as below?

	if wager > 500000 and wager > low:
		you = true
			
	if you or me:
		who_sponsored_this = false

Also this

		if coorporate_ties > 11:
			high = true
		if coorporate_ties < 11:
			high = false

what happens above when corporate_ties is 10?

Also what does this below logic mean? Overlapping ranges?

	if low < 500001:
		me = false
	if low > 500000:
		me = true
	else:
......

One other comment when testing booleans true/false, sometimes it makes more sense to just not compare it to true or false. Depending on the variable name it’s easier to read.

E.g.

	if high == true:
		low += 1

can be

	if high:
		low += 1

Also the variable name who_sponsored_this should it be renamed to any_sponsors ? who implies specific (so not a boolean), any implies you don’t care as long as someone is sponsoring.

So the code:

	if who_sponsored_this == false:
		get_tree().quit()

becomes

	if !any_sponsors: # or not any_sponsors
		get_tree().quit()
1 Like

Edit: Sorry I thought you had a code sample you were trying to reduce, I think gionggone’s post is almost off topic. but there are a lot of relevant or usages as you pointed out.

This is correct, and is the same as a nested if statement

If coorporate_ties is 10, that is less than 11 so high will be false.

If coorporate_ties is exactly 11 then high will not be changed, the previous value will remain.
If this is not what you want you can reduce this expression like so, booleans can be assigned conditions directly.

high = coorporate_ties >= 11

There isn’t an overlapping range in this example. if low is equal to 500_000 then me will be false.

Again this could be reduced to a assignment.

me = low > 500_000

The else: ... is concerning though, as it is only tied to low > 500_000, that else will trigger if the first condition low < 500_001 is met. You might have wanted to use elif to only run one of the branches.

I would agree with this entirely. Again you can reduce any_sponsors to an assignment, this time using an or just like an if statement would, if either you or me is true, then it will evaluate to true

any_sponsors = you or me

I like if not any_sponsors: more, but the bang ! works just as well.

if low < 500001:
		me = false
	if low > 500000:
		me = true
	else:

It’s compared to MATCH. It must be ran at least enough time to be flipped 500,000 but the ratio must be above 0.5. And this argument compares that with low and wager. But, it’s also not true waht you are trying to say. Because godot makes it impossible to test it as a current bool is false. so it’s safer to apply that argument to make sure it’s false.

you both want to run that by me again, on the blackboard.
Overlapping values?
so you’re saying > 500000 is extractly the same as < 500001?

I could have as well started this topic. For the course of relevancy. Because on related topics of C++ -and is used more as Current_Event trading. So is the term -or. Why exactly the topics of C++ reserved the function to -snd as used for Current_Event overflow. Excuse me if I don’t understand the used -and & -or in Godot Script.

You are right. It is not overlapping. Perhaps would be easier read with this:

	if low > 500_000: # upto and including 500_000  
		me = true
	else:
		me = false
		you = false
		#.......
....

Not sure what you asking here. (Are you talking about short circuiting? ) They are exactly the same as C++ and any other language on the planet as long as they follow the same precedence rules. Also you can use && and || in gdscript if you so desire.

1 Like

sometimes this is a math problem
example:

variable > -5 || variable < 5
will result in true in every instance…It ‘looks’ likes you a defining a range between -5 and 5, but in reality anything greater than -5 will result in a true, and anything less than 5 will result in a true for one or the other cases, therefore the expression is always true.

&& is the desired operator

You’re absolutely correct about this. This just occured to me. So in fact the question remains. Is this code in fact loaded? In that case?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.