+1 vote

So in my game server, I have an array of players. Each player is a dictionary, for example:

var players = [{ "name": "Mark", "position": 10 }, { "name": "Frank", "position": 2 }]

Now, I'd like to interpolate the position properties of the players, I tried this:

$Tween.interpolate_property(self, "players:0:position", players[0].position, 20, 0.2)
$Tween.start()

But Godot throws an error saying that it can't find the node. Is there any way to find the index of the array in a NodePath?

Godot version 3.5 stable
in Engine by (120 points)

1 Answer

+1 vote
Best answer

The first argument is the target object, which should be players[0] in your example, and the second argument is the key of the property on that object. I'm unsure if dictionary entries count as "properties", but if they do you can try something like this:

$Tween.interpolate_property(players[0], "position", players[0].position, 20, 0.2)
by (1,111 points)
selected by

Sadly Godot doesn't count it as an object: when I run the code it says

Invalid type in function 'interpolate_property' in base 'Tween'. Cannot convert argument 1 from Dictionary to Object.

It did work in Godot 2 as far as I know though

Hm, alright. According to this reddit thread it might be possible with colon separation if the dictionary is a variable on the target object, like you tried. But I'm not sure if that is possible with the dictionary inside an array. Perhaps changing 'players' to a dictionary too, could work?

You're right, it should work with a dictionary inside a dictionary. But I don't know if it would work for loops. Anyway, I'll give it a look tomorrow, thanks.

For looping I think you can do something like:

for key in players_map:
    $Tween.interpolate_property(self, "players:" + key + ":position", players_map[key].position, 20, 0.2)

Assuming nested dictionaries.

Another alternative could be to create a custom class for the player info, rather than using a dictionary. That way you can send the player info object as "target object" to the tween.

I tried the dictionary in a dictionary solution and it worked, thank you!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.