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 Jesse_Pixelsmith · May 20, 2013 at 02:40 PM · texttextasset

Reading in level data from a text file

So I currently have code setup to read the following from a text file and populate a 2D array:

 ####
 #@.#
 #$ #
 #  #
 # $#
 #. #
 #  #
 ####

Now I'm looking to store multiple levels in the same text file, and want to know how I might read in the desired level, as well as some other information in a header.

In this case if I select a GUI Button "Level 2" - I'd like it to load the appropriate level and tell my matrix how many rows/columns to build (mapMatrix = new char[rows,cols];).

I can format the header in whatever way to make the process easier. Thanks!

 Level 1, Rows 8, Columns 4:
 ####
 #@.#
 #$ #
 #  #
 # $#
 #. #
 #  #
 ####
 
 Level 2, Rows 8, Columns 5:
 #####
 #  .#
 # $ #
 ##@##
 ##$##
 #   #
 #.  #
 #####
 
 Level 3, Rows 5, Columns 7:
 #######
 #. # .#
 # $@$ #
 #  #  #
 #######
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
1
Best Answer

Answer by AVividLight · May 20, 2013 at 03:17 PM

Hey Jesse Alexander,

There are two different ways you could go here, both are good, it's just up to your preferences.

The first way would be to add a special character at the end of each level's information set, and read until you hit that, then add the information to an array, and keep going. The text file would look something like this:

 Level 1, Rows 8, Columns 4:
 ####
 #@.#
 #$ #
 #  #
 # $#
 #. #
 #  #
 ####
 Ω
  
 Level 2, Rows 8, Columns 5:
 #####
 #  .#
 # $ #
 ##@##
 ##$##
 #   #
 #.  #
 #####
 Ω
  
 Level 3, Rows 5, Columns 7:
 #######
 #. # .#
 # $@$ #
 #  #  #
 #######
 Ω

The second would be to make your text file into an XML file.

 <?xml version="1.0" encoding="UTF-8"?>
 
 <Levels>
     <Level Number="1" Rows="8" Columns="4">
         <Design>
         ####
         #@.#
         #$ #
         #  #
         # $#
         #. #
         #  #
         ####
         </Design>
     </Level>
     <Level Number="2" Rows="8" Columns="5">
         <Design>
         #####
         #  .#
         # $ #
         ##@##
         ##$##
         #   #
         #.  #
         #####
         </Design>
     </Level>
     <Level Number="3" Rows="5" Columns="7">
         <Design>
         #######
         #. # .#
         # $@$ #
         #  #  #
         #######
         </Design>
     </Level>
 </Levels>

And, finally, to read the information depends on which way you choose. If you use a text file, I'd search the second line in each search, and populate three ints with the only three numbers you'll find in it. That is to say, search the second line for numbers, and pass those to your variables.

I hope this helps! If I didn't explain something well, please let me know! -Gibson

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 Jesse_Pixelsmith · May 20, 2013 at 09:18 PM 0
Share

Thanks Gibson, this helps with some of my issue. I would only be loading one level at once, so reading the array until I hit Omega and then stopping makes sense.

However I'm not exactly sure how you would just search the "second line in each search" as you say. Presumably I would be scanning the text file for "Level X", and then reading from there.

In fact I might not even need Omega, as the Rows value should tell me how many lines I should go down. Speaking of which though - once I find "Level X" what would be a good way for me to pull the Row and Columns Int values? Just looking for the syntax, this stuff is a bit new to me.

Regardless, thanks for the direction!

avatar image Jesse_Pixelsmith · May 20, 2013 at 09:19 PM 0
Share

So yeah if it wasn't implied, I think I will be going with the text file method.

avatar image AVividLight · May 21, 2013 at 05:17 AM 0
Share

I'm glad to have helped!

By "search the second line", I mean read the numbers from the second line. You know the level information is on the second line (you'd need a line at the very top to keep this in order), so search the second line for numbers, and pass those to your variables.

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

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Finding the height of a text asset. 1 Answer

Basic Question about TextAssets 0 Answers

Is updating 1 character in a 200 character Text object more expensive than a 10 character Object 1 Answer

when loading a textasset from .txt file, the quotations disappear?? 1 Answer

How does a TextAsset work? 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