OR operator doubt

4.3

Hi all I come from Javascipt

I want a string as a response and not a boolean

extends Node

var user_color: String = “green”
var default_color: String = “blue”
var current_color:= user_color || default_color
func _ready() → void:

print(current_color)

How do you expect this to work? the or operator always returns a boolean

in JS it returns me a string, I’m starting in gdscipt :smile:

let userColor = ‘red’;
let defaultColor = ‘blue’;
let currentColor = userColor || defaultColor;
console.log(currentColor);

But which color does it return? Does it return a random color?

Didn’t know JS worked that way.

In GDscript you would have to:

	if user_color:
		current_color = user_color
	else:
		current_color = default_color

or
current_color = user_color if user_color else default_color

3 Likes

logic operators always return true or false

1 Like

in JS example the result = ‘red’

and if userColor = null , returns ‘blue’

In JS this is used almost as a fallback. It’s not that it returns one or other, but it will return the one that is not null or undefined.

In GDScript u can’t do the same. What u can do in this case is use a ternary approach like:

var current_color := user_color if user_color != null else default_color

Edit: Btw i didnt saw @trizZzle comment :sweat_smile:. It’s pretty much the same, sorry.

2 Likes

each language has its peculiarities, I think it’s a mistake on my part to try to solve this using the JS paradigm, I apologize to everyone for the post
Thx All

2 Likes

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