How to show a PackedScene instanced in a child in the parent scene?

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

I am making a 2D shooter with Godot .mono and I have the following structure for my main scene and “player”:

Node2D (main scene node)
|_CanvasLayer
|_TextureRect
|_KineticBody2D (player node)

The player scene instances a packed scene for my “bullet”:

[Export]
public PackedScene PhaserBurst;

private AnimatedSprite _sprite;

public override void _Ready()
{
    PhaserBurst = GD.Load<PackedScene>("res://Game/Weapons/MechProjectiles/IntroMech/IntroMechPhaserBurst.tscn");
    _sprite = GetNode<AnimatedSprite>("IntroPlayerSprite");
    var burst = (Area2D)PhaserBurst.Instance();
    AddChild(burst);
}

When I run the player scene by itself, everything works perfect. However, when I run the main scene, which has the player as a child, everything works except the PackedScene being instanced. It’s not just a matter of the position being off screen, printing to the console during the instantiation confirms that there is no PackedScene while running the main scene.

I’ve tried adding the instance as a child to every node in the parent scene tree, but no luck. Though I’m using c sharp, I would appreciate any input even for gdscript (I’m a .NET developer, so switching the syntax to c sharp hasn’t been too difficult). I’m also very happy to add anymore context/code/screenshots needed to get me pointed in the right direction. I really appreciate any input!

Hey zel,

do you get an error message?

juppi | 2021-11-21 13:36

Nope! But I actually fixed this, I’ll respond below. Thanks for responding!

zel | 2021-11-21 22:45

:bust_in_silhouette: Reply From: Inces

It feels like issue with export keyword. Perhaps You forgot, that You exported other variable type than packedscene in your main scene player ? Anyways, since You hardcode packedscene in ready function, try to get rid of export keyword above.

I actually fixed the issue, and it wasn’t due to the export. It was actually much sillier than that and a big oversight in my debugging.

Basically, the packed scene is being instanced every time my player hits the “ui_select” Input. Initially, I had this logic in an Unhandled Input event handler(per the tutorial I was following). It worked for running the player scene by itself, but once I had the player as a child in another scene, the event wasn’t even being detected. Moving it into a normal if statement inside of _Process actually triggers the instance now:

public override void _Process(float delta)
{
    HandleMovement(_sprite);

    if (Input.IsActionPressed("ui_select") && PhaserCooldown.IsStopped())
    {
        FireIntroMechPhaser();
    }
}

Thanks for responding though!

zel | 2021-11-21 22:52

:bust_in_silhouette: Reply From: zel

I actually fixed the issue, and it wasn’t due to the export. It was actually much sillier than that and a big oversight in my debugging.

Basically, the packed scene is being instanced every time my player hits the “ui_select” Input. Initially, I had this logic in an Unhandled Input event handler(per the tutorial I was following). It worked for running the player scene by itself, but once I had the player as a child in another scene, the event wasn’t even being detected. Moving it into a normal if statement inside of _Process actually triggers the instance now:

public override void _Process(float delta)
{
    HandleMovement(_sprite);

    if (Input.IsActionPressed("ui_select") && PhaserCooldown.IsStopped())
    {
        FireIntroMechPhaser();
    }
}