Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Desination-X · Jul 29, 2014 at 09:18 PM · stringcommandparsingargumentsin-game

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>";
         }
     }
 
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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

Parsing an odd string concatenation to float 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges