Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by Ari-Bouaniche · Jan 21, 2017 at 01:40 PM · c#stringparsing

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,

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image EDevJogos · Jan 21, 2017 at 02:08 PM 0
Share

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.

avatar image Owen-Reynolds EDevJogos · Jan 21, 2017 at 03:53 PM 0
Share

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.

avatar image tanoshimi · Jan 21, 2017 at 02:11 PM 2
Share

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?

avatar image Pengocat tanoshimi · Jan 21, 2017 at 02:42 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

277 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges