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 Master2112 · Jun 18, 2012 at 04:29 AM · textreadtxt

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)

Comment
Add comment · Show 3
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
avatar image Mizuho · Jun 18, 2012 at 09:53 AM 0
Share

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").

avatar image Master2112 · Jun 19, 2012 at 09:23 AM 0
Share

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

avatar image Drakestar · Jun 21, 2012 at 04:01 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 3 · 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
avatar image Master2112 · Jun 19, 2012 at 09:30 AM 0
Share

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.

avatar image Eric5h5 · Jun 19, 2012 at 10:00 AM 1
Share

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.

avatar image whydoidoit · Jun 19, 2012 at 10:13 AM 0
Share

@$$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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I read read from and write to a text file? 2 Answers

Getting A Variable From a Outside Text File 1 Answer

DirectoryNotFoundException when running a build 1 Answer

How can I read data from a text file, putting a large amount of data into structures 2 Answers

Trying to download and show contents of text file 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