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.

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 來指定可能出現在運算式中的變數名稱,這樣就可以在執行運算式時進行綁定。