- Home /
Making an in-game command line. How do I get arguments?
I have a debug console that I use to do things in game rather than doing the continuous hastle of doing everything in code and in the editor. So far there is a simple command called "addItem" and then I have the commands "setHealth", "setHunger" etc.. But I dont want to make an if for each value. e.g 1, 2, 3, 4 etc..
EDIT: Make sure you look at 'ExecuteCommand' method, here lies my problem. I need to find arguments in the string 'inputText'. How do I go about doing that?
How do I check and read arguments? Here is my code:
using UnityEngine;
using System.Collections;
public class DebugConsole : MonoBehaviour {
public string COMMAND_addItem = "addItem";
public string inputText;
private string log;
public bool commandLine = false;
private bool enterPressed;
private bool allowCheats = false;
PlayerInventory playerInventory;
void Start () {
playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerInventory>();
log += "\nTip: Press ESCAPE to exit debug console";
}
void Update () {
if(Input.GetKeyDown ("`"))
commandLine = !commandLine;
if(Input.GetKeyDown (KeyCode.Escape) && commandLine)
commandLine = false;
}
void OnGUI () {
enterPressed = false;
if(commandLine){
Event e = Event.current;
if(e.type == EventType.KeyDown && e.keyCode == KeyCode.Return && GUI.GetNameOfFocusedControl() == "debugInput")
enterPressed = true;
if(e.type == EventType.KeyDown && e.keyCode == KeyCode.Escape && GUI.GetNameOfFocusedControl() == "debugInput"){
commandLine = false;
PauseMenu.paused = false;
}
GUI.SetNextControlName("debugInput");
inputText = GUI.TextField(new Rect(10, 25, Screen.width - 20, 25), inputText, 50);
GUI.Box (new Rect(10, 55, Screen.width - 20, 400), log);
if(enterPressed){
if(inputText == ""){
commandLine = false;
} else {
ExecuteCommand(inputText);
inputText = "";
}
}
}
}
void ExecuteCommand(string command){
//addItem
if(allowCheats){
if(command == "addItem 0"){
playerInventory.AddItem(0);
log += "\nAdding item ID 0 (FRUIT_APPLE) to your inventory";
} else if(command == "addItem 1"){
log += "\nCannot add item ID 1 to inventory. Item ID '1' is NULL";
} else if(command == "addItem 2"){
playerInventory.AddItem(2);
log += "\nAdding item ID 2 (FRUIT_ORANGE) to your inventory";
}
}
//allowCheats
if(command == "allowCheats true"){
allowCheats = true;
log += "\nCheats set to: true (Allowed)";
} else if (command == "allowCheats false"){
allowCheats = false;
log += "\nCheats set to: false (Disallowed)";
}
//setStatus
if(allowCheats){
if(command.StartsWith("setHealth")){
} else if(command.StartsWith("setHunger")){
} else if(command.StartsWith("setThirst")){
} else if(command.StartsWith("setStamina")){
}
} else {
log += "<color=red>Not allowed to use cheats!</color>";
}
}
}
Answer by Graham-Dunnett · Jul 29, 2014 at 09:19 PM
Usually you'd use string functions to split the input string at spaces. The first part of the string before the first space is a command, and then the substrings after spaces are arguments.
Your answer
![](https://koobas.hobune.stream/wayback/20220613155325im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
iOS Not Converting String to Integer 0 Answers
Iterating through multidimensional arrays 2 Answers
C# - parsed strings not changing in function 1 Answer
Regular Expressions in Unity 0 Answers