Instance appears when AddChild'ed to button, not to hex tile

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

Hi, I have a bunch of hex tiles instantiated on world generation

        for(...)
                    Spatial p = (Spatial)LandHex.Instance();
                    p.Translate(new Vector3(x, y, z));
                    AddChild(p);

and so far all good. Then through a button I try to instantiate a scene with a couple of models, pretty standard stuff I imagine:

public void _on_ButtonFarm_button_up()
{
    GD.Print("Farm loaded");
    if(Hexagon.selectedHex != null)
    {
        Spatial p = (Spatial)Farm.Instance();
        Hexagon.selectedHex.AddChild(p);
    }
}

Now, the farm doesn’t show up here, but if instead of hexagon.selectedHex.AddChild(p) I just do AddChild(p), which will bind it to the button itself, it appears just fine. I have also attempted to Translate() it, Scale() it etc, just in case. Inspection in the editor with Remote does not show anything abnormal, all transforms etc have non-zero values.

Any ideas?

I have also tried doing .setVisible(false) on the tiles to make sure they’re not hiding it, and .setVisible(true) in case it inherits the visibility, but the child is nowhere to be seen.

The nodes:
Hexagon:

KinematicBody
    hexagon (Spatial)
        StaticBody (model with attached Hexagon.cs script)
            CollisionShape

Farm:

Spatial
    Spatial (with the actual model)

When I run it and switch to Remote, I see the farm attached under the StaticBody, which I expect since that’s where the Hexagon class lives

If the farm is a child of the tile and you hide the tile the farm will be hidden. Also what kind of node is the farm? Is it 2d or 3d, if it’s 2d it will take some extra work to make it show up in 3d. The main problem is that although you’ve explained what you’ve tried and the basic instantiation code you haven’t shown what the actual nodes are.

Merlin1846 | 2022-12-04 04:38

Sorry I’m still figuring it out, I’m not used to the nodes behaving differently between 2D and 3D.
I updated the post with the nodes, the farm becomes a child of the node with the model, as that’s where the Hexagon.cd script is attached (which I assume is the right place since it’s the most direct place to make the hexagon oscillate when it is selected). Everything is 3D.
Also the inherited hiding I (tried to) counter by explicitly setting the farm as visible, which again works when I add it as a child of the button but not of the tile.

homervp | 2022-12-04 09:24

Try seeing if the farm shows up correctly when it is not a child of the hex. Also make sure that if you’re moving the farm, not to move it relative to the world origin as that will through off you’re calculations; When the farm is a child of the hex the hex’s position is Vector3(0,0,0) as far as the farm is concerned.

Merlin1846 | 2022-12-04 18:23

You gave me an idea and were on kind of on point: I set the world coordinates of the farm, that worked so I checked the coordinates on the entire hierarchy above. I had a rogue scaling in a node above the hex, which did not affect the hex because it’s relative position to that node was (0,0,0), but apparently affected the farm because it had an offset, so that offset was scaling up. In hindsight that scaling was also not done properly, but at the time I hardly knew anything about the engine, but there’s still something that doesn’t add up in how the farm was affected (it flew off diagonally instead of vertically, and didn’t scale back down along with the selected hex which is pulsating). Now that I rectified the way I do the pulsation, it works properly. Thank you very much for pointing me in the right direction

TL;DR for anyone looking at this later: the entire hierarchy above will affect the position, including any scaling done. Try recursively printing all the parent nodes’ transforms to see if something is off.

homervp | 2022-12-05 01:19

Put this in the answer so this post no longer shows as unanswered.

Merlin1846 | 2022-12-05 17:53

Done, thanks again!

homervp | 2022-12-06 00:58

:bust_in_silhouette: Reply From: homervp

(Thanks to Merlin1846 for a push in the right direction) I had a rogue scaling in a node above the hex, which did not affect the hex because it’s relative position to that node was (0,0,0), but apparently affected the farm because it had an offset, so that offset was scaling up. In hindsight that scaling was also not done properly, but at the time I hardly knew anything about the engine, but there’s still something that doesn’t add up in how the farm was affected (it flew off diagonally instead of vertically, and didn’t scale back down along with the selected hex which is pulsating). Now that I rectified the way I do the pulsation, it works properly. Thank you very much for pointing me in the right direction

TL;DR for anyone looking at this later: the entire hierarchy above will affect the position, including any scaling done. Try recursively printing all the parent nodes’ transforms to see if something is off.