If you don't want a boolean result but still want to use a conditional expression to choose between two values based on a condition, you can use the ternary operator in GDScript. However, the condition itself should ideally be a boolean or something that can be coerced into a boolean context.
Using Ternary Operator with Non-Boolean Condition
If your condition is not inherently boolean, you can still use it in a ternary operator by converting it to a boolean. For example, if you have a condition that is not a boolean but you want to use it to choose between two values, you can do something like this:
var condition_result = _evaluate_condition(node)
var next_node_id = condition_result != 0 ? node.true_node_id : node.false_node_id
var next_node = _get_node_by_id(next_node_id)
In this example, condition_result != 0 is used to convert the condition into a boolean context, assuming that 0 is considered "falsy" and any other value is "truthy."
Alternative Approach
If you prefer not to use the ternary operator or if your condition is complex, you can use an if-else statement for clarity:
var condition_result = _evaluate_condition(node)
var next_node_id
if condition_result != 0:
next_node_id = node.true_node_id
else:
next_node_id = node.false_node_id
var next_node = _get_node_by_id(next_node_id)
This approach is more explicit and can handle complex conditions more clearly.
Using Match Statement
If you have multiple conditions or values to choose from, you might consider using a match statement, which is more flexible than ternary operators for handling multiple cases:
var condition_result = _evaluate_condition(node)
var next_node_id
match condition_result:
0:
next_node_id = node.false_node_id
_:
next_node_id = node.true_node_id
var next_node = _get_node_by_id(next_node_id)
In this example, if condition_result is 0, it uses node.false_node_id; otherwise, it uses node.true_node_id. This approach is useful if you have more than two possible outcomes based on different conditions.