Search code examples
c#instancegodot

How do I change variables of a newly created instance?


I have been trying to finish a simple obstacle in my upcoming game. It's an eye that shoots lasers after a certain amount of time has passed. I've gotten the timer and the instancing of the lasers to work so far, but for some reason, updating the initial position of the laser and informing it of which direction it should be facing has been more than a challenge.

The relevant code is this:

    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
        probe = GetNode<Area2D>("FacingProbe");
        lidSprite = GetNode<AnimatedSprite2D>("EvilEyeLid");
        ballSprite = GetNode<AnimatedSprite2D>("EvilEyeBall");
        ballSprite.Play("Idle");
        switch (direction)
        {
            case 1: lidSprite.Rotation = 3.1416f; probe.Position = new Vector2(-20,0); break;
            case 2: lidSprite.Rotation = 0; probe.Position = new Vector2(20,0); break;
            case 3: lidSprite.Rotation = 4.712f; probe.Position = new Vector2(0,-20); break;
            case 4: lidSprite.Rotation = 1.5708f; probe.Position = new Vector2(0,20); break;
        }
    }

    // Called every frame. 'delta' is the elapsed time since the previous frame.
    public override void _Process(double delta)
    {
        if (pause == 0)
        {
            var packedLazer = GD.Load<PackedScene>("res://Entities/Lazer.tscn");
            var lazer = packedLazer.Instantiate();
            lazer.Position = probe.Position;
            lazer.Direction = Direction;
            AddChild(lazer);
        }
        animations();
    }

For additional context:
'probe' is an Area2D located 20 units in front of the eye.
The game is tile based (square) and each tile is 20 units big
'Direction' is an integer variable on both the Eye and the laser it shoots
and the only code that's causing issues is the stuff inside of if (pause == 0)

The errors I'm receiving are

'Node' does not contain a definition for 'Position' and no accessible extension method 'Position' accepting a first argument of type 'Node' could be found
'Node' does not contain a definition for 'Direction' and no accessible extension method 'Direction' accepting a first argument of type 'Node' could be found
The name 'Direction' does not exist in the current context

for just a bit more context, here's both relevent properties under the inspector: EvilEye inspector screenshotLazer inspector screenshot

I'm genuinely not sure what I'm doing wrong, and instancing along with Exported variables like 'Direction' are completely new to me, so I'm likely falling to a beginner mistake.


Solution

  • Here the type of packedLazer will be PackedScene:

    var packedLazer = GD.Load<PackedScene>("res://Entities/Lazer.tscn");
    

    And the instantiate method of PackedScene returns a Node, so here lazer will be a Node.

    var lazer = packedLazer.Instantiate();
    

    And what the error tells you is correct, Node does not have definitions for Position or Direction.

    You should cast the result of Instantiate to the appropriate type, for example, if the type is Lazer you can do this:

    var lazer = packedLazer.Instantiate() as Lazer;
    

    Then it should work.