我正在尝试为我的播放器 Controller 创建自定义类型。
首先我选择了“脚本类”:
extends Node
class_name PlayerController
export var max_speed = 32
export var id = 1
export(NodePath) var node_path
onready var node = get_node(node_path)
...
我希望它像节点一样位于“PlayerController”专用部分中,因此可以继承并查看那里的漂亮属性堆栈。
我认为这是由于“脚本类”与常规扩展相比被简化了。所以我创建了一个。
tool
extends EditorPlugin
func _enter_tree():
add_custom_type("PlayerController", "Node", preload("PlayerController.gd"),preload("PlayerController.svg"))
func _exit_tree():
pass
结果实际上是一样的。
我发现以这种方式创建的“自定义类型”不是“自定义类型”,而只是附加了脚本的父类型。可以在文档中确认:
void add_custom_type(type: String, base: String, script: Script, icon: Texture)
Adds a custom type, which will appear in the list of nodes or resources. An icon can be optionally passed.
When given node or resource is selected, the base type will be instanced (ie, "Spatial", "Control", "Resource"), then the script will be loaded and set to this object.
You can use the virtual method handles() to check if your custom object is being edited by checking the script or using the is keyword.
During run-time, this will be a simple object with a script so this function does not need to be called then.
我的问题是: 可以创建真正的自定义类型吗?
最佳答案
在检查器面板中添加类别
首先,如果您想要的是检查器面板中的专用部分,您可以使用工具
(参见 Running code in the editor )脚本和 _get_property_list
来实现。 .
这是一个基于 another answer 的示例:
tool
extends Node
export var something_else:String
var custom_position:Vector2
var custom_rotation_degrees:float
var custom_scale:Vector2
func _get_property_list():
return [
{
name = "Custom",
type = TYPE_NIL,
hint_string = "custom_",
usage = PROPERTY_USAGE_CATEGORY
},
{
name = "custom_position",
type = TYPE_VECTOR2
},
{
name = "custom_rotation_degrees",
type = TYPE_REAL
},
{
name = "custom_scale",
type = TYPE_VECTOR2
}
]
在那里我创建了一个名为“自定义”的类别。请注意,type
为 TYPE_NIL
,usage
为 PROPERTY_USAGE_CATEGORY
。文档就是这样做的。请参阅Adding script categories .
使用 PROPERTY_USAGE_CATEGORY
将生成一个命名 header ,类似于问题图片上显示“Node”的 header 。使用PROPERTY_USAGE_GROUP
创建可折叠组。
hint_string
的值将用作前缀。具有该前缀的任何属性都将属于该类别。比如你在代码中看到的,每个都有各自的类型。这些都是在我的代码中用 var
声明的。但是,不能使用export
,因为这样它们会出现两次。
它看起来如下:
添加自定义类型
当我们在脚本中创建一个类时,它就是一个类型。 GDScript 类型。证明它们是 GDScript 类型的证据。请注意,使用 class_name
时,您可以声明该类型的变量,并使用 is
运算符检查值是否属于该类型。并且您可以将 extends
与它们一起使用。
还有核心类型。这也是 GDScript 类型。因此,您还可以使用声明该类型的变量,对它们使用 extend
并使用 is
检查它们。
但是,我们在脚本中创建的类不是核心类型。我们无法真正从 GDScript 或任何其他脚本语言创建核心类型。我们所能做的就是向现有的核心类型添加一个脚本。
如果你想创建实际的核心类型,你需要使用C++并创建一个Godot Module .
您可能还对 GDExtension 感兴趣在《戈多 4》中。
关于godot - 有没有办法在 Godot 中创建真正的自定义类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69636833/