Running C# inside C# with dotnet-script
February 26, 2023
Tagged: Programming C#
Since last year I’ve been thinking about how to handle tooltips in games. For Cantata, we have a procedural tooltip system. This means that, instead of hand creating every possible tooltip and toggling it on/off when the player mouses over something, we have no handcrafted tooltips and instead build them procedurally from components at runtime.
This has a ton of benefits, but one of the pain points has always been stuff like this:
Though we don’t have explicit Gameobject tooltips, each tooltip does still have its own script that is full of lots of conditional logic. Writing them procedurally is nice, but there are also downsides in the sense that changing a tooltip requires a full game recompile in order have changes updated. Wouldn’t it be nice to be able to author these external to the game and have their changes hot-loaded?
Along the way to solving the above (which I think I finally cracked, but more on that in a future post), I discovered something called dotnet-script.
dotnet-script basically lets you execute something very close to C# (.csx - “C# Script Files”) inside of C# as well as as standalone script files. It is exactly as cool as it sounds, and after a lot of research seems like the best solution for scripting in dotnet/Unity.
However, it also seems basically undiscovered, outside of a dedicated group of people that are aware of how cool it is. Searching on Google for results and blogs on it turns up only a few pages of results. So in the spirit of trying to help other people out and increase those search results, I wanted to put together a sort of “index” blog post that just captures everything useful I could find on dotnet-script. Enjoy.
Dotnet-script Repo + Readme Though the docs are lacking, the Readme on the repo provides a fairly comprehensive overview of things.
Filip W’s blog One of the main contributors of dotnet-script has a blog where he occasionally talks about new features it gets with each new release.
Dotnet-script Repo Tests The software seems pretty well tested, and the tests are broken up pretty nicely by feature here. It’s worth reading through the tests here to find something for your use case.
Looking through the dotnet-script tag on Github brought up a few repos. I combed through most of them to see if any were worth mentioning. Worth mentioning that besides some headers, it seems like .csx is largely the same as modern C#. Here’s the results:
These are some high-quality references I found for actual script files.
Assorted dev-related scripts: https://github.com/seesharper/dev-tools
Git-related scripts: https://github.com/daanflore/GitPreCommit
Dotnet build related scripts: https://github.com/seesharper/dotnet-build
Grab bag of other references found on github.
https://github.com/mrseanryan/test-doc
https://github.com/askingalot/github-issue-creator
https://github.com/jake-carpenter/clnbuilds
https://github.com/pcgsgroup/dotnet-script-utilities
https://github.com/andmos/dotnet-script-example
https://github.com/Alumniminium/linux-dotnet-scripts/tree/master/io
Other people exploring and talking about dotnet-script
https://www.hanselman.com/blog/c-and-net-core-scripting-with-the-dotnetscript-global-tool
https://galdin.dev/blog/csharp-scripts-using-dotnet-script/
https://www.elliotdenolf.com/blog/csharp-scripting-using-dotnet-script
https://ttu.github.io/dotnet-script/
https://iliasshaikh.medium.com/c-scripting-with-vscode-a-recipe-c672dd44d6
https://www.lightinject.net/blog-dotnet-steps/
https://blog.schroederspace.com/tumbleweed-technology/experimenting-with-dotnet-scripts https://kaylumah.nl/2019/09/07/using-csharp-code-your-git-hooks.html
That’s it for now! Hope this helps other people looking into using dotnet-script in their projects. As I mentioend above, I’ll have a post in the future about what I’m doing with it, so look forward to that!
Published on February 26, 2023.
Tagged: Programming C#