Readline

BlitzPlus Forums/BlitzPlus Programming/Readline

Petron(Posted 2007) [#1]
I have been trying to create a level editor for my game, using data. I have the data working flawlessly so I tried to add a save function. When I use a variable saved with the data, and apply is in place of the data, everthing works. When I use the readline command to load the same string of data I recieve an error like "all files in data must be a constant" So I made the variable loaded with the readline command a constant using the const command. I got the same error. Does anyone have any ideas of how to fix this error? Thanks


Alaric(Posted 2007) [#2]
could we get some code to test what you're talking about?


Petron(Posted 2007) [#3]
This is the problem code. It creates the error message "expression must be a constant". There is other code, but it works. The problem only occurs with the readline section.
 

Const filein = ReadFile("file.txt")
Const Read1$ = ReadLine(filein)
;Const Read2$ = ReadLine$(filein)
;Const Read3$ = ReadLine$(filein)
;Const Read4$ = ReadLine$(filein)
CloseFile( filein )
.level0
Data Read1$;1
Data "00000000000000000000000000000000000000000000000000";2
Data "00000000000000000000000000000000000000000000000000";3
Data "00000000000000000000000000000000000000000000000000";4
Data "00000000000000000000000000000000000000000000000000";5
Data "00000000000000000000000000000000000000000000000000";6
Data "00000000000000000000000000000000000000000000000000";7
Data "00000000000000000000000000000000000000000000000000";8
Data "00000000000000000000000000000000000000000000000000";9
Data "00000000000000000000000000000000000000000000000000";10
Data "00000000000000000000000000000000000000000000000000";11
Data "00000000000000000000000000000000000000000000000000";12
Data "00000000000000000000000000000000000000000000000000";13
Data "00000000000000000000000000000000000000000000000000";14
Data "00000000000000000000000000000000000000000000000000";15
Data "00000000000000000000000000000000000000000000000000";16
Data "00000000000000000000000000000000000000000000000000";17
Data "00000000000000000000000000000000000000000000000000";18
Data "00000000000000000000000000000000000000000000000000";19
Data "00000000000000000000000000000000000000000000000000";20
Data "00000000000000000000000000000000000000000000000000";21
Data "00000000000000000000000333000000000000000000000000";22
Data "0000000000000000G000003333300000000000000000000000";23
Data "3333333000033333Z333333333333333333333333333333333";24
Data "313F3DZ0000EZ333Z333333363333333333333333333333323";25
Data "3333333000033333Z333333333333333333333333333333333";26
Data "0000000000000000Z000003333300000000000000000000000";27
Data "00000000000000000000000333000000000000000000000000";28
Data "00000000000000000000000000000000000000000000000000";29
Data "00000000000000000000000000000000000000000000000000";30
Data "00000000000000000000000000000000000000000000000000";31
Data "00000000000000000000000000000000000000000000000000";32
Data "00000000000000000000000000000000000000000000000000";33
Data "00000000000000000000000000000000000000000000000000";34
Data "00000000000000000000000000000000000000000000000000";35
Data "00000000000000000000000000000000000000000000000000";36
Data "00000000000000000000000000000000000000000000000000";37
Data "00000000000000000000000000000000000000000000000000";38
Data "00000000000000000000000000000000000000000000000000";39
Data "00000000000000000000000000000000000000000000000000";40
Data "00000000000000000000000000000000000000000000000000";41
Data "00000000000000000000000000000000000000000000000000";42
Data "00000000000000000000000000000000000000000000000000";43
Data "00000000000000000000000000000000000000000000000000";44
Data "00000000000000000000000000000000000000000000000000";45
Data "00000000000000000000000000000000000000000000000000";46
Data "00000000000000000000000000000000000000000000000000";47
Data "00000000000000000000000000000000000000000000000000";48
Data "00000000000000000000000000000000000000000000000000";49
Data "00000000000000000000000000000000000000000000000000";50



Alaric(Posted 2007) [#4]
ok... first off, you can't change the value of constants that you declare. The problem is that every time you run the program, windows loads the file into a different location so you can't declare filein as constant. The same goes with read1$ because theoretically, the contents of the file could change each time the program is run. So, you need to change all of the const commands in the program to global (for a variable), or just leave it out all together.

Your next problem is that you don't understand just how the data command works and I suspect that you probably don't need it for your level creator at all. You see, the data command can only be used to store large amounts of information that don't change whenever the program is run. So data command lines are similar to const lines that store a lot of information. In addition, you have to use more commands to access the information that you stored with the data commands. Specifically, you should look into the restore and read commands if your still interested in using the data command.

However, I don't believe you need to use this command because you said that you were building a save command into your program. You usually save information into files and don't use the data command at all. normally you would use the writeline, writebyte, write-whatever, commands to save information.


Petron(Posted 2007) [#5]
First of all, without the const commands I get the error "All files in data must be a constant" YOu are right about not needing the data command for a level editor, but that is how my entire game is based, and I do not wish to change it. The only reason that I am useing the readline command at all is so that I can change levels without recompiling my game. There will be only one file like this, but I need to have it use data and work this way. The file is not used as a save file, but the editor itself. The editing is complicated and not ment to be efficient. I need the editor so that I can distribute it without giving out the source code. I am only writhing the engine, all the levels will be written by other people. This is why I need it this way. Thank you for your seggestions, but if you have any other ideas that would be greatly appreciated.


William Drescher(Posted 2007) [#6]
I believe the problem lies in the first DATA call (Data Read1$). That is where your "All files in DATA must be a constant." You can't put a 'ReadLine' returned variable into a DATA command because it's not a constant, so the first one will have to be a default, like "01EF030201", or, when you save the level, make the first line a variable, insert it in the file, then, start reading from the data commands.


William Drescher(Posted 2007) [#7]
Here's a twist!

I re-ran the program changing the Const Read1$ = ReadLine("file.txt") to Global Read1$ = ReadLine("File.txt") and the error was "Data expression must be constant" and the cursor was put to the first line in the Data call. So I have now fully confirmed my previous post both to me, and the readers! :)


Petron(Posted 2007) [#8]
Can you explain how to do that.


Alaric(Posted 2007) [#9]
This does most of what you are trying to do... I think.

Global filein = ReadFile("file.txt")
Global Read1$ = ReadLine(filein)
;global Read2$ = ReadLine$(filein)
;global Read3$ = ReadLine$(filein)
;global Read4$ = ReadLine$(filein)
CloseFile( filein )
Print "Read1="+read1
Restore level0
For I=1 To 49
Read read1
Print read1
Next
WaitKey()

.level0
Data "00000000000000000000000000000000000000000000000000";2
Data "00000000000000000000000000000000000000000000000000";3
Data "00000000000000000000000000000000000000000000000000";4
Data "00000000000000000000000000000000000000000000000000";5
Data "00000000000000000000000000000000000000000000000000";6
Data "00000000000000000000000000000000000000000000000000";7
Data "00000000000000000000000000000000000000000000000000";8
Data "00000000000000000000000000000000000000000000000000";9
Data "00000000000000000000000000000000000000000000000000";10
Data "00000000000000000000000000000000000000000000000000";11
Data "00000000000000000000000000000000000000000000000000";12
Data "00000000000000000000000000000000000000000000000000";13
Data "00000000000000000000000000000000000000000000000000";14
Data "00000000000000000000000000000000000000000000000000";15
Data "00000000000000000000000000000000000000000000000000";16
Data "00000000000000000000000000000000000000000000000000";17
Data "00000000000000000000000000000000000000000000000000";18
Data "00000000000000000000000000000000000000000000000000";19
Data "00000000000000000000000000000000000000000000000000";20
Data "00000000000000000000000000000000000000000000000000";21
Data "00000000000000000000000333000000000000000000000000";22
Data "0000000000000000G000003333300000000000000000000000";23
Data "3333333000033333Z333333333333333333333333333333333";24
Data "313F3DZ0000EZ333Z333333363333333333333333333333323";25
Data "3333333000033333Z333333333333333333333333333333333";26
Data "0000000000000000Z000003333300000000000000000000000";27
Data "00000000000000000000000333000000000000000000000000";28
Data "00000000000000000000000000000000000000000000000000";29
Data "00000000000000000000000000000000000000000000000000";30
Data "00000000000000000000000000000000000000000000000000";31
Data "00000000000000000000000000000000000000000000000000";32
Data "00000000000000000000000000000000000000000000000000";33
Data "00000000000000000000000000000000000000000000000000";34
Data "00000000000000000000000000000000000000000000000000";35
Data "00000000000000000000000000000000000000000000000000";36
Data "00000000000000000000000000000000000000000000000000";37
Data "00000000000000000000000000000000000000000000000000";38
Data "00000000000000000000000000000000000000000000000000";39
Data "00000000000000000000000000000000000000000000000000";40
Data "00000000000000000000000000000000000000000000000000";41
Data "00000000000000000000000000000000000000000000000000";42
Data "00000000000000000000000000000000000000000000000000";43
Data "00000000000000000000000000000000000000000000000000";44
Data "00000000000000000000000000000000000000000000000000";45
Data "00000000000000000000000000000000000000000000000000";46
Data "00000000000000000000000000000000000000000000000000";47
Data "00000000000000000000000000000000000000000000000000";48
Data "00000000000000000000000000000000000000000000000000";49
Data "00000000000000000000000000000000000000000000000000";50


Floyd(Posted 2007) [#10]
You'll be a lot happier if you abandon this idea and use files rather than Data statements for the levels. Since the Data statements are part of the code you can't add or change levels without recompiling and distributing new executable programs.

But if you really want to put everything into Data then you could select which one to use like this:

DataSet = 2

Select DataSet
	Case 1: Restore data1
	Case 2: Restore data2
	
	Default: ; do some kind of error handling
End Select

; Now ready to read from one of the data sets.
; In this case the next Read would produce "Second data set."


.data1
Data "First data set."

.data2
Data "Second data set."



Petron(Posted 2007) [#11]
I am already using code like that to change levels. I had another idea that may work. Is it possible to create an exe file of a piece of code, but not compile the include file in and then edit the include file to change the levels.


b32(Posted 2007) [#12]
No, I'm afraid that is not possible. All the files, including 'included' files are compiled into the .exe.

However, you can use a data file to store the level data.
So then you have the compiled .exe and a separate file that contains the level data.

Later on, when you want to publish your project, you could perhaps use a data packer to include all your data into a single file.


Petron(Posted 2007) [#13]
That is what I wish to do, use a data file. It would be greatly apprecieated if you could post some sample code that would to that.


Alaric(Posted 2007) [#14]
Here's some code that might get it accross.

fileout=WriteFile("lol.txt");Creates or Recreates a file for writing
WriteLine(fileout,"Hello World!") ;Writes one line of text
WriteString(fileout,"ABCDEFGHIJKLM");Stores a standard string variable, the characters are ended with four bytes
WriteInt(fileout,131);Integer, the standard numeric variable. Max of 2147483647 Minimum of -2147483648
WriteShort(fileout,100);Two bytes, max of 65535
WriteByte(fileout,10);Single byte, max of 256
CloseFile(fileout);Moves the file from memmory to the hard drive

filein=ReadFile("lol.txt")
Print ReadLine(filein)
Print ReadString(filein)
Print ReadInt(filein)
Print ReadShort(filein)
Print ReadByte(filein)
CloseFile(filein)
WaitKey()

Just remember not to mix up the order that you read or write from/to the file. If you do, then the data will appear corrupted. Also, if you open a file for both reading and writing at once, without closing the file first, the file's data will be lost.


Ked(Posted 2007) [#15]
Please use codeboxes ( [code][/code] or [codebox][/codebox] )


Petron(Posted 2007) [#16]
That generates the same problem, how can you get objects loaded from a file into a data statement?


Alaric(Posted 2007) [#17]
Umm... you don't. Like I said, data statements are constant, which means that they can't be changed unless you recompile the program from the source code.


Alaric(Posted 2007) [#18]
so what exactly is keeping you from just loading this into an array or something like that?


Petron(Posted 2007) [#19]
I have already written an engine for data.


Alaric(Posted 2007) [#20]
well yeah, I have to agree that it kindof sucks that you have to change it, but from my experience that should be a minor change if you used functions to encapsulate your code well.


Petron(Posted 2007) [#21]
Oops, I didn't use functions.


Alaric(Posted 2007) [#22]
Here's how I'd do it... Feel free to write your own. It would probably be better for you to do that so you can get a feel for banks. Also, remember that a bank is just a section of memory, so you have to count in bytes and this also means that there is no REALLY good way to get to the data unless you treat it like Blitz treats an array. I used banks because I'm pretty sure that you can change the sizes of blitz basic arrays easily to accommodate for levels of different sizes.




Petron(Posted 2007) [#23]
That is great! It will take a while, but I think I may be able to get it to work, but I have one question. How do you read the lvl and edit it without being able to read bites. Otherwise that is great. Thank you.


Alaric(Posted 2007) [#24]
what do you mean? You can use peekshort to read the information from the bank, once you have loaded it. You can also use pokeshort (as seen in genrandlevel) to write them. Also, I said that you can't read and write to files at the same time, banks are a different story


Petron(Posted 2007) [#25]
I ment can you read and edit the level with a program like notepad.


b32(Posted 2007) [#26]
If you use readline/writeline to read/write data to the file, you can edit the file in notepad.


Alaric(Posted 2007) [#27]
you just have to make sure that you select bytes to represent your level that can be seen inside of notepad. By the way, you can open notepad for them with execfile("notepad.exe").