Ingame Scripting

A huge amount of the time I spent on GMod Sandbox servers was spent just coding things like holographic cars, engine controllers, holographic games, walking robots, raycasters etc. “Live Coding” like that was massively fun to me and really furthered my interest in programming and CS.

There are several addons in GMod that allow the creation of scripts that control a contraption’s behaviour. Examples would be Expression 2, Starfall, zCPU etc. But the custom languages like Expression 2 and zCPU were just slow.

Would it be possible to use C# directly as an ingame scripting language so that normal clients on a server can write some control code for their contraptions?

2 Likes

I think everything will be possible. And then if it’s feasible on Gmod then it will be possibly feasible on S&Box.

1 Like

There is no in game IDE rn but because of hot reloading that’s present it is absolutely possible that you could use an IDE using C# and probably a very good idea.

2 Likes

Someone will probably do this. I’ve spent thousands of hours playing with Expression 2 (+E2P) / Starfall / Lemongate / etc., and playing with code like this - was the best part of sandbox and gmod in general for me.

2 Likes

I know that hot reloading is a feature of S&Box, but I’m talking about coding on a server which I’m not the owner of. This is why I’m curious about whether clients will be able to use C# directly.
There’d have to be another sandbox for each client so that not everyone can for example kick() everyone else.

2 Likes

Some kind of permission checks should be enough. Just like E2P has its “E2Power access” and allows you to edit other player’s entities, for example.

1 Like

C# is a general purpose language so anything you can do in LUA you can do with it too. You can do much more on the server side since you can turn off sandboxing and push code to the client.

Someone already started making their own scripting language for s&box 2 years ago… (I dont remember who tho)

I know that hot reloading is a feature of S&Box, but I’m talking about coding on a server which I’m not the owner of. This is why I’m curious about whether clients will be able to use C# directly.

That would be ridiculous. It should only work if the servers provides you something like that.

2 Likes

There’s a bit of a difference between how Lua and C# work.

It’s easy to transpile a custom language like E2 into Lua or just write lines of plain Lua at any time and run it directly (because RunString allows executing arbitrary pieces of code on the current Lua state).

But with C#, taking into account that the client and the server work with a compiled assembly, it wouldn’t be so trivial. I think it would be easy to create a custom language that operates in a specially designed VM, gets compiled/interpreted and executed through C# but I doubt it would be easy (if possible) to directly access C# state live from the game like that like we can in gmod.

UPD:
In a nutshell:
Expression 2-like scripting - yes, easy.
Luapad-like direct C# usage involving the active C# state - I’m not sure.

1 Like

Wouldn’t that be possible with Reflection?

2 Likes

Reflection is used for other things.

1 Like

Just embed Roslyn or use their own compiler API to have ingame scripting, should be no problem if you turn off sandboxing on the server. Then send the assembly to client like other addons. Might be even possible to compile on clientside if they expose the compiler API.

2 Likes

This is pretty much going to be your only way to directly use C# with your script. You could look at embedding Moonsharp if you wanted to stick with Lua? Cannot tell you how fast it is but it exists? You at that point could write C# code and expose it to Lua and they could script using this. Either way, though you are going to have to deal with passing the code (and its dependencies mind you) Across client and server and replicating it. I do not think it will be an easy task either way.

1 Like

You missed my main point: of course it is possible to compile/run ANY language from C# (even C++ can be transpiled to JS using emscripten), but it’ll run slower (it’s a C# state inside of a C# state) and you’ll still need to create some API to access game objects and systems from that embedded state, which makes this contained C# environment nothing different from any other language (be it C++ or Expression 2 or Lua or whatever).

1 Like

but it’ll run slower

C# is not interpreted. This is wrong.

1 Like

I never said it is.

1 Like

Interpreted languages would run slower, this is not the case for compiled languages.

Quite sure the statement of player-made C# code within C# being slow is related to the overhead of the API and libraries used to provide that. The same would apply to any language you decide regardless if it’s interpreted or not.

1 Like

While C# is compiled, it it still compiled into bytecode which gets executed in a VM, it’s not like C++ which is compiled into native machine code.

Having a VM inside of a VM will be slower than only one layer of virtualization, and yeah, as @WYVERN said, overhead of the wrapping API will also make a difference but probably not so significant.

1 Like

This is all wrong. Please do not share wrong information here.

While C# is compiled, it it still compiled into bytecode which gets executed in a VM, it’s not like C++ which is compiled into native machine code.

C# gets JIT compiled to machine code at runtime. There is no difference in execution to C++ or any other compiled language.

Having a VM inside of a VM will be slower than only one layer of virtualization, and yeah, as @WYVERN said, overhead of the wrapping API will also make a difference but probably not so significant.

Having a VM inside of a VM will be slower than only one layer of virtualization

There is no virtualization here. Virtual machine refers to having its own bytecode and instruction set. Not virtualization. There is not any kind of virtualization. Like said the “virtual machine” here does not refer to virtual machines like VMWare or VirtualBox. Also all code would run in the same runtime. You are not having a runtime inside a runtime.

1 Like

We are probably talking about different things.

The topic is about in-game scripting brought in form of an addon, the author brought examples like E2 and Starfall, which are exactly that.

From what I can imagine, S&Box C# API won’t be rich enough in low-level APIs to write a proper runtime for a language (be it C# or something else) for it to run efficiently using optimizations like jit, which allows for near-native experience.

Of course Facepunch can specifically come up with a solution that allows to compile, send and run pieces of C# code while in game, but I think this topic is more about what any one of us, as a S&Box addon developer, could design to run other languages (including but not limited to C#) using S&Box C# API to interact with the engine.

And to me it looks like the only solution to this would be C# runtime written in C# (and probably none of existing ones will work for S&Box because many of the needed low-level features won’t be whitelisted in S&Box C# API for security reasons), so it’d be a rather limited subset of C# which runs slower which to me kinda kills the point of using C# as such a scripting language in the first place.

Also, you edited your post so many times trying to make it clear what is a virtual machine and what is virtualization and so on, don’t try so hard, I know what is what, I didn’t mean system virtualization when I said virtualization, I used that term to describe having VMs running C# bytecode inside one another, that has nothing to do with VMWare or VirtualBox.

1 Like