How can I get better access to a Node's metadata?

Godot Version

v4.3.stable.official [77dcf97d8]

Question

I’m using a Node with metadata. This works great, but one of the metadata entries is a Vector2i array.

  1. I’d like to resize this array from another Scene during runtime, but I can’t find a way how to do it.

  2. I’d also like to modify a particular entry of the metadata array. But, again, I can’t find a way how to access a particular Array part. I tried various
    MyNodeWithMetadata.set_meta("local_d", ???) = my_value or
    MyNodeWithMetadata.set_meta("local_d")[0] = my_value
    …and similar, but no luck.

I “solved” both problems by copying the whole array locally, modifying it and then copying it back like this:

var A = MyNodeWithMetadata.get_meta("local_c")
if some_condition: A.resize(new_size)
for s in A.size(): pass # some calculations
MyNodeWithMetadata.set_meta("local_c", A)

but this doesn’t look too efficient. Is there a better way to do it?

Just access them normally like this:

func _ready() -> void:
	var value := [Vector2i(1, 1)]
	$Node.set_meta("my_value", value)
	$Node.get_meta("my_value").resize(5)
	for i in $Node.get_meta("my_value").size():
		$Node.get_meta("my_value")[i] = Vector2i(2, 2)
	$Node.get_meta("my_value")[0] = Vector2i(0, 0)
	$Node.get_meta("my_value").append(Vector2i(-1, -1))

and define a shortcut like var A = MyNodeWithMetadata.get_meta("local_c") is good when you need to do some complex calculations.

func _ready() -> void:
	var value := [Vector2i(1, 1)]
	$Node.set_meta("my_value", value)
	
	var my_value = $Node.get_meta("my_value") # it's a reference
	my_value.resize(5)
	for i in my_value.size():
		my_value[i] = Vector2i(2, 2)
	my_value[0] = Vector2i(0, 0)
	my_value.append(Vector2i(-1, -1))

Both of them works well.

$Node.get_meta(“my_value”)[i] = Vector2i(2, 2)

Hmm so in order to set it I have to “get” it… and the [i] is added after the brackets :sweat_smile: haha no wonder I was not able to figure it out!

Anyway this works, thank you! I am able to set all the required data using your examples:
image

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