Skip to content

Instantly share code, notes, and snippets.

@ratkingsminion
Created February 4, 2024 11:27
Show Gist options
  • Save ratkingsminion/160e2400a06cc824032ff2906aa8f8e9 to your computer and use it in GitHub Desktop.
Save ratkingsminion/160e2400a06cc824032ff2906aa8f8e9 to your computer and use it in GitHub Desktop.
GDScript example for _get_property_list() in combination with a resizable array
@tool
extends Node
@export var hammers:Array = [ 0, 0, 0 ]
func _get(property:StringName) -> Variant:
if property.begins_with("hammer_type"):
return hammers[int(str(property))]
match property:
&"hammer_count": return hammers.size()
return null
func _set(property:StringName, value:Variant) -> bool:
if property.begins_with("hammer_type"):
hammers[int(str(property))] = int(value)
return true
match property:
&"hammer_count":
if value < 0: return false
hammers.resize(int(value))
notify_property_list_changed()
return true
return false
func _validate_property(property:Dictionary) -> void:
match property.name:
"hammers": property.usage = PROPERTY_USAGE_NO_EDITOR
func _get_property_list() -> Array[Dictionary]:
var properties:Array[Dictionary] = []
properties.append({
"name": "hammer_count", "type": TYPE_INT,
"usage": PROPERTY_USAGE_EDITOR
})
for i in hammers.size():
properties.append({
"name": "hammer_type_" + str(i), "type": TYPE_INT,
"usage": PROPERTY_USAGE_EDITOR,
"hint": PROPERTY_HINT_ENUM, "hint_string": "Wooden,Iron,Golden,Enchanted"
})
return properties
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment