- Home /
unity allow player to script a object or compile and run a code in runtime
Hi!
I am working on a game in which the player needs to code a cube, now I have asked this question before but I didn't get a clear answer, I have tried many approaches such as: using lua moon sharp(but I couldn't figure out how to tell the object to goForward() for exp), compiling the script(C#) on the fly(but couldn't figure out what to do with the assembly, I don't even know what an assembly is). I have also tried codeDom, and I have a script that compiles string but I don't know what to do with it.
so can somebody tell what is the simplest way to let a player write his own code(C# or lua or python) and then compiling it and applying it to a game Object (for now lets assume that there are no error or security problems) .
BTW I have already made a program that allow the player to write his source code and save it, and I do know how to take the source code and turn it in to a string.
It would be great if you will give a clear and detailed answer. and if can someone explain to me what is dll it would be great too
thanks for the help
I do not want to be rude but you are trying to write a compiler without a basic program$$anonymous$$g knowledge. That's not the right way to do this. First, learn a bit more and then try to do such a hard task ;)
I have actually read every article about the topic and to I am asking you for a simple answer its because English is not my native language
you generally wouldn't want to allow a user to write ACTUAL code in your game for security and safety purposes. Also users would crash there own games. so i can assure Unity wouldn't have a formal option for it.
if you where really persistent and if you had a whole lot of time, you could take your users input and have your real code attempt to read the users string and try make scene of it to let them change some basic stuff about an object.
but you can't import/compile actual scripts on the fly.
Wrong, you can import actual scripts on the fly. This is the way to do it. It only does not work on iOS.
Answer by AurimasBlazulionis · Sep 13, 2016 at 06:08 AM
I have not tried any of this, but this blog post seems to explain how to do this really well. You will just need to rewrite the code so that it loads not from the URL, but from the file instead.
The only problem I see is compiling actual code. This looks like an excelent post.
Now what is dll? Windows calls it Aaplication Extension. Dll is a compiled library (any kind), which can be loaded to application to make use of. In dlls you can store scripts or some low level code. Usually, unity builds few dlls for all of your scripts, but you can add more at any time you want. This is very useful when you need to update your game so people would not need to redownload everything again or when you want to do things what unity engine can not provide by itself.
Note: dlls usually have to be compiled for each operating system separately.
I have seen that article before but, I missed something in there. And I found what was missing and I think I have just got clear answer, I will do some testing and I will let you know how it goes.
thanks you very much
and thanks about the explanation on the dll it was useful:)
so after a lot of testing and reading I couldn't figure out how to solve a problem with the script in the article(it delegates a static function only and transform can not be in a static function) but after thinking about the idea of C# compiling at run time again I came to the conclusion that it may not be the smartest idea letting the player use C#.
So after doing some research about moonsharp again I figure out how to run lua script but I can only run return value and I don't know how to tell lua to do things like forward() GetEnemyDistance() Turn(), is there a DLL for those thing? Do I need to make my own DLL?if so, how?
All you need is a value. As I am not familiar on lua script, you could try storing a returned value to a variable in c# and then in update loop check for changes.
On the other hand, in c# code, when you have a static function, you can also have a static variable pointing to a transform you want to edit. So it would be declared as public static Transform myTransform;
. Then, you just assign YourClassName.myTransform
to your object and now you can move it from script.
But have you seen this? It states to include everything you need. It says it allows to read and write lua variables, call .net functions etc. It is really worth checking out, the example scene. It is licensed under $$anonymous$$IT so you can do almost whatever you want with it.
Check TestBehaviour. It looks like you can call Debug.Log, GetComponent and much more.
However, it seems that the library does not work under linux so I could not test it.
Also, it allows for the full access to unity api so wouldn't it be just simplier to get back to c# compiling and figuring things out of static variables, because it looks like to be the easiest way for multiplatform (apart iOS, which is again bad). I guess it is a tough decision, the lua one might support all platforms even iOS, but you will have to compile the library yourself (I do not know how).
thanks a lot I will look in to your idea of calling value in an update loop. its a really good idea
and I will check out the link.
many thanks :)