- The game is built with C# and XNA and the script should work with this setup.
- The script should not be able to screw up the player's computer. I.e. no file access and such.
- The script should run fast enough to not hinder my 60 fps target.
- Hot-reloading the script would be nice. Not a 100% requirement, but it would help.
- Good support for debuggers.
Running C# scripts (i.e. just have .cs-files in a directory that are compiled on the fly when starting the game) sounded like the perfect solution at first. I like working in C#, and some quick testing revealed that the visual studio debugger can easilly attach to the script, the performance is great (equal to the rest of the code) and everything is peachy. Until the moment I realized that requirement #2 is violated, you can do anything with the scripts.
The solution I researched is to make a separate AppDomain, set minimal rights and run the script there. The problem is, the cross-domain performance is pretty awful. In my profiling tests, I get about 3500 calls to the script before 16 ms has passed.
So, I tried Lua. Using LuaInterface, I made the same kind of setup as before. My initial runs were positive, getting about 8x the performance of the C#/AppDomain scripts. Then I realized that I had to sandbox the lua calls too to make the tests fair, and after that the performance is much more evened out.
Stats when running 1000000 method calls, release build of course. Tests are:
- simple: just a call to a function that returns a string.
- noDomain: calling an identical function from a C# script. Most of the overhead is probably the reflection used to invoke the function.
- domain: doing the same thing as above, but this time the script is run in a separate appDomain. I.e. real test for C# script.
- lua: similar functionality for Lua instead.
- simple: 0,0025514
- noDomain: 0,6140648 (simple * 240,6776)
- domain: 4,819073 (simple * 1888,795)
- lua: 3,304015 (simple * 1294,981)
Question: Do these results seem reasonable? On average, how many script calls do you guys make per frame?