Expression

继承: RefCounted < Object

存储您可以执行的表达式的类。

描述

表达式可以由任何算术运算、内置数学函数调用、传递实例的方法调用或内置类型构造函数调用组成。使用内置数学函数的示例表达式可以是sqrt(pow(3, 2) + pow(4, 2))。在下面的示例中,我们使用LineEdit节点来编写表达式并显示结果。

var expression = Expression.new()

func _ready():
    $LineEdit.text_submitted.connect(self._on_text_submitted)

func _on_text_submitted(command):
    var error = expression.parse(command)
    if error != OK:
        print(expression.get_error_text())
        return
    var result = expression.execute()
    if not expression.has_execute_failed():
        $LineEdit.text = str(result)

方法

Variant

execute(inputs: Array = [], base_instance: Object = null, show_error: bool = true, const_calls_only: bool = false)

String

get_error_text() const

bool

has_execute_failed() const

Error

parse(expression: String, input_names: PackedStringArray = PackedStringArray())


方法说明

Variant execute(inputs: Array = [], base_instance: Object = null, show_error: bool = true, const_calls_only: bool = false) 🔗

执行先前由parse()解析的表达式并返回结果。在使用返回的对象之前,应通过调用has_execute_failed()检查方法是否失败。

如果您在parse()中定义了输入变量,则可以在输入数组中以相同的顺序指定它们的值。


String get_error_text() const 🔗

如果parse()execute()失败,则返回错误文本。


bool has_execute_failed() const 🔗

如果execute()失败,则返回true


Error parse(expression: String, input_names: PackedStringArray = PackedStringArray()) 🔗

解析表达式并返回Error代码。

您可以选择使用input_names指定可能出现在表达式中的变量名称,以便在执行时绑定它们。