A better way to cache a bunch of bone IDs?

Godot Version

4.6.2

Question

I’m doing some stuff with bones. I learned there are no structs in GDScript, so I tried storing them in an array, and referencing by enum.
However, typing this out everywhere quickly became annoying:

skeleton.get_bone_global_pose( bone_idx[BoneId.LEFT_FOOT] )

So instead, I just did this:

@onready var skeleton: Skeleton3D = $"../PivotGround/proto/Skeleton3D"
@onready var spine_0: int = skeleton.find_bone("spine")
@onready var spine_1: int = skeleton.find_bone("spine_001")
@onready var spine_2: int = skeleton.find_bone("spine_002")
@onready var thigh_left: int = skeleton.find_bone("thigh_L")
@onready var thigh_right: int = skeleton.find_bone("thigh_R")
@onready var shin_left: int = skeleton.find_bone("shin_L")
@onready var shin_right: int = skeleton.find_bone("shin_R")
@onready var foot_left: int = skeleton.find_bone("foot_L")
@onready var foot_right: int = skeleton.find_bone("foot_R")

So that I could just do this:

skeleton.get_bone_global_pose(foot_left)

And I have no issues with it, but I just wonder if there’s a more elegant way of storing and retrieving information like this.

There kind of is, keep in mind in C++ the only difference between a class and a struct is their default public/private value for variables, class is private by default, and struct is public by default.

class MyStruct:
    var x: int = 0
    var bone: String = "spine"

Though I’m not sure how a struct will help you with this.

Well… I’m not sure either, to be quite honest! haha
I guess I just thought typing out a bunch of @onready variables for each bone was somehow wrong, that string lookups were expensive, and that maybe there was a way to loop over the whole skeleton and store it in a human readable way (MyStruct.spine) that avoids repeat string lookups.

You can use a dictionary and do something like this:

var bone_indexes: Dictonary[String, int] = {}

for bone_idx: int in skeleton.get_bone_count():
	var bone_name: String = skeleton.get_bone_name(bone_idx)
	bone_indexes[bone_name] = bone_idx

Then you can access them by name like: bone_indexes.left_foot

That looks to be exactly what I’m looking for. Thanks!
Now as to whether or not I actually need do store the whole skeleton like this.. I’m starting to have doubts. But it’s nice to have some options, so thanks to both of you.

Ya know, originally I read this, saw the “class MyStruct”, and my brain interpreted it as me needing to create and maintain another script for this, which seemed awkward for the task.

But I was just looking for a way neatly package up all the position/normal/etc data for a raycast, discovered the difference between class and class_name, and your post immediately dawned on me. I naively thought they were related. :sweat_smile: