Skip to content

Source Generators

Valk edited this page Oct 4, 2024 · 3 revisions

Cat Lips Source Generators

For full explanations and a complete list of all of Cat Lips source generators, visit https://github.com/Cat-Lips/GodotSharp.SourceGenerators

OnInstantiate

Add the following to any node script.

[OnInstantiate]
private void Init() // optionally add params here
{

}

Then from anywhere do the following.

MyNode myNode = MyNode.Instantiate();
AddChild(myNode);

SceneTree

Add [SceneTree] attribute to the top of any class and you will be able to do some really cool things! Note that this only works for root scene nodes and the script must have the same name as the scene node and be right next to the scene node in the file system.

[SceneTree]
public partial class MyScene : Node2D 
{
    public override void _Ready() 
    {
        // You can access the node via '_' object.
        GD.Print(_.Node1.Node11.Node12.Node121);
        GD.Print(_.Node4.Node41.Node412);

        // You can also directly access nodes marked as having a unique name in the editor
        GD.Print(MyNodeWithUniqueName);
        GD.Print(_.Path.To.MyNodeWithUniqueName); // Long equivalent
    }
}

InputMap

From anywhere type InputActions.(...) to access both built-in and user defined actions defined in project.godot. For example InputActions.MoveLeft.

Switching Scenes

Important

You will need to restart your IDE if you want to see the new changes to the Prefab and Scene classes.

Any .tscn files placed in the following paths will be added to Prefab and Scene classes.

  • Prefab Resources:

    • Search Path: res://**/Prefabs/**/*.tscn
    • Associated Class: Prefab
  • Scene Resources:

    • Search Path: res://Scenes/**/*.tscn
    • Associated Class: Scene

Example Usage

Game.SwitchScene(Scene.UICredits);
Game.SwitchScene(Prefab.UIOptions);
Clone this wiki locally