Possible to override a Nodes default getter/setter functions?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jarlowrey

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 - Enhancement: enable override of default Node functions · Issue #18654 · godotengine/godot · GitHub

:bust_in_silhouette: Reply From: Zylann

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.

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.

jarlowrey | 2018-04-05 02:48

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.

njamster | 2020-06-12 14:20

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.

Zylann | 2020-06-12 16:04

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

njamster | 2020-06-13 11:27