Distance with top level objects

Godot Version

4.3 stable

Question

So I want the distance between my spell and the player that cast the spell, but the spell shouldn’t move with the player, so I set the top_level property to true to prevent it from moving with the player, but now when I try and get the distance using the difference between the global positions it doesn’t work as the spells global position always starts at (0, 0, 0) no matter where it’s created. The spell also needs to be a child of the spell caster so that it can use get_parent to know who cast the spell. How can I get the distance between the parent of the spell and the spell without having the spell move with the spell caster?

P.s. I am doing this in Rust, but the methods in Rust and GDScript are almost always the same, so any advice for GDScript probably also applies.

You could create a variable on “spell” to store who casted it, which could be cheaper than get_parent and top_level shenanigans.

Eitherway, does position work, not global_?


Props for rust, gdextension?

I think there were borrowing issues when I tried to store the parent as a field but I can’t remember the details and it was for a different problem so I’ll try that again, but I have a feeling it won’t work.

position gives the same data in this case as global_position so no sadly it doesn’t work.

And thanks for the props, I wanted to learn Rust and needed the performance, so yeah I decided to use GDExtension.

Can you show your code for creating the spell? Maybe you are adding it as a child before setting the initial position?

1 Like

The project is MMSpellbook. I add it as a child and then set the position as you say, but that’s because I was having issues when doing it the other way around. You can see the spell creation in the method cast_spell of the MagicalEntity struct in the MagicalEntity.rs file.

God, you were completely right, I don’t know why it’s working, because I swear it broke itself when I did this last time, but setting the position and then adding it as a child has fixed it, global position now returns it’s actually global position. Thanks a lot!

The _ready override function will run immediately when added to the scene, I presume that is where you were checking position.

It depends, the player can choose if it’s checked in ready or in physics_process, when I did testing in physics_process I think it was still treating global position as starting at where the player created the spell. I don’t really get why, but it working is enough for me. Again, thanks for the help :slight_smile:

Edit: Damn, why does it display the : ) when together as an emoji?! That’s annoying.

1 Like

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