Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Turning String Input into Executable Code

Discussion in 'Scripting' started by Nigerx, Dec 27, 2020.

  1. Nigerx

    Nigerx

    Joined:
    Dec 27, 2020
    Posts:
    1
    Hello. I am a newcomer to Unity and a newcomer to C# as well. Recently I have been trying to maximize players' freedom in my game. To do that, I hope to find a way through which I am able to let the user enter the code as string, and then execute the string at runtime.

    I think a perfect example that demonstrates my need is an online graphing calculator like Desmos, where the user can kind of enter the equation entirely by themselves then the graph will be generated based on that. Is there a way I can do things like that? If not directly, then is there an alternative approach I can use to make something like the Desmos graphing calculator?

    To make it more clear, I am wondering if I can generate the graph through something like:

    String userInput = "...(a function in terms of x)";
    for (i++) {
    int x = i;
    int y = userInput;
    generate point with coordinate (x,y)
    }

    Sorry for any misused terms as I am new. Nonetheless any ideas or tips are appreciated.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Turning the string into executable code is both overkill and really dangerous if all you really need is a calculator or mathematical expression parser.

    Look up some tutorials for writing a calculator in C#. There are many. That will put you on the right path.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Look into MoonSharp LUA as well if you want a general way to configure already-built Unity programs.

    But beware that in Apple and Android there are explicit callouts in the EULAs saying you can only do this if it doesn't fundamentally change the purpose of your original program's functionality.
     
  4. Ardenian

    Ardenian

    Joined:
    Dec 7, 2016
    Posts:
    313
    As PraetorBlue points out, creating code is usually both dangerous and overkill for most projects. An answer to your question highly depends on your needs for your project.

    If you absolutly want to allow actual code, take a look at Creating a function dynamically at run-time, especially the answer by TcKs. In this context, Microsoft's article Expression Trees might also be interesting.

    A classic approach to dynamic "code" is using functional elements that the user can piece/link together at runtime to create complex functional elements. A game that does this is Autonauts, for instance. Such block-.based coding gives you full control over what you want to allow and what you don't. want to allow. This quickly goes into the direction of Visual Scripting. A game that heavily features this kind of modding is Starcraft 2 with its official modding tool, the Galaxy Editor. It is surprising how creative you can get with such a powerful modding tool to create custom games within the same engine and game.