Skip to content

C sharp in Godot

  • Follow these instructions and don’t forget to enable auto-reload:
  • Pasted image 20231227101015.png
  • To set up debugging, follow these instructions:
    • NOTE (Fri 03/08/2024): perhaps this whole process is simplified with this plugin.
      • Also, I think you can just run “.NET: Generate Assets for Build and Debug” from VSCode to have it create launch.json and tasks.json for you.
    • Make sure you put those JSON files in a .vscode folder
    • If it says it can’t find the program (which refers to Godot), then make sure you set an environment variable in your shell file and that you reloaded it.
    • If you get an issue like No loader found for resource: res://NameOfScript.cs , it could be that the environment variable you set was for Godot rather than Godot_mono. Godot_mono is needed to be able to interpret C#.
  • To install packages with NuGet, you just need to run something like dotnet add package TwitchLib from the directory with your .csproj file.
  • To use secrets for your project (e.g. for any API/chat connections), do the following from your project directory:
    Terminal window
    dotnet user-secrets init
    dotnet user-secrets set fake-password hunter2
    • This puts your secret into ls ~/.microsoft/usersecrets/ with the UUID that printed from the init command.
    • To read the secrets, you’ll need to run dotnet add package Microsoft.Extensions.Configuration.UserSecrets and then do something like this from the code:
    using Microsoft.Extensions.Configuration;
    var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
    string username = config["username"];
    string password = config["password"];
  • See my C# notes for any general C# knowledge.
  • Prefer the C# collections where you can since they don’t require marshaling to C++ like the Godot collections do. See this for more information.
  • There is no way to Instantiate a scene and call the constructor of the script at the same time (reference). IMO, the next best thing is probably something like this to set private variables in a static factory method:
    public partial class Character : Sprite2D
    // Prevent construction of Character instances outside of the CreateCharacter method
    private Character() { }
    // Private setter means only this class can set the Town
    public Town Town { get; private set; } = null!;
    public static Character CreateCharacter(Town town)
    PackedScene scene = ResourceLoader.Load<PackedScene>("res://Character.tscn");
    Character c = scene.Instantiate<Character>();
    // Change anything here
    c.Town = town;
    return c;

VSCode doesn’t show errors/warnings

Section titled VSCode doesn’t show errors/warnings

There are a hundred possible reasons for this. In my particular case, it came down to a casing issue in a .sln file (reference).

Can’t set breakpoints while debugging

Section titled Can’t set breakpoints while debugging

If you click in the VSCode gutter while running and get a gray breakpoint, then it’s possible you’re hitting an issue where Godot has the wrong assembly_name in project.godot. For example, I had this folder structure:

  • Skeleseller
    • Game
      • project.godot

…and originally, project.godot had project/assembly_name="Skeleseller". When I changed it to Game, it worked.