- Home /
unity load an player made C# script
Hi all
I am working on a game in which the player write his own C# script and apply them to an objects. And in one scene the player write his code and game saves it as X.cs, if I run the game in the editor it saves the scripts in assets and if I run the game in a PC build it doesn't work probably because the script isn't included in the assests folder. short story: When I run the game in the editor the game works perfectly OK but on the build it doesn't run the script.
here is the script that add the player script in to the object:
public bool once = false;
public string script;
public InputField loadScript;
// Use this for initialization
void Start() {
}
// Update is called once per frame
void Update () {
script = loadScript.text;
if (Input.GetKeyDown(KeyCode.LeftControl) == true) {
if (once == false) {
System.Type MyScriptType = System.Type.GetType(script + ",Assembly-CSharp");
gameObject.AddComponent(MyScriptType);
once = true;
}
}
}
}
is it even possible to run an external C# script like this if not how can I replace it?
I have read about lua scripting(moonsharp, lua interface, Nlua) but i don't know how to work with it.
thanks for the help.
Answer by thor348k · Aug 26, 2016 at 06:36 PM
C# is compiled into IL. The IL is then converted JIT (just in time), into the native assembly language of the host machine.
You might be able to write a. NET runtime that is interpreted (instead of compiled) to IL instead.
I'm not entirely sure how'd you go about this. You might actually just have to write an interpreter "in-game", and not use the C# compiler.
It would be easier to almost just let the player write in Python, or Lua, and interpret line by line, like Python's Idle.
Any compiler questions, just let me know. It's a hobby of mine.
Here's a good link on .NET and C#: https://msdn.microsoft.com/library/z1zx9t92 Best of luck :)
Hi thanks for the help but I have found some thing called moonsharp which allow the usage of lua scripts my new plane is that the player will load a lua script. The problem is that I have no idea on how to use lua script or how to load them so any idea? thanks again
It would be beneficial to learn Lua, definitely. The $$anonymous$$oonsharp site has a Getting Started section, and great tutorial it seems like. I'd start there.
If you'd like to learn Lua in relation to game development, LOVE development platform. @Vice_Versa mentioned the Resources in Unity, for saving to, and loading from, for the scripts. I'm not aware of how $$anonymous$$oonsharp handles things, but this might work.
If there isn't support for that in $$anonymous$$oonsharp, I'd use C# directly. I prefer the support, and power of having full control over what is saved, how it's saved, and where. Here is a good tutorial on $$anonymous$$SDN.
Good luck!
yes I have found a guide in youtube on how to use moonsharp and I think i know how to load a script but I don't know to tell lua commands like transform.Translate(Vector3.forward) or deltaTime or any other commands in unity but I will give it a shoot and if it will go well I will let you know. and again thank you very much your tips are very useful!
ok I think that I just made lua script(using moonsharp) to run
the debugger console tells me the script runs but it doesn't appear like it but the script is a = 5 print (5) does it even show it on unity?
At this point, I'd imagine you'd have to write your own interpreter, for the Lua to "translate" into Unity. You'll have to parse the user code, and handle it appropriately. Here is a good blog on doing so. If this is the case, you might as well not even use Lua, and just let the user write C# - if you're going through the trouble of "translating" their code.
But I may be wrong. All I know is that a long while a go, I was working on a little side project that taught the player how to program in a Final Fantasy-like RPG. I only got to parsing one line at a time (that's all I let them do at that point), and I needed to write my own system of handling tokens, lexers, etc. It didn't take too long, but there is a bit of a learning curve.
For an example, they would type, "attack.$$anonymous$$agic("Wolf", fire);" and it would make the player shoot a fire ball at the wolf.
Answer by Vice_Versa · Aug 27, 2016 at 03:47 AM
I dont know if this is still relevent, but i know that in older versions of unity (i think it was 5.1) you could make a folder called Resources inside of the Assets folder. In your game you can call a function called Resources.Load("script name") and it would allow you to add scripts to objects in a build. that sounds like its exactly what youre looking for
After I have written this question I did new research and I have found a file called Strea$$anonymous$$gAssets and the idea of the file is that it stays the same after the build but there is a small problem with that because I can't add scripts from strea$$anonymous$$gAssets. but thanks for the tip!
Your answer
Follow this Question
Related Questions
My script behave different when I compile, idk why, everything was recorded 0 Answers
Why is this variable not setting in build? 0 Answers
Visual Studio Code Intellisense 0 Answers
"Only assignment, call, increment, decrement and new object expressions can be used as statements" 1 Answer
(C# and Java)Unable to give the Damage from enemy bullet script to Player Health. 3 Answers