nodes instanced not available in _Ready()

BigAlNaAlbaBigAlNaAlba Posts: 2Member

Hi, I am getting a Null Reference exception when I run this simple C# code. I am sure it is a stupid oversight on my part.

I am able to assign a value to my label.Text in _Ready() but not in _Process(). Can somebody help me understand where I am going wrong? I need a fresh set of eyes.

using Godot;
using System;

public class Player : KinematicBody2D
{
    public int speed = 100; //Movement speed of character
    public Vector2[] path; //Path to be followed
    public int currentPoint = 1; //The current point in the path
    public AnimatedSprite sprite;
    public Label label;

    public override void _Ready()
    {
        AnimatedSprite sprite = GetNode<AnimatedSprite>("AnimatedSprite");
        Label label = GetNode<Label>("Label");
        label.Text = Name;
        SetProcess(true);
    }

    public override void _Process(float delta)
    {
        var moveDistance = speed * delta;
        MoveAlongPath(moveDistance);
    }

    public void MoveAlongPath (float distance)
    {
        var startPoint = this.Position;
        if(path != null)
        {
            if (path.Length > 0)
            {
                var distanceToNext = this.Position.DistanceTo(path[currentPoint]);
                if(distance <= distanceToNext && distance >= 0.0)
                {
                    this.Position = startPoint.LinearInterpolate(path[currentPoint], distance/distanceToNext);
                    this.label.Text = "Moving"; //*** CRAPS OUT HERE *** <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                    if(Position.x - path[currentPoint].x > 0) {GD.Print("left");}
                    if(Position.x - path[currentPoint].x < 0) {GD.Print("right");}

                }
                else
                    {
                        GD.Print("idle");
                        if(currentPoint < path.Length -1 ) { currentPoint++;}
                    }
            }
        }
    }

    public void _on_MainScene_MoveUnit(Vector2[] UnitPath, int NavPoint)
    {
        path = UnitPath;
        currentPoint = NavPoint;
    }
}

Comments

  • BigAlNaAlbaBigAlNaAlba Posts: 2Member
    edited July 25

    GAH!!!! Duplicate instance of label on lines 10 and 15. I knew it would be something dumb.

    I am leaving this here in case it helps somebody else.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file