ReadLine
Blitz3D Forums/Blitz3D Programming/ReadLine
| ||
I have a question about files. If I make a .txt file and I write "PositionEntity cube, 0, 9, 8" in it, and then I read that line with "ReadLine," would it position the cube? Or is there a way where I can read Blitz code from a file? |
| ||
Readline won't work this way, but you can use the "Include" command. But consider that this File cannot be edited if you have once made an Exe from the Source. Cause it simply includes your File in the Source and then compiles both to one Exe. But personally I would rather store x,y and z as numbers in the file, then open the file from within Blitz, read the values and position the cube using these values. |
| ||
Thanks for the information, jfk. I have a function named "CreateObject" with a bunch of paremiters following (darn, I can NEVER spell that word). I want my level file to look something like this: createobject("car", "Bob's Car", 8, 7, 1, 0, 0, 1, 1, "object", 14, 9) createobject("bob", "Goordinal", 5, 1, 0, 1, 1, 2, 1, "object", 14, 9) createweapon("weapon1", "shotgun", 4) createplayer() initlevel(level) Which is all Blitz code. And I initially tried using "Include:" Function LoadLevel(filepath$) Include filepath$ End Function But it brought up an error that said "Expecting include filename." How could I fix the include error? |
| ||
Or, rather, is there another way to go about the problem? |
| ||
You are confusing the Include command Steven. 'Include' is for including ACTUAL CODE into your program code. This can only be done while compiling your program. The Include command just allows you to break up your large blocks of code into seperate files for better organization of your code. WHAT YOU WANT is to have a file parser read in a file and perform commands based on the contents of the file. You will have to write specific code yourself to 'read' the file. Have a look at this tutorial I wrote on how to parse a file: http://www.blitzbasic.com/Community/posts.php?topic=27896 |
| ||
Thanks, WolRon. |
| ||
Or you could try to use BVM. |
| ||
I'd have to pay for that. :( |
| ||
Or you can use read / write commands to save and load the data values individually.Global dat$[16] dat[1]="car" dat[2]="Bob's Car" dat[3]=8 dat[4]=7 dat[5]=1 dat[6]=0 dat[7]=0 dat[8]=1 dat[9]=1 dat[10]="object" dat[11]=14 dat[12]=9 filename$="info" ;save info file=WriteFile(filename$+".dat") For loop = 1 To 12 WriteString(file,dat[loop]) Next CloseFile ;load info file=ReadFile(filename$+".dat") For loop = 1 to 12 dat[loop]=ReadString(file) Next CloseFile(file) |
| ||
The way I do it, is that I have "custom variables" which are just types with a name and a value field... And I have another flag that indicates wheter the custom var is saveable, and one that indicates that it should be deleted after saving (and also after loading, after I did the right things with them). That way I can loop trough my cvars and write the name/values of the saveable ones to a file easily. Basically the same principle as the arrays in the post above, but having names instead of numbers makes it a bit simpler to me. |
| ||
The array is just for an example, you could do it with vars, types, etc. The ideas is that you can save and load the values individually instead of grouping them together. If you want the saved data to be easily edited use WriteLine/ReadLine instead of the other Write/Read commands. But, if you absolute want to parse the values, I recommend using the CSV (comma separated values) format which a common format. You can find a few parsers in the code archives. I grabbed a couple of snippets for you: Split CSV parameters out of a string & Read Comma Delimited String. |