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 RaymondTunstill · Apr 01, 2015 at 11:51 AM · c#arraylistsplittextfile

Splitting text from lines into variables

i have this text file below:

001 Bulbasaur 45 49 49 65 65 45 Grass Poison

002 Ivysaur 60 62 63 80 80 60 Grass Poison

003 Venusaur 80 82 83 100 100 80 Grass Poison

004 Charmander 39 52 43 60 50 65 Fire

005 Charmeleon 58 64 58 80 65 80 Fire

I would like to read all of the lines into a temporary array, then set the size of a class object to the number of lines then for each string split by space in each line assign to class object. The code i tried was this.

 var lines = textFile.text.Split("\n"[0]);
     monster = new Monsters[lines.Length];
     List<string> lineSplit = new List<string>();
     for (int i = 0; i < lines.Length; i++) {
         Debug.Log(lines[i]);
 
         lineSplit.Clear();
         lineSplit = lines[i].Split(' ').ToList ();
 
         monster[i].ID                   = int.Parse(lineSplit[0]);
         monster[i].Name             = lineSplit[1].ToString();
         monster[i].HP                  = float.Parse(lineSplit[2]);
        monster[i].ATK                  = float.Parse(lineSplit[3]);
         monster[i].DEF                 = float.Parse(lineSplit[4]);
         monster[i].SPATK             = float.Parse(lineSplit[5]);
         monster[i].SpDEF             = float.Parse(lineSplit[6]);
         monster[i].speed             = float.Parse(lineSplit[7]);
         monster[i]._FirstType        =   (FirstType)System.Enum.Parse(typeof(FirstType),lineSplit[8]);
         monster[i]._SecondType    =  (SecondType)System.Enum.Parse(typeof(SecondType),lineSplit[9]);  
 ``  }

Any help would be appreciated thank you

Comment
Add comment · Show 2
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 gjf · Apr 01, 2015 at 10:08 AM 1
Share

if you have no control over the file (i.e. generate in another format like json) then take a look at regular expressions. following your file format (it looks like the second type is optional), here's how you might do it using regular expressions:

 using UnityEngine;
 using System.Collections.Generic;
 using System.Text.RegularExpressions;
 using System.IO;
 
 public class RegexDemo : $$anonymous$$onoBehaviour
 {
     public enum $$anonymous$$onsterType
     {
         Grass, Poison, Fire, Default
     }
 
     public class $$anonymous$$onster
     {
         public int ID;
         public string Name;
         public float HP;
         public float AT$$anonymous$$;
         public float DEF;
         public float SPAT$$anonymous$$;
         public float SpDEF;
         public float speed;
         public $$anonymous$$onsterType FirstType;
         public $$anonymous$$onsterType SecondType;
     }
 
     private const string DataFilename = "/Assets/data.txt";
 
     private List<$$anonymous$$onster> _monsters;
 
     void Awake()
     {
         _monsters = new List<$$anonymous$$onster>();
 
         var dataFile = File.ReadAllText(Directory.GetCurrentDirectory() + DataFilename);
         var lines = dataFile.Split('\n');
 
         var regex = new Regex(@"(\d*)\s*(\w+)\s*(\d*)\s*(\d*)\s*(\d*)\s*(\d*)\s*(\d*)\s*(\d*)\s*(\w+)\s*(\w+)?", RegexOptions.IgnoreCase);
 
         foreach (var line in lines)
         {
             var currentLine = line.Replace("\n", string.Empty);
 
             if (currentLine != string.Empty)
             {
                 var lineElements = regex.$$anonymous$$atch(currentLine);
 
                 if (lineElements.Success)
                 {
                     var monster = new $$anonymous$$onster
                         {
                             ID = int.Parse(lineElements.Groups[1].Value),
                             Name = lineElements.Groups[2].Value,
                             HP = float.Parse(lineElements.Groups[3].Value),
                             AT$$anonymous$$ = float.Parse(lineElements.Groups[4].Value),
                             DEF = float.Parse(lineElements.Groups[5].Value),
                             SPAT$$anonymous$$ = float.Parse(lineElements.Groups[6].Value),
                             SpDEF = float.Parse(lineElements.Groups[7].Value),
                             speed = float.Parse(lineElements.Groups[8].Value),
                             FirstType = ($$anonymous$$onsterType)System.Enum.Parse(typeof($$anonymous$$onsterType), lineElements.Groups[9].Value),
                             SecondType = !string.IsNullOrEmpty(lineElements.Groups[10].Value) ? ($$anonymous$$onsterType)System.Enum.Parse(typeof($$anonymous$$onsterType), lineElements.Groups[10].Value) : $$anonymous$$onsterType.Default
                         };
 
                     _monsters.Add(monster);
                 }
             }
         }
 
         Debug.Log("Found " + _monsters.Count + " monster definitions");
     }
 }


avatar image RaymondTunstill · Apr 01, 2015 at 12:28 PM 1
Share

Thank you i solved it without your code, it turns out i was referencing a new class object of a existing object what so the instance was always null. Thank you anyways, Good solution

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Array of tags, gettings tags using CompareTag 1 Answer

How to properly create a 2 dimensional array of an object. [C#] 1 Answer

Split without using a delimiters 2 Answers

Pathfinding through pairs of connections 2 Answers

question about array or list of boolean 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