- Home /
Help with a string format input
Hi! I am making a game which uses kind of a string format (I don't know how to call this) to get the player's keyboard input and execute an action based on that string format.
I'll explain it with an example:
We are in a MMORPG world (think WoW) and we can use the keyboard to chat and to execute commands. These commands are made up of multiple strings put together to make something to happen.
For example "hit the enemy with 2 points of power". This command is set up with 3 variables (something like hitattack (string), enemytarget (string) and powerpoints (int) but the command makes use of the variables turned into strings, but if you modify the string, the variable value will change too, so if you type those 3 elements, something will happen.
I'll try with another example as I know this is hard to explain (sorry guys):
You need to combine multiple variables turned into strings (so if you have a float 0.6f it will turn into a string "0.6" and so on...) to execute a command line, which depending on the values you set to a float, it will turn the float value into a string, but it will make use of the float value.
Sorry if I make this harder, if you guys still don't understand me, I'll try with more examples. Thank you sooooo much! You are awesome!
As a design question, you may be better served posting this on Unity forums...or another program$$anonymous$$g forum since there is nothing Unity in this question. Parsing text like this can be fairly simple or very complex depending on the allowed language and syntax. For example, what if the user typed "Apply 2 point of power to hitting the enemy?" Okay? Not okay? What about 'two' ins$$anonymous$$d of '2'? What about misspellings? What are the restrictions to your language? Being specific about scope and rules will really help you get a good answer.
What scripting language to you prefer? This should be relatively easy to do using GUI.TextField and the C# String.Split method as long as each of the variables that the string must assign the value of are separated by a specific character (e.g. each is one word, and they are separated by spaces, so the command in your example would be more like "hit enemy 2" ins$$anonymous$$d of "hit the enemy with 2 points of power").
Yeah, I know it. But I only need the basic idea to develop this, as I have been testing but I can't find it :/ Thanks anyway, I'll keep trying till someone helps me ^^
Edit: I use C# and I think it could be either easy or difficult depending on the way I make it, but I only need some basic info
If you confine your language to three things, an action, an object, and an amount, this can be fairly easy. First create an array of strings for each action. It would contain things like 'hit', 'lift', 'bribe', 'break' 'cast', 'chop', etc. Then a list of objects like 'enemy', 'door', 'wizard', 'troll', etc. When the user submits a command, look in the string to see if any action words exist. You can cycle through the array of actions and use 'string.Contains()' on the submitted string to check. If you find one, you have your action. If none exist, then the string is invalid. Do the same thing with the list of objects. Then find the first occurrence of a digit and parse out a value. This is a very flexible solution since the user can input the string in a variety of ways, but it also will parse out valid commands from nonsense or ambiguous strings.
Do you mean an AI editor? I've never played WoW but have seen one in a couple of other games, just googled it and came up with this.
If that's the kind of thing that you're after then as robertu proposed just implement an array of known strings and have the user either type or choose them from multiple drop-down lists.
Your answer
![](https://koobas.hobune.stream/wayback/20220613135823im_/https://answers.unity.com/themes/thub/images/avi.jpg)