- Home /
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
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");
}
}
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