- Home /
Is it possible to make a code simulator?
Hey, so I was wondering how difficult it would be to implement a code editor that runs a real life language in the game. This is for a game mechanic that I’ve been refining and wondering how hard it would be to do this.
Answer by JackMini36 · May 17, 2018 at 10:04 AM
I made something similar a few years ago, but with a simplified language not a real one.
It is complicated to parse the code effectively and translate that into game logic yourself. You can go on and study a bit about code parsers and how they work and implement a sophisticated one youself.
What we did was just some text processing and finding certain patterns which made up the simplified language we came up with. Specific patterns or keywords where matched with specific game logic, so whenever a match was found we executed that logic based on the variables defined that followed those keywords/patterns.
If you don't necessarily need to use a real language, you can make up your own simplified version to fit your game mechanics. For example, our for loop was something like this:
// Iterate a specific number of times
for(10)
{
function();
}
// Iterate a specific number of times (stored in a variable)
for(iterations)
{
function();
}
This can be achieved by simple parsing and text processing. However it gets way more complicated with nested fors, ifs and so on. I remember it being a pain in the ass to make.
EDIT: This is the demo we made. I don't still have working demo since it is not mantained http://www.jackhadjicosti.com/project/3/Coducation
Answer by troopy28 · May 17, 2018 at 06:09 PM
Yes! It is!
What you would have to do is called "hosting". And it is exactly what Unity is doing with your C# scripts: they are hosted by their C++ codebase through Mono, and are called (and managed) from this C++ code. Now, as you are already using C#, you wouldn't have to suffer the process of embedding a language from C++, which is not always that funny.
I would recommend you to host a C# script engine, as you are using C# Unity scripts, because there are a lot of ways to do this, and it would allow you to have a great interactivity between your C# host (Unity scripts) and the C# hosted scripts. I suggest you to have a look at CS-Script, which seems to be great, and more importantly, read about Microsoft's Roslyn managed compiler, which allows you to do a lot of things about C# hosting (and other .NET languages if I'm not wrong).
Now if you want a more "scripty" language, like Python or Lua, well, there are libraries dedicated to this, like IronPython for Python hosting, or MoonSharp for Lua (which seems to be truly compatible with Unity, see here).
But I recommend you to go for C#. It would be really something easy to do.
Good luck on the path!
Your answer
![](https://koobas.hobune.stream/wayback/20220612162841im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Detect Text in GUI; Print 1 Answer
Unity editor GUI, how to prevent "passthrough"/"clickthrough" etc? 0 Answers
GUI text font change 3 Answers
Limit on GUI Components? 0 Answers
Display the set up key for a button 2 Answers