+2 votes

When my node rotates I want a specific child to maintain a 0 degree rotation. The easiest way I can think of doing this is overriding Node2D's set_global_rotation function, if it exists. Is this possible?

Edit: See also - https://github.com/godotengine/godot/issues/18654#issuecomment-386919138

in Engine by (695 points)
edited by

1 Answer

+1 vote
Best answer

You can't override any function with scripts, if it's not marked virtual in the docs. You can't override properties either, at least I think it's not intented to work.

If you want a child node to keep its rotation, you have a few options:

1) Not make it a child of the rotating part, though you might want to keep it in the same scene. You could eventually unparent the child at runtime using a script.

2) Rotate a child and not the parent, especially if the rotation is only for visual purpose. Typically, you could do that if you separate the visual part of your scene in a distinct branch so that you get more control over its transforms. I usually go for that solution.

3) Add an intermediate Node between the parent and the child (so you have parent -> node as child -> your non-rotating node as sub-child). However, doing so makes the sub-child behave as if it was in world space so you will need to move it manually.

4) Don't use nodes, use _draw or servers directly and calculate transforms yourself. This is a bit too advanced for that use case though.

by (29,120 points)
selected by

Good to know. I made a new function just for rotation and added all my logic there, that seemed the simplest solution. It's good to know all these other methods though too.

You can't override any function with scripts, if it's not marked virtual in the docs.

That's seems to be incorrect, see the comments over here.

But it won't override it. It only "shadows" the original function in GDScript, so it looks like it works if you call it from GDScript. If the engine wants to set the position of your node, it won't necessarily bother calling your script's set_position because the setter is not meant to be virtual. At least it won't happen consistently.

I see. Thank you for the clarification, makes sense now! :)

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.