Attention: Here be dragons

This is the `latest` (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

# 表达式估值¶

Godot 提供了 Expression 类，可以用来对表达式进行估值。

• 类似 `(2 + 4) * 16/4.0` 的数学表达式。

• A built-in method call like `deg_to_rad(90)`.

• 调用 Expression.execute() 时如果 `base_instance``null`，那么调用用户提供脚本的方法，比如 `update_health()`

Expression 类是独立于 GDScript 的。即便禁用 GDScript 模块编译 Godot 也能使用。

## 基本用法¶

```var expression = Expression.new()
expression.parse("20 + 10*2 - 5/2.0")
var result = expression.execute()
print(result)  # 37.5
```

`+`

Godot 所支持的所有 Variant 类型都可以使用：整数、浮点数、字符串、数组、字典、颜色、向量……

```# Returns 1.
[1, 2][0]

# Returns 3. Negative indices can be used to count from the end of the array.
[1, 3][-1]

# Returns "green".
{"favorite_color": "green"}["favorite_color"]

# All 3 lines below return 7.0 (Vector3 is floating-point).
Vector3(5, 6, 7)[2]
Vector3(5, 6, 7)["z"]
Vector3(5, 6, 7).z
```

## 向表达式传递变量¶

```var expression = Expression.new()
# Define the variable names first in the second parameter of `parse()`.
# In this example, we use `x` for the variable name.
expression.parse("20 + 2 * x", ["x"])
# Then define the variable values in the first parameter of `execute()`.
# Here, `x` is assigned the integer value 5.
var result = expression.execute([5])
print(result)  # 30
```

## 为表达式设置基础实例¶

```func double(number):
return number * 2

var expression = Expression.new()
expression.parse("double(10)")

# This won't work since we're not passing the current script as the base instance.
var result = expression.execute([], null)
print(result)  # null

# This will work since we're passing the current script (i.e. self)
# as the base instance.
result = expression.execute([], self)
print(result)  # 20
```

• 在表达式中引用实例的常量（`const`）。

• 在表达式中引用实例的成员变量（`var`）。

• 在表达式中调用定义在实例上的方法，并使用其返回值。

## 示例脚本¶

```const DAYS_IN_YEAR = 365
var script_member_variable = 1000

# Constant mathexpression.
evaluate("2 + 2")
# Math expression with variables.
evaluate("x + y", ["x", "y"], [60, 100])

# Call built-in method (built-in math function call).

# Call user method (defined in the script).
# We can do this because the expression execution is bound to `self`
# in the `evaluate()` method.
# Since this user method returns a value, we can use it in math expressions.
evaluate("call_me() + DAYS_IN_YEAR + script_member_variable")
evaluate("call_me(42)")
evaluate("call_me('some string')")

func evaluate(command, variable_names = [], variable_values = []) -> void:
var expression = Expression.new()
var error = expression.parse(command, variable_names)
if error != OK:
push_error(expression.get_error_text())
return

var result = expression.execute(variable_values, self)

if not expression.has_execute_failed():
print(str(result))

func call_me(argument = null):
print("\nYou called 'call_me()' in the expression text.")
if argument:
print("Argument passed: %s" % argument)

# The method's return value is also the expression's return value.
return 0
```

```4
160
1.5707963267949

You called 'call_me()' in the expression text.
1365

You called 'call_me()' in the expression text.
Argument passed: 42
0

You called 'call_me()' in the expression text.
Argument passed: some string
0
```

## 内置函数¶

All methods in the Global Scope are available in the Expression class, even if no base instance is bound to the expression. The same parameters and return types are available.