如何在Godot的string export中传入数组作为提示?
在Godot中复用数组作为导出变量的提示选项
一、Godot 4+ 推荐方案:@export_enum 直接复用数组
Godot 4 引入的 @export_enum 注解支持直接传入数组变量作为枚举选项,彻底解决重复编写的问题:
extends Node var things_list := ["thing_1", "thing_2", "thing_3"] @export_enum(things_list) var current_thing := things_list[0]
编辑器属性面板会自动加载 things_list 中的选项,无需重复写字符串字面量。
二、Godot 3.x 替代方案:用常量复用字符串
Godot 3.x 不支持在 export 注解中直接引用数组变量,但可以通过定义字符串常量避免重复编写:
extends Node const THING_1 := "thing_1" const THING_2 := "thing_2" const THING_3 := "thing_3" var things_list := [THING_1, THING_2, THING_3] export(String, THING_1, THING_2, THING_3) var current_thing := THING_1
通过常量统一管理字符串值,修改时只需调整常量即可,避免多处同步修改的麻烦。
三、关于获取导出提示选项
GDScript 没有 current_thing.export_hints 这类直接语法,但可以通过 ClassDB 在运行时获取属性的导出信息:
Godot 4 示例:
func _ready(): var prop_info := ClassDB.get_property_info(self, "current_thing") if prop_info.hint == PROPERTY_HINT_ENUM: var options := prop_info.hint_string.split(",") print(options) # 输出 ["thing_1", "thing_2", "thing_3"]
Godot 3.x 示例:
func _ready(): var prop_info = ClassDB.get_property_info(self, "current_thing") if prop_info.hint == PROPERTY_HINT_ENUM: var options = prop_info.hint_string.split(",") print(options) # 输出对应选项列表
内容的提问来源于stack exchange,提问作者cak3_lover
相关产品推荐
相关产品推荐

