C sharp in Godot
Setting up
Section titled Setting up- Follow these instructions and don’t forget to enable auto-reload:
- 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
andtasks.json
for you.
- Also, I think you can just run “.NET: Generate Assets for Build and Debug” from VSCode to have it create
- 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#.
- NOTE (Fri 03/08/2024): perhaps this whole process is simplified with this plugin.
- 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:
- This puts your secret into
ls ~/.microsoft/usersecrets/
with the UUID that printed from theinit
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:
- This puts your secret into
- For hot reloading from VSCode on macOS, simply enable
csharp.experimental.debug.hotReload
(which comes from C# Dev Kit) and make sure you have a .NET SDK > 8.0.100 (e.g. 8.0.303 works). Then, debug through VSCode and it’ll reload changes automatically.
Basics
Section titled Basics- See my C# notes for any general C# knowledge.
- Predefined colors exist in
Colors
(plural), notColor
, e.g.Colors.White
. - 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.
- Rather than use
System.Timers.Timer
, which will require deferred calls due to threading, just use a Godot timer (AddChild(new Timer())
).- If you don’t want to add a
Timer
to the scene tree, use this code (await ToSignal(GetTree().CreateTimer(1.0f), SceneTreeTimer.SignalName.Timeout);
)
- If you don’t want to add a
- 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:
Troubleshooting
Section titled TroubleshootingVSCode doesn’t show errors/warnings
Section titled VSCode doesn’t show errors/warningsThere 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 debuggingIf 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.
An exported build doesn’t work
Section titled An exported build doesn’t workPerhaps your assembly name doesn’t match your solution/project names (reference)?