跳轉到內容

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)

一個簡單的示例,它展示了一個可以儲存 intString 的資源。


_get_property_list() 必須返回一個 陣列字典,每個字典都包含以下值

  • name (字串): 屬性的名稱。
  • type (int): 以 TYPE_* 開頭的常量。

以下是可選的

  • hint (int): PROPERTY_HINT_* 常量。
  • hint_string (字串)
  • usage (int): PROPERTY_USAGE_* 常量。

property_type 太簡單了,不需要用 _get_property_list() 來更改或定義。相反,使用 export,以及 setget 來處理更改時的事件。

property_type 設定後,property 的型別就會改變。

現實世界的例子包括儲存鍵或影像。資源只是為其他指令碼儲存資料,它本身並不實際處理資料。


Godot 遊戲引擎指南

入門 [編輯]
安裝
什麼是節點?
程式設計
資源和匯入
訊號和方法
您的第一個遊戲
使其工作
除錯
輸入
物理
儲存和載入
多人遊戲
使其看起來更好
UI 皮膚
動畫
高階幫助
伺服器(單例)
平臺特定
最佳化
加密
匯出
外掛
其他
有用連結
作者和貢獻者
列印版


<-- 上一個 返回頂部 下一個 -->

華夏公益教科書