DefData ReadData System

Monkey Forums/Monkey Programming/DefData ReadData System

Chroma(Posted 2013) [#1]
Is there any way to emulate the DefData/ReadData commands from BMax using an external text file?

Something like:

[monkeycode]
Local data:DefData = LoadDefData(data.txt)
Local a:String
ReadData(a,data)
[/monkeycode]

And the ReadData would read sequentially through the data(stream?) and assign the value to a?

The text file would look something like this:
8,20,9
"00,00,00,00,00"



smilertoo(Posted 2013) [#2]
This is what im using:

Method loadLevel:bool(filename:string)
Local filepath:string = "levels/" + filename
Local file:string
Local fileLines:StringList = New StringList
Local count:Int = 0

file = LoadString(filepath) 'load in the file
lineCount = 0 'reset data lines counter

If file 'if file loaded then start pulling it apart into lines
Local line:String [] = file.Split ("~n")

'strip file into lines
For Local thisline:String = Eachin line
fileLines.AddLast ( thisline.Trim () )
Next
Return True
EndIf

Return false
End


Chroma(Posted 2013) [#3]
Dude, thank you! You just gave me back what I had in BMax for loading data from external files. :)


NoOdle(Posted 2013) [#4]
Please see below... decided to write a better example for you!


NoOdle(Posted 2013) [#5]
Ok, its your lucky day!

[monkeycode]

'/ Blitz Style Data
Class Data

'/ Stores data by filename (without txt extension)
Global dataMap : StringMap< String[] > = New StringMap< String[] >

'/ Used to read data
Global pointerData : String[]
Global pointerIndex : Int = -1


'/ Loads CSV Data from file
Function Load : Void( fileName : String, directory : String = "" )
Local file : String = LoadString( directory + fileName + ".txt" )
file = file.Replace( "~n", "," )
file = file.Replace( ",,", "," )
Local fileData : String[] = file.Split( "," )
dataMap.Set( fileName, fileData )
End Function


'/ Changes Pointer and resets index to 0
Function Restore : Void( name : String )
pointerData = dataMap.Get( name )
pointerIndex = -1
End Function


'/ Reads next bit of data if it exists
Function Read : String()
If pointerData
pointerIndex = pointerIndex + 1
If pointerIndex < pointerData.Length
Return pointerData[ pointerIndex ]
Endif
Endif
Return ""
End Function

End Class
[/monkeycode]

To load data use the function Data.Load( file, dir ="" ) You mustn't provide the .txt part in the filename as it knows it is a text file. You can optionally provide a directory e.g. "levels/" otherwise the default data dir will be used. Restore and Read works like in Blitz..

[monkeycode]
'/ Loads data
Data.Load( "level1" )
Data.Load( "level2", "maps/" )

'/ Start reading level 1 data
Data.Restore( "level1" )
Print Data.Read()
Print Data.Read()

'/ Start reading level 1 data from beginning again
Data.Restore( "level1" )
Print Data.Read()
Print Data.Read()

'/ Change to level 2 data and start reading
Data.Restore( "level2" )
Print Data.Read()
Print Data.Read()
Print Data.Read()
Print Data.Read()

[/monkeycode]


Amon(Posted 2013) [#6]
Now that is Sexy! :)


Chroma(Posted 2013) [#7]
Awesome thanks! I'm saving that one.


Amon(Posted 2013) [#8]
After testing this on glfw, works fine, on Android I get 'Invalid INT ~n'.

I'm basically loading the CSV data in to my array using INT(Data.Read()). Again this works on glfw but crashes on Android. I'm refering to NoOdles code.


Amon(Posted 2013) [#9]
If I remove ~n from the file android version works without crashing but the level data is all messed up i.e. it doesn't represent the correct positions.

Any ideas?


NoOdle(Posted 2013) [#10]
I get my nexus 7 today for my birthday so I'll be able to test this over the weekend and get back to you :)


Amon(Posted 2013) [#11]
Thanks dude! :)

[edit] Happy Birthday to you, also!


Amon(Posted 2013) [#12]
Did you get your nexus 7? :)

Any news on this fix? I'm still clawing at my brain trying to find a workaround but it's not happening!


Amon(Posted 2013) [#13]
Bump!


NoOdle(Posted 2013) [#14]
I did get my nexus! I also had a rather long house party, consumed silly amounts of alcohol and as a result I've been feeling rather ill for most of the day! I'll try out the code tomorrow and get back to you. Sorry for the delay.


Amon(Posted 2013) [#15]
Sorry to be a pain but, Bump!


Amon(Posted 2013) [#16]
Yojumpy bumpy!


Amon(Posted 2013) [#17]
Yolanda, I'll bump this anaconda!