You have a game you are developing using Godot, and there is some functionality isn’t included in the core engine. That is normal– game engines cannot provide all the functionality you might need, considering how diverse and different every game can be.
This is where using a library comes into play, and considering that Godot supports C# natively and .NET has a vast ecosystem of libraries, also known as packages (see Nuget.org to explore).
Steps & Demo Project
Here are the steps, and a video, to walk you through adding a library to your Godot 4.2 project.
Here is a demo project if you want to see something working. (LINK)
1. Find a Library
Might seem obvious, but before you worry about adding one, find one that provides the functionality you are looking for. For example, in the demo project (linked above) I import and use YamlDotNet because Godot does not have native support for parsing and deserializing YAML files.
While you’re looking for a library, you should check for compatibility.
2. Check for Compatibility
There are a couple of things to keep your eyes open for with respect to Godot 4.2 compatibility. Check for the following on the README.md or Nuget library pages while picking your library:
- .NET 7 or 8 Compatibility
- NativeAOT Support
NativeAOT is beyond the scope of this article, but in short, it is a new way of packaging up .NET application and Godot 4.2 relies on this packaging method to make its modern .NET compatibility.
If the library doesn’t explicitly say that it doesn’t support NativeAOT and does support .NET 7 or 8, then you’re probably good to go. We’ll confirm it later in step 4.
3. Add the Library to your Project
From the terminal, cd
into your project and use the dotnet CLI that comes with installing the .NET SDK with the following command:
dotnet add package <package-name-goes-here>
For example, in the demo I use:
dotnet add package YamlDotNet
This downloads the YamlDotNet package and adds it to the .csproj
file in the Godot project with these lines:
<ItemGroup>
<PackageReference Include="YamlDotNet" Version="15.1.2" />
</ItemGroup>
For the full details on the dotnet add package
, check out the .NET CLI documentation (LINK).
Can I add a locally downloaded library?
Yes, although you probably shouldn’t.
I asked a bunch .NET of experts about this, and it usually causes problems for you further down the road when it comes to building your project on build servers rather than your local device.
Plus, there are a lot of other ways to go about this rather than adding a locally hosted DLL to your project. For example, if you wrote the library yourself, reference the project rather than the package. If you really want to, you can even host your own Nuget feed where you can privately expose the library for your own projects.
In short, can you do it? Yes. Should you do it? Probably not.
4. Use the Library
At this point, you have the library in your project. It’s up to you to use it. In the demo project, I added some using statements and deserialized the YAML file in the project in Main.cs
.
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
// use the Library
var deserializer = new DeserializerBuilder()
.WithNamingConvention(PascalCaseNamingConvention.Instance)
.Build();
5. Build it
This is the part where if things are incompatible, the compiler will give you errors. Run dotnet build
from within the project folder, or click the “build” button that is at the top of the Godot window.
And with that, you’ve added a library to your Godot 4 project! Well done!
Leave a Reply