Using data from the Data command?

BlitzPlus Forums/BlitzPlus Beginners Area/Using data from the Data command?

Tomas Khan(Posted 2008) [#1]
Okay, so I have the Command Reference page for the Data command up, and it has an example of using Data on it, but I don't understand exactly how it works.

First, where are the variables for the data defined? Do you define them at the beginning with enemy images and such, or do you just call them in order and hope the compiler can figure out which is which, or is it something else?

Second, I've seen Data used to store information about level tiles (as in WolRon's platformer), but how exactly do you tell it what to do with the information you give it?

Third (but related to the second), how can you use the data from Data in conjunction with, say, types (such as a Tile type)? Or is it done with arrays?

Thank you.

EDIT: Okay, I think I got one. I just read the page for the Read command again, and it seems to say that you choose the variable's name when you call Read. So, unless that's not the case, First is solved. If that's not the case, though, please tell me.


markcw(Posted 2008) [#2]
Well, Data stores a list of variables which can be integers, strings, etc in consecutive order. You have to put a label before the Data variables to access them, the syntax is ".mylabel". Then to access the data you go to the start of the data list with "Restore mylabel". Then you read them one by one with "Read mydatavariable". So usually you do this in a For..Next loop. You have to know when the data ends or you will get a read error. To use them in conjunction with types you would just read the data variable into the type variable.


Tomas Khan(Posted 2008) [#3]
The label, Restore, and Read made enough sense. (It makes more sense when you explain it than when the Command Reference does, though. That thing really isn't clear.)

How, though, would you deal with, say, a group of tiles? You said "you would just read the data variable into the type variable" -- as in, say, "Read tile\x," right?

But how would you handle several hundred of them, as I think I would need for a functional screen? It would obviously be a little too complex to deal with each individually, and I don't really understand Data conventions enough to know how to deal with all of the tiles properly.


Snarkbait(Posted 2008) [#4]
You just read them in with a for loop, like this:

Type tile
	Field x,y,frame
End Type

Restore level1

For a = 1 To numTiles
	t.tile = New tile
	Read t\x
	Read t\y
	Read t\frame
Next

.level1
Data 0,0,1,0,32,1 ;etc...


Now, if the data statements get too long you are better off writing/reading directly from a file.


Tomas Khan(Posted 2008) [#5]
So you simply put all of the necessary information in order -- tile 1's X, Y, and type; tile 2's X, Y, and type... like that?


Snarkbait(Posted 2008) [#6]
yes.


Tomas Khan(Posted 2008) [#7]
Okay, I'll try coding it and see just how well I get what you've all said. Thanks!


markcw(Posted 2008) [#8]
Yes, "Read tile\x" is right. Read takes the current data variable and put its contents into the variable you specify after Read. Then the data pointer moves on to the next data variable.

You use Data/Restore/Read when you want to store some predefined data in the exe. I've always used it to store media in the exe. I don't know if there's a limit to how much you can store but I've stored at least 5mb without any problems. I always coded a file to generate the data rather than write it by hand.

If you have a lot of labels then you can manage them with Select/End Select. Like so:

Type tile
	Field x,y,frame
End Type

whichlevel=1

Select whichlevel
 Case 1
 Restore level1
 Case 2
 Restore level2
 ;etc.
End Select

For a = 1 To numTiles
	t.tile = New tile
	Read t\x
	Read t\y
	Read t\frame
Next

.level1
Data 0,0,1,0,32,1 ;etc...
.level2
Data 0,0,1,0,32,1 ;etc...



Tomas Khan(Posted 2008) [#9]
How do you code "a file to generate the data" as opposed to "writing it by hand"?

By the way, I've been coding a bit, and my tiles seem to work very well.


markcw(Posted 2008) [#10]
Well that's when you've got all the data in a file and you want to transfer the data into the blitz exe. Example:
http://www.blitzbasic.com/codearcs/codearcs.php?code=1820

Basically you use WriteFile with "myfilename.bb" and then read the source file and turn the values into strings and write them to the bb file. Then include it into your main program file.

If you don't have data already in a file then you would have to write it by hand/code it.


Tomas Khan(Posted 2008) [#11]
Okay. I really didn't understand that code, although I'm sure it works. I guess putting in the data by hand will work fine...

Well, I guess that's all. Thank you both very much!