Runtime Script Compilation

Hi,

A friend of mine has been trying to convince me to consider Unity for my development for some time now. In the past it hasn’t been an option, but I do have a project coming up next year which could possibly use Unity.

The one sticking point is that the game will require runtime script compilation, and ideally using Lua.

Now I see that Unity offers Javascript, Python and C# scripting, but my friend suggested that it might be possible to bind Unity functions to lua scripts through the C++ plugin architecture. Could someone confirm that this is possible and how I would go about it? I had a look at the examples and I could see how I would go about setting up a Lua VM, but I couldn’t see how I could get access to Unity “objects” through the C++ plugin, and hence through the Lua scripts.

Now if your answer to the above question is positive, then I don’t need an answer to the second question. I know how to compile my scripts at runtime with a Lua VM. Let’s assume, however, that your answer to the first question was negative. The chances of me persuading my employer to accept Python are slim, but let’s also assume I can do that. Can the python scripts be compiled at runtime?

Essentially, what I need to be able to do is develop the game, essentially providing a rough draft, if you like, where all the gameplay is controlled by scripts, all of which are distributed as source code, not compiled. I then need to be able to pass the whole game off to other developers, and have them run the game, edit the scripts and through the scripts change gameplay elements.

I should indicate at this point that web and IPhone deployment should not be a consideration here. I know that you’re obviously going to disable runtime scripting for web deployment because you would not be able to sandbox the application, and therefore you would never be able to get security certificates for your plugin. And the IPhone more than likely has similar considerations. I get all that. I’m talking purely downloadable deployment on Mac and Windows.

Unity has no python

It has .NET and uses C#, with additional support for boo (python like) and a javascript like language.
But all of them are compiled at build time, no support for runtime compilation through the mono .net compiler.

don’t know thought how much you could bind to dynamically loaded scripts but would be interested to know that myself.

This is incorrect–you have runtime compilation through use of eval() when using Javascript. I don’t know if that’s possible with C# or Boo, but I don’t think so.

I suppose there might possibly be some convoluted way of using Lua through plugins or something, but honestly I expect that would be far more work than it’s worth, assuming it actually is possible at all. You’d be much better off just using Javascript and eval() for runtime compilation.

–Eric

Thanks for the replies. My gut feeling is that you’re right about Lua, too much trouble by far making that work.

I’m intrigued by Eval() though. I did a forum search to learn more about it and the first few threads I found turned up people reporting that Eval() only works in the IDE, not outside of it. Can someone confirm that Eval() works in a deployed project and not just in the IDE?

Yes, it’s been fixed since then, so it works anywhere as far as I know (definitely in stand-alones, haven’t tried webplayers).

–Eric

boo supports this, there’s Boo.Lang.Compiler lib that allows assembly generation, then you link it into your game using System.Reflection

You just replied to a stone age thread…