• General Chat
  • Accessing variables from other nodes through code using C#

Hi,

I am new to Godot and I cant access a variable from another node from the same scene.

This is the code I want to access

using Godot;
using System;

public class Player : KinematicBody2D
{
    [Export]
    public float playerSpeed = 250f;

    [Export]
    public float framesPerSecond = 4f;
    float singleFrameTimer = 0;

    AnimatedSprite playerSprite;
    
    PackedScene loadPlayerBullet = GD.Load<PackedScene>("res://Scenes/Player_Bullet/Player_Bullet.tscn");

    [Export]
    public string previousSpriteState = "Forward";
    bool isJustFired = false;

    public override void _Ready()
    {
        playerSprite = GetNode<AnimatedSprite>("AnimatedSprite");
    }
    
    public override void _PhysicsProcess(float delta)
    {
        Vector2 playerMovement = new Vector2();

        if(isJustFired == true)
        {
            singleFrameTimer += delta;

            if(singleFrameTimer >= 1/framesPerSecond)
            {
                playerSprite.Animation = "Idle_" + previousSpriteState;
                isJustFired = false;
            }
        }

        if(Input.IsActionPressed("Move_Right"))
        {
            playerMovement.x = Input.GetActionStrength("Move_Right");
            playerSprite.Animation = "Idle_Right";
            previousSpriteState = "Right";
        } 
        else if(Input.IsActionPressed("Move_Left"))
        {
            playerMovement.x = -Input.GetActionStrength("Move_Left");
            playerSprite.Animation = "Idle_Left";
            previousSpriteState = "Left";
        }
        else if(Input.IsActionPressed("Move_Down"))
        {
            playerMovement.y = Input.GetActionStrength("Move_Down");
            playerSprite.Animation = "Idle_Forward";
            previousSpriteState = "Forward";
        }
        else if(Input.IsActionPressed("Move_Up"))
        {
            playerMovement.y = -Input.GetActionStrength("Move_Up");
            playerSprite.Animation = "Idle_Backward";
            previousSpriteState = "Backward"; 
        }

        if(Input.IsActionJustPressed("Fire"))
        {
            Node playerBullet = loadPlayerBullet.Instance();
            playerSprite.Animation = "Shoot_" + previousSpriteState;
            isJustFired = true;
            singleFrameTimer = 0f;
            GetTree().GetRoot().AddChild(playerBullet, true);
        }

        MoveAndCollide(playerMovement * playerSpeed * delta);
    }
}

I want to access previousSpriteState through this code

using Godot;
using System;

public class Player_Bullet : KinematicBody2D
{
    String directionOfMotion;

    public override void _Ready()
    {
        this.Position = GetTree().GetCurrentScene().GetNode<KinematicBody2D>("Player").Position;
        directionOfMotion = GetTree().GetCurrentScene().GetNode<KinematicBody2D>("Player").GetScript().previosSpriteState;
    }

    public override void _Process(float delta)
    {
      
    }
}

I don’t know for sure, but you probably need to change

directionOfMotion = GetTree().GetCurrentScene().GetNode<KinematicBody2D>(“Player”)

to

directionOfMotion = GetTree().GetCurrentScene().GetNode<KinematicBody2D>(“Player”).previousSpriteState

Though thinking about it, you probably can only access variables within the class you have retrieved. Since KinematicBody2D doesn’t have the previousSpriteState variable, you might need to use:

directionOfMotion = GetTree().GetCurrentScene().GetNode<Player>(“Player”).previousSpriteState

instead, since the Player class has the previousSpriteState variable defined.

This Reddit thread, seems to have useful information on accessing variables in Godot through C#, which might be helpful.

Side note: I have experience with Godot, but not really with the C# integration in Godot.

Hopefully this helps! :smile:

Hey, Thanks man. Replacing KinematicBody2D with Player solved the issue. You saved my Day!!! :-) :-)

@Anderoo99 said: Hey, Thanks man. Replacing KinematicBody2D with Player solved the issue. You saved my Day!!! :-) :-)

Great, I'm glad it is working now :smile: Happy to have helped!