- Home /
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:
#######
#. # .#
# $@$ #
# # #
#######
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
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!
So yeah if it wasn't implied, I think I will be going with the text file method.
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
Follow this Question
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