- Home /
Read txt into string array, recognize as commands with arguments
Hi,
I'm making a game with a friend, inspired by an old game called Colobot. In our game, you will have 2 teams, each with 10 tanks. These tanks can drive, turn, aim, shoot and scan for targets, but do nothing on their own, nor do they react to input.
The goal in the game is to write a (very) basic script in a .txt file, which could look something like this:
--
scan
ifFound
turnTo(target)
move(1)
fire
endif
--
a tank executing that simple script will do only one at a time. For example, after move(1) is a fire command. The tank would not fire untill the move(1) command is finished.
But there is one problem; this is the first time either of us have done anything with reading a text file, and recognizing commands(and their arguments).
So my questions are, how do i read seperate lines of a txt and put them in a string array, and, how do i recognize those as commands, with arguments?
the ifs might be a challenge too, but i'm mostly interested in the two points above, hoping i'll manage to figure the rest out myself.
(PS: i'm thinking of suggesting the name Codeception to my friend :D)
I would write this in a more structured way. Having the arguments in parentheses LOO$$anonymous$$S nice, but it's harder to parse. Just use spaces. You have to break the file down by lines and parse each line, probably by using a switch statement based on the first set of letters on the line (ie. for "move 1" you would match "move").
Thought about that too, and i decided i'd do either move(1) or move 1, whichever was easier. But i'm not sure how to get the individual lines in a text array, so i have to find out how to do that first :P
once i've done that, the idea is just to read the line, and if the string of text on that line is equal to "fire", for example, i just want to start a function that fires the cannon.
i understand from what i asked it might seem like i'm planning something really complex, but the above is all i want to start with, and from there i'll try to add whatever i can :P
The best way to extract text that satisfies certain conditions from a line of text is a regular expression. In C#/.NET, you use the Regex class, and for what you're doing you probably want to use a match collection. Regular expressions themselves are quite cryptic, unfortunately (and I don't have time to write an actual solution to your problem), but there's multiple webpages like this one to get you started: http://www.dotnetperls.com/regex-match
Answer by Drakestar · Jun 18, 2012 at 09:37 AM
I'm not sure this is a topic that can be sufficiently explained in a single Unity Answers post. Just reading a text file and breaking it into individual parts is easy (using the C# string functions or Regular Expressions), but you're also looking at various tasks that comprise (even simple) compiler design, like scanning, parsing, tokenization etc.
If you have iTunes University, you can search for "Introduction to Compiler Construction" from the University of Salzburg for an introduction to the problem. I'm sure there's other web resources along the same lines, as well. Alternatively, you might be able to find and use a scripting module/plugin for the Unity Engine - but if one exists I'm not aware of it.
added a comment at my original question, i think (but i might be wrong), that you thought i was making something much more complex as what i'm trying. I'm not sure how exacly i will identify the arguments yet, but i'm not trying to invent a new language (yet! :D).
so far, all i'm trying is to compare two strings, and if they mach up, start a function based on what was entered.
For example, on line 3, the player typed the line "move 5". i want to read that line, or the first word at least, and my script will compare it to all functions i have. Eventually, it will compare the input(move) to a string "move", and it will then start a function in my script something like this: void $$anonymous$$oveForwards() { targetpos = transform.forward * ; }
where arg is the argument in the txt, in this case 5. which i will have to detect somehow, and i'm not sure how yet, as i don't know how reading txt's work yet.
thanks anyway for your comment, i might try that once i finish my current education (GameDeveloper!)
ps: this comment is way longer than i originally intended.
Well...but you are inventing a new language. ;) BTW, just to clarify, it's not C# string functions and regular expressions, it's .NET/$$anonymous$$ono string functions etc.
@$$anonymous$$aster2112 you've got an if block in there to it's more than just a standard list of commands. I've written a basic scripting language for Unity, but I made it functionally based - so if was a function like it is in Excel or something. If you build stuff that way it is much easier. To build a real language with constructs like that you are going to be writing a lexer and a grammar processor - the easiest way to do that would be to find some .NET compatible Lex/YACC implementation and use that.
Your answer
![](https://koobas.hobune.stream/wayback/20220613072918im_/https://answers.unity.com/themes/thub/images/avi.jpg)