【Godot】制作动态属性列表

tech2024-06-17  70

Godot 3.2.3

话不多说,直接上代码。下面是我正在做的游戏的一个光环特效节点的代码

class_name HaloList extends Node """面板属性""" var halo_num: int = 1 setget set_halo_num # 光环数量 """私有变量""" var property_data: Dictionary = {} # 记录属性的数据 var property_list = [] ##============================== ## setget方法 ##============================== func set_halo_num(value) -> void: halo_num = max(0, value) property_list_changed_notify() # 发出通知更新属性面板 ##============================== ## 内置方法 ##============================== func _get_property_list() -> Array: var group_name: String property_list = [ {name='HaloProperty', type=TYPE_NIL, usage=PROPERTY_USAGE_CATEGORY | PROPERTY_USAGE_SCRIPT_VARIABLE}, {name='halo_num', type=TYPE_INT}, ] # 动态添加属性 for i in range(halo_num): group_name = 'halo' + str(i) + '/' property_list.append({name=group_name + 'color', type=TYPE_COLOR}) property_list.append({name=group_name + 'duration_min', type=TYPE_REAL, hint=PROPERTY_HINT_RANGE, hint_string='0.01,1,0.01,or_greater'}) property_list.append({name=group_name + 'duration_max', type=TYPE_REAL, hint=PROPERTY_HINT_RANGE, hint_string='0.01,1,0.01,or_greater'}) property_list.append({name=group_name + 'interval_min', type=TYPE_REAL, hint=PROPERTY_HINT_RANGE, hint_string='0.01,1,0.01,or_greater'}) property_list.append({name=group_name + 'interval_max', type=TYPE_REAL, hint=PROPERTY_HINT_RANGE, hint_string='0.01,1,0.01,or_greater'}) property_list.append({name=group_name + 'scale_min', type=TYPE_REAL}) property_list.append({name=group_name + 'scale_max', type=TYPE_REAL}) property_list.append({name=group_name + 'alpha_min', type=TYPE_REAL, hint=PROPERTY_HINT_RANGE, hint_string='0,1,0.01'}) property_list.append({name=group_name + 'alpha_max', type=TYPE_REAL, hint=PROPERTY_HINT_RANGE, hint_string='0,1,0.01'}) property_data = {} return property_list #>>>> 记录数据(如果不做下面的步骤,每次开始游戏的时候,预先设置好的属性就会重新变成默认值) func _set(property: String, value) -> bool: if property.begins_with('halo'): property_data[property] = value return true func _get(property: String): if property.begins_with('halo'): if property_data.has(property): return property_data[property] else: return null

OK,完成。

最新回复(0)