DefData ReadData System
Monkey Forums/Monkey Programming/DefData ReadData System
| ||
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" |
| ||
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 |
| ||
Dude, thank you! You just gave me back what I had in BMax for loading data from external files. :) |
| ||
Please see below... decided to write a better example for you! |
| ||
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] |
| ||
Now that is Sexy! :) |
| ||
Awesome thanks! I'm saving that one. |
| ||
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. |
| ||
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? |
| ||
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 :) |
| ||
Thanks dude! :) [edit] Happy Birthday to you, also! |
| ||
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! |
| ||
Bump! |
| ||
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. |
| ||
Sorry to be a pain but, Bump! |
| ||
Yojumpy bumpy! |
| ||
Yolanda, I'll bump this anaconda! |