Loading Data Values into an ARRAY

Blitz3D Forums/Blitz3D Beginners Area/Loading Data Values into an ARRAY

Waz(Posted 2003) [#1]
HI ALL
I am currently reading that topic in my krylers book and it confuses me.

It kinda not explain what data values are used for?

Basicly far as I understand u can create a data value and load it into an Array to hold the data information and then be used later on?

what confuses me is why do this? Why not just type all the data in an ARRAy?

Heres the code i came up with from the book yet its confusing me.

Can someone explain to me plz in a noobie form way.
Cheers
waz
Graphics 640,480

; Dim our namearray
Dim NameArray$(4,4)

;Read in the data
;go to the front of the data lines
Restore NameData

;Loop from 1 to 4 rows
For NameType% = 1 To 4
	;loop from 1 to 4 columns
	For Names% = 1 To 4
		Read NameArray$(NameType%,Names%)
	Next
Next

;Print out the data
;Set up the vertical control varible
Texty% = 0

;Loop from 1 to 4 rows
For NameType% = 1 To 4
	;Loop from 1 to 4 columns
	For Names% = 1 To 4
		Text 0,Texty, NameArray$(NameType%,Names%)
		Texty = Texty + 16
	Next
Next

WaitKey()

End


.NameData
Data "John","Joe","Mark","George"
Data "Sally","Betty","Lorelei","Anne"
Data "Fido","Spot","Killer","Tank"
Data "Millennium Hawk","Tea Fight","Zap-Wing","Dead Star"




pjd(Posted 2003) [#2]
Not sure how you would expect to "just type all the
data in an array" ?!?!?!?!

Do you mean like :-

namearray$(1) = "john"
namearray$(2) = "joe"
namearray$(3) = "mark"
etc.
....
This would be very inefficient !

Think about how awkward it would be to add new data,
change the order of data in the array etc etc.

And with the RESTORE command, you can direct your
code to read data from different sections (eg
different levels in a game)


(if that doesn't make sense, just take my word for it -
it's a lot easier and more flexible using read/data !!!!)


semar(Posted 2003) [#3]
Of course you can store each value in the array, but consider when you have an huge numbers of values, for example a lot of numbers that should be stored in the array.

How would you do it manually ? You should then write something like:
dim arr(1000)
arr(1) = some value1
arr(2) = some value2
arr(3) = some value3
arr(4) = some value4
arr(5) = some value5
arr(6) = some value6
.
.
arr(99) = some value99
arr(100) = some value100


But then, you would have spent lots of time, with an high typo risk.

Instead, a loop like this:
for n = 1 to 100
Read my_value
arr(n) = my_value
next

Data 1,4,7,3,5,78,0,4,7,8,1,4,7,3,5,78,0,4,7,8
Data 1,4,7,3,5,78,0,4,7,8,1,4,7,3,5,78,0,4,7,8
Data 1,4,7,3,5,78,0,4,7,8,1,4,7,3,5,78,0,4,7,8
Data 1,4,7,3,5,78,0,4,7,8,1,4,7,3,5,78,0,4,7,8
Data 1,4,7,3,5,78,0,4,7,8,1,4,7,3,5,78,0,4,7,8


Will fill the array without any hassle for you.

Same with the example provided.

Of course, you can insert each array entry with:
namearray(1) = "John"
namearray(2) = "Joe"
namearray(3) = "Mark"
.
.
and so on, but is much more easy to write data statements for this, and make a loop to read all the values.

Suppose for example that the telephone company gives you a job, and you have to fill an array with all the names of your neighbourgs.

The list of names is provided in a plan .txt file, like this:
John
Mike
Rob
Betty
Sally
Sara
.
.
and so on.

How do you put all that thousend of names in an array ? Would you open the txt file, copy each name, and paste in a statement like
arr_name(1) = "john"
arr_name(2) = ...

or, perhaps, would you read the names directly from the file in the array ?

I suppose the second one, if you want to end the task before the end of this century.

So, reading values from data statements, is easier like reading data from a file.

Hope this has sense for you,
Sergio.


Bremer(Posted 2003) [#4]
It is to make it easier on your self. Instead of having 200 lines of code that puts different data into an array you can have 3-4 lines of code that reads it off the data lines and puts it in there for you. eg.

either

dim blockCoordinates(40,40)
blockCoordinates(0,0) = 10
blockCoordinates(0,1) = 10
blockCoordinates(0,2) = 10
blockCoordinates(0,3) = 10
blockCoordinates(0,4) = 10
... etc..80 lines of code later,,,,

or

dim blockCoordinates(40,40)
restore bcoords
for x = 0 to 39
for y = 0 to 39
read blockCoordinates(x,y)
next
next

.bcoords
data 0,10,20,30,40,50,60 etc...

It makes your code easier to read as well.

I hope this help, it is early and I hadn't had breakfast yet, ;-)


soja(Posted 2003) [#5]
Hi Waz,

The first reason that occurs to me is that if you typed in all the data into the array, then you would have to do a lot of typing, e.g.:

NameArray$(1,1) = "John"
NameArray$(1,2) = "Joe"
NameArray$(1,3) = "Mark"
NameArray$(1,4) = "George"
NameArray$(2,1) = "Sally"
...etc, for all 16 elements

This is because each data value is quit unique. They have to be set separately. However, in the DATA example, you just need a loop or two.

And the second reason that occurred to me:
Keeping your DATA all in on spot (i.e. top or bottom of program) makes it very easy to see and change when you need to. Obviously it doesn't matter for short programs, but when your program gets bigger, or you have more data, it's easier to deal with when it's all together.

Keep in mind that data can be used anywhere a normal variable can be used (once it is read). It doesn't HAVE to be used in arrays -- that's just the example here.

A typical use for DATA might be level data. Say you're making a maze game with treasures. You could have different datasets for each level, e.g.:

.level1
Data "######"
Data "#  $ #"
Data "# ####"
Data "#  # #"
Data "##   #"
Data "#  #S#"
Data "######"

And then you can read that in, draw a wall everywhere a # is, treasure where $ is, and start the player where S is. (I'm not saying this is how anyone SHOULD do it, I'm just saying it might be how someone might do it. Personally, I rarely use DATA sets...)


soja(Posted 2003) [#6]
Wow, everybody sure jumped on this topic (there were 0 replies when I started mine) =)


Waz(Posted 2003) [#7]
THanks for fast replies is very kind of you.
I think I am starting to understand.

I think yet again what confuses me is how I would use it in a game?

Could it like hold images for levels or something?

Ya see i learning all this code and programming like arrays, constants, varibles, control statements etc yet I aint still got a clue how I make a game with it.

I looked at other example codes and games yet it just ponders me.


Azathoth(Posted 2003) [#8]
In C you can do it but I don't think with Blitz


Waz(Posted 2003) [#9]
Ah ok..

So what kinda data would I use data values for in a game?

could I do like
.PlayerNames
Data "+p1+","+p2+"
Data "+p3+","p4+"



and have like 

player1name$ = input$("Enter player 1's name: ")


and then later do something like

dim players$(1,1)

for n = 0 to 1
   for n = 0 to 1
      Read players$(n,n)
  next
next

For n = 0 to 1
   for n = 0 to 1
   Text 0,0,players$(n,n)



Would it be used for sometihng like that?
thats just an example to try wrap my head round things.


Dr Derek Doctors(Posted 2003) [#10]
NameData is a label. When you use the RESTORE command it basically tells any subsequent READ statements to start from the label you RESTOREd to.

So if you need to read the same chunk of data twice you might stick a label at the top of it so you can RESTORE the read pointer between sets of reads.

The actual variable you READ into (as in a$ in READ a$) isn't related to the label name at all, but just to the type of data you're reading. Ie, if you're reading string data you should use read variable_name$, or if you're reading float you use read variable_name#.


pjd(Posted 2003) [#11]
namedata is the label which identifies where the data
is (ie, where it will start looking for data when you
do a "read" command)

When you do the "read", it will read the first item after
the "data" statement and store it in the variable after
the "read" command", in your case this is name$

EG, the following will print "john" and then "sally"
because first it restores to namedata1 so the first
"read" reads the first bit of data after .namedata1
then it restores to namedata2 and the second
"read" reads the first bit of data after .namedata2

restore namedata1
read name$
text 100,100,name$
restore namedata2
read name$
text 100,100,name$
end
.namedata1
date "john" , "fred"
.namedata2
date "sally" , "jo"


hope that makes sense.


pjd(Posted 2003) [#12]
Huh, too late.
(what you need WAZ, is a mentor .... !)


QuietBloke(Posted 2003) [#13]
OK.. you seem to have got a little confused.

At the start of your data you have a label called
.NameData

This is just a label to mark the start of your data. In fact take that line out and your code will still work.

In your loop what you are doing with the

Read name$

line is asking to read in the next item in your data and place the results into the variable called name$

The code then checks to see if the value of name$ equals "STOP". If it does then the variable bFinished is set to a value of True.

If the value of name$ is not "STOP" then youdisplay the value of name$ onto the screen.

The wend will then take you back to the top of the loop.
The while statement states that if the variable bFinished is false then continue into the loop.

You then read the next value from your data into the variable name$.. etc.. etc..

So you might be asking what is the point of the .NameData line ?

Well.. suppose after you have read all the data your program needs to read it in again.

What you would do is

Restore NameData

Now if you do a Read Name$ and display the value it will contain the value of "John". It has started from the top of your data again.

With the labels you can decide within the program at what point within the data you want to start reading.

Graphics 640,480

;print out the data
;set up our vertical control varible
Texty% = 0

;set up our boolean value for seeing if we're done or not
bFinished = False
restore NameDataLvl2

;While we're NOT finished
While Not bFinished
	Read Name$
	If Name$ = "STOP"
		bFinished = True
	Else
	Text 0,Texty%,Name$
	Texty% = Texty% + 16
	EndIf
Wend

WaitKey
End

	

.NameDataLvl1
Data "John","Joe","Mark","George"
Data "Sally","Betty","Lorelei","Anne"
Data "Fido","Spot","Killer","Tank"
Data "STOP"
.NameDataLvl2
Data "Millennium Hawk","Tea Fight","Zap-Wing","Dead Star"
Data "STOP"


The above example will start reading from NameDataLvl2.

Hope that helps a little. Keep at it because it will all become crystal clear soon.