0
\$\begingroup\$

I'm trying to dynamically create a KinematicBody2D, attach a script to it, then attach it to the tree. However, none of the methods inside the added script seem to be triggered.

var kinematic = new KinematicBody2D();
var sprite = new Sprite();
var texture = (Texture)GD.Load("res://assets/characters/healer/healer_red.png");
sprite.SetTexture(texture);
kinematic.AddChild(sprite);
kinematic.SetPickable(true);
kinematic.SetGlobalPosition(new Vector2(10,10));
kinematic.Name = "Healer_red_1";
var script = (CSharpScript)GD.Load("res://TempHandler.cs");

kinematic.SetScript(script);

GetTree().GetRoot().GetNode("Node").GetNode("Sprites").AddChild(kinematic);

var addedNode = GetTree().GetRoot().GetNode("Node").GetNode("Sprites").GetNode("Healer_red_1");
GD.Print(addedNode.GetScript());

When running the game, no error are shown and the last GD.print yields [CSharpScript:1079].

Here is TempHandler.cs

using Godot;

namespace BoomCraft
{
    public class TempHandler : KinematicBody2D
    {
        public override void _Ready()
        {
            GD.Print("Ready temp !");
        }

        public override void _EnterTree()
        {
            GD.Print("Entered tree");
        }

        public override void _PhysicsProcess(float delta)
        {
            GD.Print("Process");
        }
    }
}

The output never mentions any of these methods. Is there something I'm missing when adding the script to the node ?

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.