So I have a situation where I want some code that know for a fact has been coded many many time before. Rather than reinvent the wheel (and the differential gear and the axle and the inflatable tyre and the road) I would like to find an existing implementation I can use. What I need is a script interpreter to run inside my game, so the user can write their own ai scripts at runtime. I have searched the forums and while there are some ideas here, they do not seem to suit me. The lua plugin only runs on unity pro and I have basic for the moment. The jurassic thread also looked interesting but assuming it runs on basic (not tested) there are still issues with both of these implementations:
Security. I want something where I can give the user sandbox access to a limited amount of functions predefined by me. So they can call getNearestEnemy() for example and get a transform, and they can set currentTarget, but they can’t simply execute arbitrary code in the engine.
They do too much. These scripting engines are way too complicated and bloated, and I am not a good enough programmer to keep an overview of security concerns with such a large codebase. I basically only need the parser to handle for() and if(), to be able to read and write a limited number of variables (eg. 10), and to be able to call a list of predefined functions.
I know I could code this myself but it is a lot of work* and like I said, reinventing the wheel. I know I am not the only one who could use something like this and I imagine there may even be someone who has already done it. If so, can I have your code?
*To put ‘a lot’ in context here, the rest of the game is very simple and has taken me a few days to put together. I know in comparison to creating a full 3d game this is not that much work but I am one person and I am trying to do simple projects that I can make in a week or two.
There most likelly is something like that, check the asset store and spend some $$$. Don’t expect such a bulk of code to be handed out free, though – becasue it is a lot of work it is the precise reason that people are reluctant to share such things.
This lua interpreter is way less ‘lite’ than I had in mind. It would take me many hours to read through it and get the hang of how to use it and what it can do. There are no manuals or tutorials to speed this up. At this point it is still a coin toss as to whether it is faster to integrate this into my project, or simply to write a simple parser of my own. I may still end up using it, I am sick today so my brain is full of cotton wool and I am not going to try it right now. This thread is not answered imo. Oh and zeth, I am broke, so that’s not the best solution either. If you can hook me up with a job that pays in unity store credit I will reconsider.
***edit: removed stupid question and replaced with the above
I discussed this a bit in the last post of the thread. There are two layers - the Lua interpreter, and LuaInterface. LuaInterface is a library that adds a lot of interoperability with .NET - it lets you load assemblies, create objects, etc, and also makes it a lot easier for your C# code to pass objects to the Lua code, and have the Lua code able to use the objects in a natural way. It’s this library that gives the scripts more power than you want, but it’s totally optional. If you construct your Lua interpreter through LuaInterface then it will be able to do all this stuff (and you can interact with it from C# using LuaInterface too). Otherwise, construct your Lua interpreter directly, in the way LuaInterface does it under the hood, and it will only be able to access what you offer it.
Doing this involves using the standard C Lua API, which is not much fun… but you don’t need to interact with it too much. You can create an interpreter as follows, also demonstrating how to execute a string of Lua code - which could define a function, or set a variable, or it could be a whole block of user code, or it could be a call to the function. You can do a lot with just this one function, even if there are better, more direct ways to do these things.
luaState = LuaDLL.luaL_newstate();
LuaDLL.luaL_dostring(luaState, "x = 5"); // set global variable 'x' to 5
LuaDLL.luaL_dostring(luaState, "obj.method()"); // call a method of an object (maybe something the user defined?)
Each interpreter instance is a totally separate sandbox, and the user code can’t access things you don’t provide to it.
I’m not good with Lua, either at writing in the language or using the interop API, but as far as I know, this library operates almost exactly how the standard C one does. Hence the awful API, including the legacy separation into a “static” library and a “DLL”…
Man I took so long to type the edit I not only deleted the post you were replying to, but also asked a new question you were already helping with. I apologise, as I said I am sick and not thinking straight today. Hence doing periferal research rather than coding proper.
I think if you found a Lua advocate and asked them, they’d tell you how to use Lua for whatever it is you need. Not sure if that helps though. I’m not really a Lua advocate, but I do think you’d probably be better off using it rather than making up your own language.