Solutions to implement real time code in Unity

I am looking for a way to implement common code (C#, JS, Python, C++) into Unity at runtime.

The idea is to ask the player to solve a puzzle that involve code; kinda like these games where you have to hack doors or vaults :slight_smile: But instead than write a simple “fill in the missing part”, I would like to expose a decent interface that allow someone to actually

  • create variables and functions, and run them
  • have a separate environment so they can’t mess up with the game or with the computer itself
  • get a 1:1 replica of what would it be, if they were running the code outside the game.

I did try to code my own parser, but takes forever and it will never be 1:1 to the real world. On top of that, I need to implement something that already exist, so it is not really efficient at all.

Is there any open source/official/paid way to do this? I can go for the simplified version that I wrote, but I would love to offer a realistic experience instead.

at least there’s
lua, GitHub - moonsharp-devs/moonsharp: An interpreter for the Lua language, written entirely in C# for the .NET, Mono, Xamarin and Unity3D platforms, including handy remote debugger facilities.
also this custom one MiniScript: lightweight scripting language for your game

not sure about how to get 1:1 replica, if you are going to call your own interface/api to access unity gameobjects though?

2 Likes

Thanks mgear; so LUA is fundamentally the only one that can be used in Unity?
My objective is not much to have them interact with the interface to manipulate game objects; it is more like “solve the equation” kinda of approach. So given a specific piece of code, the final result should be a specific value, and the parser will elaborate the code written by the user, to figure out if that solution is acceptable or not, considering the constrains of the language used.

Kinda like to open the door you need to move 4 bytes from a registry to another, using a buffer as transition point. This can be done in few ways, so that’s easy to implement. But if you ask to solve a sorting, there are many algorithms that can be used; and as such, you want to cover all the cases. This is why I can’t write my own parser, without oversimplify the whole activity, or spend years just to code something that is similar enough to a real parser for a language.

I had a picture in my mind, like creating on the fly a virtualenv, and have the commands run in that limited environment; so it won’t affect anything, and the only thing going out is the output of the function written. Even if I can cover the basics (variables, functions, constants, to be able to write basic algorithms), that would be enough to start with.

So what’s wrong with MiniScript? (The second link @mgear posted above.)

I designed it for exactly this sort of thing (and, in part, as a reaction to learning the deep horrors of Lua).

The problem is simple: the language used in your Miniscript is neither C#, JS, C++, Python, BASH or Java :slight_smile:

If I need a syntax-precise command in one language, that will be different from a syntax-precise command in another language; as expected.

Fundamentally I don’t need a scripting language to drive the application objects; I need a 1:1 parser that takes commands in one of the languages mentioned earlier, and execute them in a protect area of the application. You can call it a terminal emulator or a virtual env and so on; but fundamentally need to parse code written in one language, validate it and execute it.

Your solution is nice, but it is not validating code written in other languages, beside the one you implemented (unless I read wrong on the description).

No, you’re right, it’s its own language. Not any of those you mentioned.

I don’t fully understand why you need it to be one of those languages. (I mean, BASH? Seriously? :wink: ) But that’s OK, I trust you do.

Unfortunately I’m not sure what to suggest in that case. It’s possible to run C# code dynamically, at least on the desktop (I don’t think it works on iOS). But doing so opens you up to all sorts of hacks, no matter how much you try to lock things down. I don’t know any way to run such code in an airtight sandbox.

yeah… should it run on all platforms or pc only?

for pc you have good selection of interpreters, python and others.

and probably you could even have some small c++ compiler included in your folder, you pass the code to it,
it would output to file and you check the output, but of course would need some proper regex filters to avoid users building something dangerous…(which might be difficult?)

maybe could even run the code on some cloud server, i’d think linux has better options for easy sandboxing?
(or at least it wouldnt break the users machine in any case)

*or something like this,

Yeah, actually running the code online isn’t a bad idea. I have a handful of apps on my phone that work that way, allowing you to dink around with Python, JavaScript, etc… as long as you have a network connection.

Yep, that was the issue; your solution is great to add scripting, but sadly I need the whole language implementation for the ones I did mention.

Bash is cool :wink: Imagine if you play something like uplink with a real BASH environment :wink:
The languages are used so someone that knows how to program in the “real world”, can get more fun out of the game. For a car simulator enthusiast, the best part is to have a car that is reacting like a real one, so he/she can use real world notions to drive the simulated car. In my case, knowing a programming language does augment the experience of playing a game, where you actually write real code while in game. It may be niche, but it is not like I am going to sell it anyway :slight_smile:

take a look at roslyn then. (google ‘github roslyn’)

thats the codename for the actual c# compiler. (well, there is mono with its own compiler as well, but its not as widely used).

There’s no easy way to sandbox this though. You’d have to do your own syntax analysis and binding steps which will be a ton of work, but is possible).

It can compile code at runtime (but it is pretty slow, on the order of tens of milliseconds, so you definitely won’t get anything like ‘compile every frame’ if you plan to run at 60fps)

Maybe that could work for you?
As for the 1:1 output, yeah, thats exactly what the c# compiler does, except that it also has a neat feature of being able to compile stuff and give you classes / delegates you can use at runtime.

Preventing “messing up” will likely be the hardest thing here because there’s ton of ways to do stuff in c#.

Here’s a hint to prevent you from going into the wrong direction from the get go:
Lets assume you’re trying to be “smart” and are preventing file and registry access etc…
And all you give the user to interact with is your “class MySafelyDesignedClass”.

Then imagine what’d happen here:
typeof(MySafelyDesignedClass).Assembly.GetTypes().Find(t => t.Name == "FileInfo").GetMethod("Delete") ...
And there are other ways that don’t even rely on reflection like that…

edit: (i realize the above code isn’t exactly right, FileInfo is defined somewhere else, but the point still stands, you can easily load arbitrary stuff that way too :P)

Just windows would be fine; OSX at most, but it is not really a thing.

I see, so you would say that it is easier to send out the code via network, and run it maybe on a remote virtual box? I know cloud9 that is a 100% web based compiler and editor…I used it while I was playing with the Beaglebone.
It would be great to have something like hackerank; they support a ton of languages, and I am really curious to know how do they do that :slight_smile:

codinggame is similar too, they list some tech

Nice, never heard of codinggame.com.

So at this point, the solutions are very limited: either I create a web service that collect scripts, and send them to the appropriate compiler, or I write something customized for each language…I never thought it would be so complicate to write a terminal simulator that would be mimicking a computer. Thanks for the hints!