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() {}
}
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
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})"); }