Best runtime scripting language for unity?

I would like to run small script programs at run time in unity. For things like experimenting.

Things like:

for(x=1;x<100;x++){
   AddNewMonsterAt(x*10,0,0);
}

And various things like that. So what do you think is the best scripting language to link into Unity that will let me call some of my c# functions at run time. And also have things like for loops and so on.

1 Like

You have two basic paths available to you.

C# itself is the first path. Choosing C# will allow you the most performance because the code will be compiled prior to execution (through a Just-in-Time compiler), but this can potentially allow modders to create malicious scripts and this path is not compatible with AOT platforms like iOS.

https://assetstore.unity.com/packages/tools/integration/dynamic-c-82084

Your second path is to choose an interpreted language which wonā€™t have the risk of malicious code and will run on AOT platforms but will be significantly slower. There are multiple entries on the Asset Store but the ones linked below have either been kept up-to-date or have positive recent reviews.

https://assetstore.unity.com/packages/tools/integration/miniscript-87926
https://assetstore.unity.com/packages/tools/moonsharp-33776

3 Likes

Why does being interpreted prevent malicious usage?

Lua is fairly easy to implement. I sometimes use the same package what @TonyLi put in the Dialogue System. (And Iā€™m currently not in the front of my Unity-computer so I canā€™t link it)
It of course depends how much performance you need. It can be slow if you use it for serious update-loop things.

Not automatically, but if itā€™s not a DLL, you can prevent it to call into your system. And of course you design the entire API what is published to the scripting language.

I wouldnā€™t call it protection against ā€˜malicious codeā€™, because with DLLs and C# you can inject whatever you want at any time. But it protects against user mistakes.

2 Likes

Like @ mentioned itā€™s more the ā€œautomaticallyā€ part but I didnā€™t think about that at the time. Interpreted is much more easy to filter out the possibility of a mod being malicious because you can modify the interpreter to not allow certain instructions while a compiled approach makes it much more difficult to intercept malicious instructions.

Okay, Iā€™m home, so I believe our Lua solution came from here: Lua Interpreter - CodeProject

The version in the Dialogue System has a lot of fixes. Iā€™d push them to a public repository, but the original author didnā€™t set one up. If anyone would like a copy of the updated LuaInterpreter, let me know and Iā€™ll make a unitypackage. There are other free Lua packages on the Asset Store, too, such as MoonSharp, which Ryiah linked above. And thereā€™s also @JoeStrout 's Miniscript, also linked above.

4 Likes

Paging @JoeStrout . MiniScript seems like a good fit here.

If you tightly control what commands your interpreted language has, you can reduce the possibility that someone writes malicious code. For example if you have access to full C#, you can start doing random stuff to the file system. If you only have access to an internal MoveCharacter command, you canā€™t do much damage to the userā€™s system.

1 Like

Agreed. :slight_smile: If youā€™re interested, the forum thread is here. Iā€™d be delighted to talk to you about your runtime scripting needs (including why, after writing three chapters of a book about Lua, I scrapped it and started designing MiniScript instead).

2 Likes

Also, you can make lua work in sandbox mode, which only allows access defined commands by user, and basics math.
But you can make it as well having access to system files.
And there are few more modes, which allows less, or more permitted access. For example you can get system time, but you can not read / write files.
Mentioned moonsharp has such options.

I used it not so long ago in sand box mode.
I wanted only custom predefined methods, to be executed.
Therefore, you can not do anything malicious via lua, for example like reflections, or attaching dll etc.

Well you made me curious at leastā€¦

I have just started to use Lua becasue Z-Wave uses it. It must be the most backward language in existing. I mean, this is how you foreach a array in that language.

local devices  = {19}
switchedOnDevices  = {}

for i, device in ipairs(devices) do
   if luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", device) == "1" then
      table.insert(switchedOnDevices, device)
      luup.call_action("urn:upnp-org:serviceId:SwitchPower1", "SetTarget", {newTargetValue = "0"}, device)     
   end
end

or that static table.insert to mimic List behaviour. Interesting

1 Like

Just for interested in programming languages family
lua is roughly at 1 oā€™clock, next to PHP.
C is at 10-11oā€™clock
C++ at 8-9oā€™clock
C# at 3oā€™clock

1 Like

Yes, youā€™ve pretty much got it. Lua looks attractive at first, but when you start to see the ugly, you realize it goes very deep. All variables global by default, arrays much harder to use than they should be, etc. I know it does the job it was intended to do, but I found I just couldnā€™t stomach it after a while.

MiniScript was designed from the beginning to have a clean, straightforward syntax, drawing the best features from modern languages like Python, C#, and VB.NET.

Itā€™s interesting to ponder where it would fall in the language tree above. ā€¦But now that I look at it, Iā€™m not sure that diagram is very useful. It has Visual Basic and VB.NET at opposite sides of the tree, even though the latter is derived directly from the former and has almost the same syntax. It shows Xojo most closely related to Delphi, even though Xojo is also based on Visual Basic and has nothing at all in common with Pascal. It shows C, C++, and ObjC having nothing (!) in common, though in reality both C++ and ObjC were derived directly from C. Go figure. ĀÆ_(惄)_/ĀÆ

1 Like

Yep there may be few discrepancies. Perhaps would be needed more research to find most accurate graph.
There are some overlaps and cross shares across languages too, as far I am aware. Which perhaps is hard to picture on radial graph. I know there is many graphs, but If anyone has better, what more important more reliable graph representation, it would be nice to share it.

The best graph of the computer languages I saw in my life is in the Computer History Museum in Mountain View, California: Programming language heritage chart | It all starts with Forā€¦ | Flickr and PJB - Home

Looks interesting at glance, but unreadable (too small) :frowning:

I know, you will have to visit it. :smile: They have ENIAC too and Enigma and Babbage too! And many more.

2 Likes

Miniscript looks really nice from what Iā€™ve seen. Nice bits from python and visual basic.

BTW In case anyone wants to implement yet another programming language here is one I came up with. See if you can guess what it does:

1..10 : x=>{
    1..10 : y=>{
            x*y >> " " : print;
    }
    newline;
}

I might be popular with the German audience because the verbs are at the end! (Not so good for intellisense though!) One could also have an ā€œifā€ rule as condition ? action and a ā€œwhileā€ rule as condition # action. Else might be ~?

I think a class would just be implemented as exceptions. Just set something equal to a base class (default being Object) and add exceptions/additions such as:

Dog = Animal @ {
    sound = "woof";
    legs = 4;
}

(This also works if you think of it as a union between Animal class and an abstract class.)
Then you would say dog = new Dog; sound dog : print. (As in sound of dog). OK, now Iā€™ve got carried away!

2 Likes

I made an interactive BASIC runtime (derived from Microsoft Small Basic) that can be embedded in Unity games:

https://github.com/wixette/isb

Could be an alternative choice of Miniscript.

2 Likes