Sandbox Logo
08 September 2017

Dev Blog 2 - C#

How we're using C# in Sandbox.
Garry Newman
Programmer
UK
Coding in c/c++ isn't enjoyable for us. Unreal Engine doesn't make that any more enjoyable. So the goals are simple
  • Load new code at runtime
  • Hotload code while playing
  • Easy to learn and use
  • Reduce coding ballaches
  • End user never touches c++, Unreal Engine or any other SDK
Please keep in mind that I haven't cleaned any of this code up, we're still prototyping so nothing is fixed yet. The implementation is very straight forward. There's a World class, a Gamemode class, and Entity classes. We're remaining very Source engine'y with our terminoligy because that's what we know. Here's some example code.
This should be quite easy to read. It's from a weapon that shoots a rocket. It's creating a rocket entity, setting the position and velocity and spawning it.
You may have noiticed in that last bit of code the [Server] attribute on the function. That means it should always be run on the server. It can be called by the client that owns the weapon. How does the client call it?
Just like a normal function. Sandbox's engine knows you're a client and this function needs to be called on the server, so sends an network message to the server to call it. These network functions can have multiple arguments, so you can basically use them like normal functions. They work from server to client too.
You can make Multicast functions, these are for when you want to call the function on all clients.
If you want a variable in your entity to be synconrised with clients, just add a [Replicate] attribute.
This works with more than just plain old data.
We have async functions. They can run asyncronously.
Which prints out (check the time stamp)
They run in the game thread, which limit the amount of ways you can mess up with them. We're not limited to the game thread though. If you think something is thread safe, you can totally use threads too.
I want to end by emphasising that this is all at runtime. UE has things to make developing easier like Blueprints or MonoUE. But you use this things in conjunction with UE's editor. You edit them, you compile and cook and ship. Then everything is in a format you can't edit. While this is optimal it makes it all quite modder unfriendly. Sandbox is anti-cooking. Old school. No SDK, no editor downloads, no pak files. Just straight up editing loose files on disk, pressing save and seeing the changes in game.
Or not.