Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Can you execute C# from single string in game?

Discussion in 'Scripting' started by grimofdoom, Feb 22, 2016.

  1. grimofdoom

    grimofdoom

    Joined:
    Sep 6, 2013
    Posts:
    168
    I want to know if it is possible to execute C# from a string in game (such as "save.health = 500" or "killDepth = -500" or "if (soulContainer.transform.childcount >= 0) {Debug.Log("Souls count: " + soulContainer.transform.childCound) " (I am pretty sure some errors would be thrown. but you should understand what I mean). I am trying to make a Command Console for in-game debugging and messing around, but doing case switch is a lot of work and errors (I currently got it 'ok' working but pretty sure things aren't as great as they could be). Currently, it looks like this jumbled mess , and being able to just execute C# from a string with out having to program every little thing would be a huge time saver

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class CommandManager : MonoBehaviour {
    7.  
    8.     private GameManager gm;
    9.  
    10.     void Start () {
    11.         if (GameObject.Find ("GameManager").GetComponent<GameManager> () == null) {
    12.             Debug.Log ("Game Manager Not Found");
    13.         } else {
    14.             gm = GameObject.Find ("GameManager").GetComponent<GameManager> ();
    15.         }
    16.     }
    17.  
    18.     void Update () {
    19.         CommandControl ();
    20.         CommandInput ();
    21.     }
    22.  
    23.     //CONTROLS TURNING ON/OFF THE COMMAND CONSOLE
    24.     public void CommandControl(){
    25.         if (Input.GetKeyUp (KeyCode.RightAlt) && gm.commandEnabled) {
    26.             if (!gm.commandOpen) {
    27.                 gm.commandOpen = true;
    28.                 gm.commandConsole.SetActive (true);
    29.                 gm.ShowMouse ();
    30.             } else if (gm.commandOpen){
    31.                 gm.commandOpen = false;
    32.                 gm.commandConsole.SetActive (false);
    33.                 gm.HideMouse ();
    34.             }
    35.         }
    36.     }
    37.  
    38.     //CONTROLS INPUT OF COMMANDS
    39.     public void CommandInput(){
    40.         if (gm.commandOpen) {
    41.             if (Input.GetKeyUp (KeyCode.Return) || Input.GetKeyUp (KeyCode.KeypadEnter)) {
    42.                 if (gm.commandInput.text.Length >= 1) {
    43.                     gm.commandString = gm.commandInput.text;
    44.                     gm.commandString.ToLower ();
    45.                     gm.commandSplit = gm.commandString.Split (' ');
    46.                     CheckCommand (gm.commandSplit);
    47.                     gm.commandInput.text = "";
    48.                 }
    49.             }
    50.         }
    51.     }
    52.     //EASIER CHANGE COMMAND RETURN TEST
    53.     public void ReturnCommand (string command){
    54.         gm.commandReturn.text = command;
    55.     }
    56.  
    57.  
    58.  
    59.     //ALL COMMANDS
    60.     public void CheckCommand(string[] command){
    61.         switch (command[0]) {
    62.         case "killplayer":
    63.             gm.KillPlayer ();
    64.             ReturnCommand ("Player Killed");
    65.             break;
    66.         case "quit":
    67.             ReturnCommand ("bye bye");
    68.             gm.QuitGame ();
    69.             break;
    70.         case "save":
    71.             ReturnCommand ("Game Saved");
    72.             SaveLoad.Save ();
    73.             break;
    74.         case "restart":
    75.             ReturnCommand ("Restarting");
    76.             SceneManager.LoadScene (SceneManager.GetActiveScene ().name);
    77.             break;
    78.         case "hidemouse":
    79.             gm.HideMouse ();
    80.             break;
    81.         case "showmouse":
    82.             gm.ShowMouse ();
    83.             break;
    84.         case "sethealth":
    85.             SetHealth (command [1]);
    86.             ReturnCommand ("Health set to" + command [1]);
    87.             break;
    88.         default :
    89.             ReturnCommand ("Unknown Command");
    90.             break;
    91.         }
    92.  
    93.         gm.commandInput.text = "";
    94.     }
    95.  
    96.     //==============COMMAND METHODS===========================
    97.  
    98.     //SETHEALTH COMMAND
    99.     void SetHealth(string health){
    100.         if (int.Parse (health) >= 1) {
    101.             gm.health = int.Parse(health);
    102.         }
    103.     }
    104.        
    105. }
    106.  
    (I don't know the "proper" way of producing a GameManager, but I am using mine for storing all the main variables and GameObject references for easier access without having to create a lot more variables than needed.)
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    Let's start with the easy answer: Use a singleton.
    Code (csharp):
    1. public class GameManager : MonoBehaviour {
    2. public static GameManager instance;
    3.  
    4. void Awake() {
    5. instance = this;
    6. }
    7. }
    8. // anywhere else
    9. GameManager.commandOpen = true;
    10.  
    As for the console, executing native C# code in the way you want ranges from "virtually impossible" to "actually impossible" depending on the platform you're deploying to. And on top of that, opens up a whole can of worms in terms o security and stability. Best to do it the hard way.