- Home /
Allowing users to import their own UnityScript/JavaScript into the game at run time
I want to make a game where players can hack to beat the game, it's possible to beat fairly but its absurdly hard. I want players to be able to write their own UnityScript/JavaScript and import it into the game and attach it to any gameObject they want. I know I can let them write some code in a text field and then use eval() to run the code, but that's not what I want, I want them to be able to attach said script to a gameObject in run time. Is there anyway to do this? They don't need to write it in run time, simply so that they can import the .js file and have the game recognize it as a script. If so how would I do that?
Answer by rutter · Jul 02, 2014 at 07:34 PM
Short answer: that's theoretically possible, but so difficult that it might as well be impossible.
Unity's editor ships with compiler tools, but the built player does not. You would need to embed a compiler and/or interpreter with your game, just to get the code to a point where it could process new code at runtime.
How will the user input code? You may need an elaborate UI system.
How will the code be compiled? You may need an elaborate compiler/interpreter. You may need to devise your own programming language. You may need to make sure that the user isn't able to perform dangerous operations (like deleting or corrupting files, including the game itself and other files on their system).
How will the code be run? Will you bind to a DLL that's compiled while the game is running? That sort of binding is only available in Unity Pro. Will you run a separate application, and connect to it via sockets and RPCs? Then you'd need to put together a framework by which your applications can communicate.
No the user will write the code any way they want as long as its compatible with unity (free) ( they can test this with the free version of unity, the users who don't know how to program can just download solutions/code provided by other people). I will just be importing the .js file, if the code is bad the game should crash because its failed code. As for compiling I thought JS, doesn't need compiling... That's the reason web browsers use it. Also what about the eval() function, how does that work, it can't be compiling the JS code, isn't it just running it?
How does the eval() function work then in unityScript? It can't be compiling the code can it? I thought it just runs it on the spot. And for now I just want the user to be able to import .js files and attach them to a gameObject, that's all that matters for now.