Search Unity

Allow user-player scripting?

Discussion in 'Scripting' started by djnewty, Dec 16, 2015.

  1. djnewty

    djnewty

    Joined:
    Apr 7, 2015
    Posts:
    40
    Hi,

    I'm wondering whether there is a way to allow a (cut down) version of scripting within an app?

    In other words to be able to attach scripts to certain objects and determine some values based on if/else statements?
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    You want the players of the game to be able to write scripts in the game?

    That's very possible. The easiest way would be to compile their scripts on the fly, using the CSharpCodeProvider class. That being said, that's probably not available on every platform - mobile platforms in particular usually doesn't allow you to compile code at runtime (for security reasons).

    In that case, you'll either want to write/find some kind of parsing tool that interprets the player's code, or send the code to a server, compile it there, and send the result back.
     
    peterroth124 likes this.
  3. djnewty

    djnewty

    Joined:
    Apr 7, 2015
    Posts:
    40
    ahh that makes sense. parsing the code is probably the best method - as I'm hoping to do mobile as well.

    Thanks v much Baste - hope you have a great day!
     
  4. djnewty

    djnewty

    Joined:
    Apr 7, 2015
    Posts:
    40
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    It's free, so it should be easy to check.

    Download the package. Then, open your build menu (file->build setting). Select the mobile platform you're interested in (ie. iOS), and click the "Switch Platform" button. If that causes compilation errors to happen, it means that it's using libraries or function calls that are not available on that platform.

    Lua is a popular scripting language for games, and it's made to be fairly easy to use, so it's a good choice.
     
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    You can also use a pure C# implementation of Lua and drop the source code into your project so you don't need to use separate DLL plugins: UniLua, MoonSharp, LuaInterpreter, etc.

    @JoeStrout is also working on a simpler scripting language called MiniScript that looks like it'll be really useful for applications like this.
     
    Kiwasi and JoeStrout like this.
  7. StudioEvil

    StudioEvil

    Joined:
    Aug 28, 2013
    Posts:
    66
    We are currently using UniLua (https://github.com/xebecnan/UniLua) but we have some problems related to the use of generics by this library (random crashes, mostly).
    @TonyLi are you using UniLua without problems?
     
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    @StudioEvil, if you get frustrated with the Lua solutions, and want to give MiniScript a try, please let me know. I'm looking for beta testers to stress-test it, and it sounds like your app would certainly do that! (MiniScript will eventually be a non-free asset in the Asset Store, but of course it'll be free to intrepid early adopters.)
     
    StudioEvil likes this.
  9. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    No, sorry, I've used NLua, KopiLua, and LuaInterpreter, and to keep things as simple as possible for end users I don't do much with generics.

    Speaking of simple, MiniScript has a simpler and more forgiving syntax than Lua, although your modders may already be familiar with Lua since it's an established modding language. If there are any issues with generics in MiniScript, you have the attention of the developer in the post above this one. :)
     
    JoeStrout likes this.