tutorials/scripting/evaluating_expressions #177
Replies: 1 comment
-
So I like Expressions. There's lots of stuff you can do that you might not have expected is possible at first glance. Here's a little cookbook. In these examples, AssignmentAn expression like # position.x = 100
expr.parse("set_indexed(\"position:x\", 100)")
expr.execute([], self) Multiple function callsIf you want to call two or more methods in one expression, you can put them inside an array. # print(0); print(1)
expr.parse("[print(0), print(1)]")
expr.execute() This is something you can't do in regular GDScript because Conditional valueThe ternary if/else operator does not seem to work as expected inside # "old enough" if (age >= 18) else "too young"
expr.parse("[\"too young\", \"old enough\"][int(age >= 18)]", ["age"])
expr.execute([12]) # too young
epxr.execute([22]) # old enough Conditional executionIf you have the functions to call within the base instance, you can conditionally execute function calls. # hide() if visible else show()
expr.parse("[show, hide][int(visible)].call()")
expr.execute([], self) |
Beta Was this translation helpful? Give feedback.
-
tutorials/scripting/evaluating_expressions
Godot provides an Expression class you can use to evaluate expressions. An expression can be: A mathematical expression such as(2 + 4) * 16/4.0., A built-in method call like deg_to_rad(90)., A meth...
https://docs.godotengine.org/en/stable/tutorials/scripting/evaluating_expressions.html
Beta Was this translation helpful? Give feedback.
All reactions