Change Node2D child property

Godot Version

4.2.2

Question

I have a C# tool script in a Node2D which exports a Vector2 and will change a RayCast2D child node’s TargetPosition property.
The Export annotation works but the values inputted reset as soon as focus is lost on the field. I followed this document to make the script.

Any help would be appreciated!
Tool script:

using Godot;
using System;

[Tool]
public partial class Teleporter : Node2D {

	[Export]
	public Vector2 targetPosition = new Vector2( 0, 0 );

	[Export]
	public PackedScene targetScene;

	private Vector2 _rayCastTargetPosition = new Vector2( 0, 30 );

	[Export]
	public Vector2 rayCastTargetPositionInput {
		get => _rayCastTargetPosition;
		set {
			_rayCastTargetPosition = rayCastTargetPositionInput;
		}
	}
	
	// Called when the node enters the scene tree for the first time.
	public override void _Ready() {
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta) {
		if ( Engine.IsEditorHint() ) {
			RayCast2D playerDetectionRayCast = GetNode<RayCast2D>( "RayCast2D" );
			playerDetectionRayCast.TargetPosition = rayCastTargetPositionInput;
		} else {

		}
	}

	private void Interact() {

	}

	private void DetectPlayer() {}

}

you know what you doing here?

_rayCastTargetPosition = get => _rayCastTargetPosition;

try

_rayCastTargetPosition  = value;

It’s defining the Get and Set of the variable since the _rayCastTargetPosition is a private value. I used rayCastTargetPositionInput to set the value of the private variable.

There are 2 distinct rayCastTargetPosition variables

ok then in this what is rayCastTargetPositionInput? if not _rayCastTargetPosition

		set {
			_rayCastTargetPosition = rayCastTargetPositionInput;
		}

Blockquote * The value keyword is used to define the value the set or init accessor is assigning.

It’s a concept of encapsulation of Object Oriented Programming. Read this if you want to know more.

And I was in the wrong, fixed by doing this:

playerDetectionRayCast.TargetPosition = _rayCastTargetPosition;

Then explain output of this:

internal class Program
{
    private static void Main(string[] args)
    {
        Node node = new();
        node.Print();
        node.rayCastTargetPositionInput = new Vector2(15, 14);
        node.Print();


    }
}
public struct Vector2(float x, float y)
{
    public float x = x;
    public float y = y;
}

public class Node
{
    private Vector2 _rayCastTargetPosition = new(0, 30);
    public Vector2 rayCastTargetPositionInput
    {
        get => _rayCastTargetPosition;
        set
        {
            _rayCastTargetPosition = rayCastTargetPositionInput;
        }
    }
    public void Print() { Console.WriteLine($"Node({rayCastTargetPositionInput.x},{rayCastTargetPositionInput.y})"); }
}

obraz

Fixed

internal class Program
{
    private static void Main(string[] args)
    {
        Node node = new();
        node.Print();
        node.rayCastTargetPositionInput = new Vector2(15, 14);
        node.Print();


    }
}
public struct Vector2(float x, float y)
{
    public float x = x;
    public float y = y;
}

public class Node
{
    private Vector2 _rayCastTargetPosition = new(0, 30);
    public Vector2 rayCastTargetPositionInput
    {
        get => _rayCastTargetPosition;
        set => _rayCastTargetPosition = value;
    }
    public void Print() { Console.WriteLine($"Node({rayCastTargetPositionInput.x},{rayCastTargetPositionInput.y})"); }

Output
obraz

1 Like

Yeah, I did not know that “value” needed to be called there, I used other source as reference for making the script.
Thanks

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.