Parsing string as code (C#)
So I want to build this little code sandbox in Unity, which would allow me to teach students the basics of algorithmics and coding.
The idea would be for them to enter (very basic) code in a text box or something of the kind, and to observe the effects of their code onto objects present in a Unity scene. I'm pretty sure this has been done a million times, but I'd love to try my hand at this. The rub is, I have no idea where to start...
I guess the idea is that the string would be compiled into code & executed at runtime, at the press of a button.
I've read about numerous other questions on SO, and have come up with very diverse solutions such as using a C# parser, reflection, expression trees, CodeDom, etc.
From what I understood of all these (i.e., not much), CodeDom seemed more appropriate, but then I read that it only ran inside of Visual Studio and generated errors in public builds. So does that mean that this is going to be a problem within Unity3D (as it is based on Mono?)
Thank you for your help,
I've tried to do something like this once, but also hited a wall, my alternative solution was to check for patterns and parse the values, ins$$anonymous$$d of trying to compile the code:
Like, to change the character speed the pattern would have to be something like: parameter = value;
Then i would slipt the string and parse the value to float and assign to the character speed.
Yes, it probably has been done a million times. $$anonymous$$ostly by computer science juniors taking the "Languages" class. You write the Parser, turning the string into tokens in a tree (don't have to, but that's the easiest.) Running it as you go is what an Interpreter does: if(cmd==Cmd.Assign) ... . Stuff like that.
It requires some background, but their are lots of books about hand-writing parsers and interpreters. The end result is just a boring C# program that reads strings. Like finding the first and last name in "Smith, John", except longer.
With respect, I'm not sure why you'd use a runtime compiler inside a game to $$anonymous$$ch students how to code. Why not get them to write the actual code that is used to create the game?
I agree. If you worry that the students will be bored or overwhelmed then perhaps setup a little game and have them finish some bits of code to make it work like make the score counter work or something like that. If you try to setup something that will use the input of the student you have to be 100% sure that it never fails to give the student the correct result otherwise you will $$anonymous$$ch them something that is not right. I have seen many examples of sandbox code games and they are mostly dull or not a reflection of the real thing. It is like trying to $$anonymous$$ch a language by removing most of the letters in the alphabet and when students want to use those "forbidden" letters they get an error.
Answer by Artaani · Jan 21, 2017 at 03:44 PM
I researched this theme some time ago and found several solutions. I am still not sure which solution is better. But they are works.
The answer can be found in the Asset Store by simply searching for "C#"
The first solution is: https://www.assetstore.unity3d.com/en/#!/content/23510
I tested it and it works very well, but with one important flaw. It works in Unity editor but can't be build due some error about lack of some Mono library.
Also, you may notice that author of this asset said in the description that he is recommend to use alternative solution, which is located here: https://github.com/aeroson/mcs-ICodeCompiler
This link on GitHub contains Unity project with working solution which works well both in Unity editor and build. Also it contains detailed explanation about why it have some complications during build and how author solved this problem.
The problem is, that second solution (in my opinion) is much harder to understand and work with. Besides that, I can't figure out how to compile several scripts at once (not only one) and author of this solution is not very active on the forum.
Anyway, I recommend you to check both links and see how it made, also check various links provided by authors of this tools.
In addition, in Asset Store you may find even more tools. For example this: https://www.assetstore.unity3d.com/en/#!/content/23248
But I discovered it only now and haven't used it yet. Also it is not free.
But besides that, also you may found plenty of similar tools, not for C# but another languages.
For example:
Java: https://www.assetstore.unity3d.com/en/#!/content/2345
Lua: https://www.assetstore.unity3d.com/en/#!/content/30055 https://www.assetstore.unity3d.com/en/#!/content/391 https://www.assetstore.unity3d.com/en/#!/content/33776
Python: https://www.assetstore.unity3d.com/en/#!/content/645
I tested only Java, it works very well. But of course, C# will be better for education.
Your answer
Follow this Question
Related Questions
function based on return value 2 Answers
How to get the active/loaded Scene then turn the name of it into a string? [C#] 1 Answer
How to change a public location,how to get a public variable 1 Answer
Accesing specific int from inspector 2 Answers
Premature level loading. String loads level, instead of Return key. 2 Answers