Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Bojaniko · Dec 01, 2017 at 06:28 PM · arraylistmapinformation

[C#] Creating and Importing an Array from a separate file

I'm making a 2D strategy game and I want to make a modular level system. What I did was create a script that has an array which has 2 mandatory values: rows and columns that represents the map tiles and then I multiply those two numbers so I know how many tiles I can give a value to.
And I can then set each value separately so for example 0 would be nothing, 1 is a soldier, 2 is a building... And this works fine but I don't want to have to edit each levels content in that script, rather I'd like to create a file in which you have a list of numbers and then in the array in my script the values would be set to the list of numbers I made in the separate file.
How would I do this?

Thanks in advance :)

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ShadyProductions · Dec 01, 2017 at 10:41 PM

I would just suggest going for some simple System.IO.File

setup a text file with your grid example:

 1 1 1 1 1 
 2 1 2 2 1 
 1 3 2 1 1 
 1 2 1 1 1 

and then just write a method that adds it into the array based on your 0 dimension and 1 dimension length. You can use File.ReadAllText Method

Quick example (untested):

     public int[,] GetGridByTextFile(string path, int gridSizeX, int gridSizeY)
     {
         // Get all characters in textfile except whitespace
         var lines = System.IO.File.ReadAllText(path).Replace(" ", "").ToCharArray();

         // Quick check
         if (lines.Length < gridSizeX * gridSizeY)
             throw new Exception("Grid is not correct in the textfile based on the method input.");

         // Setup map grid
         int[,] map = new int[gridSizeX, gridSizeY];

         for (int x=0; x < gridSizeX; x++)
         {
             for (int y=0; y < gridSizeY; y++)
             {
                 // Apply grid data
                 map[x, y] = int.Parse(lines[x].ToString());
             }
         }

         return map;
     }

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 Bojaniko · Dec 02, 2017 at 05:06 PM 0
Share

Okay so I have a grid background that spawns and each one is presented by a number so I don't have to apply most of your code but what I needed I used and here is what I came up with:
In the update I call the GetGridByTextFile like this: for (int k = 0; k < 10*10; k++) GetGridByTextFile (Application.dataPath + "/LevelData/level0.ps", k);
The 10x10 just represents the rows and columns for now I just made it like this for testing. In the GetGridByTextFile method I did this:

 void GetGridByTextFile (string path, int index) {
     numbers = System.IO.File.ReadAllText(path).Replace(" ", "").ToCharArray();
     for (int i = 0; i < numbers.Length; i++)
         lvl [i] = int.Parse (numbers [i].ToString ());
 }


This seems to me like it should work but I get an error saying:

FormatException: Input string was not in the correct format System.Int32.Parse (System.String s) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Int32.cs:629)


Also some more errors that basically with this above say that there is a problem in parsing the ints. What is going on here?

avatar image ShadyProductions Bojaniko · Dec 02, 2017 at 10:48 PM 0
Share

Debug what you parse, see if there are no extra whitespaces etc You'll have to filter that out.

avatar image Bojaniko · Dec 03, 2017 at 02:04 PM 0
Share

Thanks! Turned out I can't use Enter for new lines!

avatar image
0

Answer by MacDx · Dec 01, 2017 at 09:01 PM

You could create some tools and a couple editor extensions to allow for an easier workflow and creation of this kind of files within Unity's editor.

Something like this tutorial might help with that: https://www.lynda.com/Unity-tutorials/Unity-5-2D-Building-Tile-Map-Editor/384876-2.html

Or you could also (and I like this option better) use an external program that is already specialized in this kind of task, and the only thing you need to code is the parsing of the level files which you probably are already doing. Right now I'm working on a project where I've taken this path. I have a script in my scene that can parse CSV (Comma Separated Values) files and instantiate the corresponding objects. As for the creation of said CSV file, I use the Tiled Map Editor which is like a mini photoshop for tiled based map creation and I really like it, it is pretty easy to use and it is free! There are a couple of plugins in the asset store that do this piece of functionality too, but those aren't free :(

Hope this helps!

Comment
Add comment · Show 2 · 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 Bojaniko · Dec 01, 2017 at 09:20 PM 0
Share

I'm not really looking to make a tile editor. I have a script that spawns background tiles which you can select and each tile has a container GameObject which just tells it what is at that tile and I also have a script that creates objects depending on the ID and where in the array it is located. So if I said 1 would be a soldier, then if array element 6 has a value of 1 then at tile 6 the script would create that soldier. The problem is I don't want to have long lists of arrays in my script for each level but rather one level array in the script which takes a list of numbers from a separate file according to the level the player picks and spawns the map according to those numbers in the separate file. That is what I want to know how to do. Also please post only free tutorials :P

avatar image MacDx Bojaniko · Dec 01, 2017 at 10:34 PM 0
Share

which takes a list of numbers from a separate file according to the level the player picks

And how are you planning on getting that file? Are you going to write it by hand? According to the number of levels and the size of your grid, that will get unmaintainable really really fast buddy :/ That's why a tile map editor is necessary.

And this works fine but I don't want to have to edit each levels content in that script, rather I'd like to create a file in which you have a list of numbers and then in the array in my script the values would be set to the list of numbers I made in the separate file. How would I do this?

You should be clearer on your explanation next time. So it is specifically the parsing of the file what you are interested in. $$anonymous$$y suggestion is you start doing some research on some basic parsing, serialization/deserialization, and common formats like JSON and CSV. If you want a more specific solution you will need to post some code so I can see what your working with.

And finally

Also please post only free tutorials :P

I got it for free so I assumed it was free, my bad. You have every right to not use it if you don't want to. Nevertheless, I only post and will keep posting content that I deem meritable. Some things are worth paying for :)

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

82 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I want to add items from a list into an array using the editor and popup menus 0 Answers

How to perform this Word Game 0 Answers

Assign role randomly from array for my online game 2 Answers

Keeping track of dynamic positions of items in a grid using List/Array? 0 Answers

How do i move a list object at the end of the list while shifting back the others? 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