Baffled by this array issue

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By grognard3

Hi all,
I have the following code set up with a button:

var moodarray = ["a", "b", "c", "d", "e", "f"]
var mood

func _ready():
	mood = moodarray[0]

func _on_Button_pressed():
	if mood == "a" or "b":
	elif mood == "c" or "d":
	elif mood == "e" or "f":

No matter what the variable “mood” get assigned out of the array, it always prints “one”. If the var mood is “e”, it still prints “one” not “three”.
Can anyone tell me what’s going wrong here? Thanks!

:bust_in_silhouette: Reply From: kidscancode

If you don’t first call randomize() you will get the same “random” shuffle each time you run. Put randomize() in _ready() to seed the random number generator.

Also, this is not valid:

if mood == "a" or "b":

This is analogous to writing:

if (mood == "a") or "b":

Which is always going to result in true. If you want to test whether mood has either value, you can do one of the following:

if mood == "a" or mood == "b":


if mood in ["a", "b"]:

Thanks a ton.
I cut out the shuffle(), along with all the other extraneous bits of code that I knew weren’t part of the problem. I appreciate the work-around suggestions. I ended up doing something similar. I still don’t understand the underlying logic of what

if "b":

means when mood =/= “a”, and why it returns a true, but if that’s what it is then I’ll deal with it. Thanks again.

grognard3 | 2019-06-02 02:20

if "b" returns true because the string has a value. if "" would be false.

kidscancode | 2019-06-02 02:41