Creating Hierarchical Properties

Godot Version

4.2

Question

I’m trying to define an ability system, and wanted to model properties similar to how Unreal GAS does. In that system I see them defined as a hierarchy and using dot notation.

Example:
Attribute.Strength.Max

I want to be able to add these properties to an array variable in a resource type within the inspector. I’m having trouble finding a way to structure the properties as a hierarchy. I’ve tried a few things but they didn’t allow the definitions to be added to the resource’s array.

Any ideas how to do something like this? Thanks!

1 Like

Can you write some classes that extend Object or RefCounted? And have their member data be classes you also designed etc?

You can get this kind of thing using Dictionaries. E.g.:

extends Node2D

var d = {
	attribute = {
		strength = {
			value = 5,
			max = 25
			}
		}
	}

func _ready() -> void:
	print(d.attribute.strength.max)

Just stick that script on some random node and run. The keys must exist before you try to ‘dot’ access them.

I like this solution, but it doesn’t allow me to utilize it in an exported array:

image

Can you assign a type to it?

I tried something like “d:Dictionary”, but that didn’t fix this error. I’m pretty new to this, so I might just be defining something wrong.

Have you looked into using inner classes. I use it for making data structures as well methods to manipulate.

Example:

class Strength:
	var max: int=0
	var name: String=""
	
	func incr():
		max += 1

class Attributes:
	var strength:=Strength.new()
	var amount: float=10.0
	pass
	
func _ready():
	var attr=Attributes.new()
	attr.strength.max=10
	attr.strength.incr()
	attr.strength.name="ABC"
	attr.amount=100.0
	pass
3 Likes

I’ll take a look at that. Thanks

extends Node2D

@export var bag:Array[Dictionary]

var d = {
	attribute = {
		strength = {
			value = 5,
			max = 25
			}
		}
	}

func _ready() -> void:
	print(d.attribute.strength.max)
	var attr1 = d.duplicate(true)
	var attr2 = d.duplicate(true)
	attr2.attribute.strength.max = 500

	bag.append(attr1)
	bag.append(attr2)

	#etc?
	print(bag[0].attribute.strength.max)
	print(bag[1].attribute.strength.max)

Ah! Thank you that helps greatly

You have marked your “thank you” as the solution.

I am curious which of the last two posts you found most helpful.

I liked dBat’s last post. I’m still having trouble, but it is something I need to decide. Keep running into GDScript limitations.

1 Like

Would using c# be an option and solve your issues?

That is a great question. I like C# anyway, but didn’t know how much of the OO aspects I can use and still integrate with Godot. I haven’t found any tutorials yet, mostly just how to setup the C# environment.

You may be able to find some inspiration searching github for “using Godot” and limit to c# to find a wider variety of examples.

https://github.com/search?q="using+Godot"+language%3AC%23+&type=code

I have good luck doing github searches restricted to gdscript when searching for something, so perhaps a similar approach could work.

A couple of what looked like non trivial examples of c# godot code that also mentioned “ability”

That looks very promising. Thanks!

1 Like

I’m also looking into this, and have been spoiled by Unreal’s hierarchial gameplay tags, but not only due to their convenient query and lookup functions, but rather mostly because there’s a clean and simple interface for adding new tags. You basically do it in a table interface the same way you add input actions in Godot, and then every time you want to use them you get nice auto complete.

But to get that convenience, now we’re entering the realm of plugins, right? Or can this be created with the @tool functionality I’ve heard about?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.