flavio
September 12, 2024, 11:54am
1
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
flavio
September 12, 2024, 12:40pm
3
in JS it returns me a string, I’m starting in gdscipt
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
Moreus
September 12, 2024, 1:08pm
6
logic operators always return true or false
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
1 Like
flavio
September 12, 2024, 1:20pm
7
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 . It’s pretty much the same, sorry.
2 Likes
flavio
September 12, 2024, 1:33pm
9
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
system
Closed
October 12, 2024, 1:34pm
10
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.