Godot 遊戲引擎指南 / 建立資源
外觀
< Godot 遊戲引擎指南 | 外掛
要建立一個新的 資源,您需要建立一個新的指令碼(該指令碼未附加到節點)。以下是一個示例
tool extends Resource export (int, INT, STRING) var property_type = 0 setget set_type var property func _get_property_list(): var out = [ { "name": "Property", "type": [TYPE_INT, TYPE_STRING][property_type] } ] return out func _get(what): if what == "Property": return property func _set(what, to): if what == "Property": what = to func set_type(value): property_type = value match value: 0: if !property is int: property = int(property) 1: if !property is String: property = String(property)
一個簡單的示例,它展示了一個可以儲存 int 或 String 的資源。
_get_property_list() 必須返回一個 陣列 的 字典,每個字典都包含以下值
以下是可選的
- hint (int):
PROPERTY_HINT_*常量。 - hint_string (字串)
- usage (int):
PROPERTY_USAGE_*常量。
property_type 太簡單了,不需要用 _get_property_list() 來更改或定義。相反,使用 export,以及 setget 來處理更改時的事件。
當 property_type 設定後,property 的型別就會改變。
現實世界的例子包括儲存鍵或影像。資源只是為其他指令碼儲存資料,它本身並不實際處理資料。
