Hi all!
How can I load/compile scripts at runtime?
Basically I want the user to be able to modify the game scripts without having the project’s source files (eg. for creating custom game mods such as new weapons, NPCs or dialogue options). In the engine I used before Unity I could just use ‘include xyz’ and the script loaded/compiled the given file at this point.
Thanks,
Yhoko
you would use the .NET compiler + reflection to use them.
Include etc are compile time things and don’t exist in the build.
One thing in question though that I would recommend to check before going further is what you are allowed to exposed to scripting (I would guess exposing unity functions is not allowed, just your own game functionality, but I’ve not checked the license on that matter). This restriction is often present to ensure that users don’t use the engine to create their own creation technology but only to make games moddable within the games environment.
In Javascript you can use eval(). This would also allow you to narrow the focus to specific functions rather than entire scripts, so you could do something like:
private var fireFunctionText : String;
function Update () {
if (Input.GetButtonDown("Fire1"))
Fire();
}
function Fire () {
eval(fireFunctionText);
}
You would load the fireFunctionText with the usual .net IO functions or WWW, and then it would execute as code.
There are a few limitations with doing that…one, code in eval can’t access private variables, so any variables referenced in the Fire function would have to be public (you can still use @HideInInspector if you want to keep the inspector tidy). This is probably a good thing, since you can use this limitation to prevent imported code from having too much access to stuff you don’t want it to touch.
Also, there is a delay when the code is first evaluated, since it has to be compiled on the fly. Therefore it’s probably a good idea to run the code once at startup, so it can be used during the game without freezing it temporarily. You could do this by using a boolean called “evalInitialize” or something, and have all your eval code functions start with “if (evalInitialize) return;”. So you’d have evalInitialize start out as true, eval all your imported text, then change it to false. That way the functions won’t actually do anything when initialized, but the code will be compiled and ready to use later.
–Eric
Thanks to both of you! I already knew about JS/eval but I never heard of .NET reflection. How exactly does this work / where can I find a simple example or tutorial?
Google for Reflection C# and you will find many articles likely 
Also you can naturally check out the whole documentation on Reflection on the MSDN with the examples there
I -of course- did that before asking. What I need is help in googling an example that more or less explains what I need to know. All I found were examples on how to retrieve certain values from assemblies or so (eg. http://www.codeguru.com/csharp/csharp/cs_misc/reflection/article.php/c4257 ). I can’t yet see how this would allow me to include mods at runtime.
Including such mods will be a lot of work for you. There is no magic to just “drop dll in and it will work” if that was what you were hoping for.
You will work a lot with Method and Type related reflection aspects so you can Invoke methods on the reflected objects etc.
It a pretty advanced topic and I’m sure that you will have to plan the feature for a while to get it into a state where it becomes usable.
For indepth mods you can likely easily plan the next 2-4 months to refactor your current code to be capable to redirect its calls to such mods etc