Can I read a single value from an array?

BlitzMax Forums/BlitzMax Beginners Area/Can I read a single value from an array?

WarpZone(Posted 2006) [#1]
Okay, so I've got this array:

WayPointList:Int[1536,2560,1,0,0,0]

It's supposed to be a list of waypoints and the speed at which the screen should scroll from the old waypoint to the next one. I.E. this format:

[x,y,speed,x,y,speed... etc.]

Now I'm trying to figure out how arrays work in Blitz. This documentation is terrible. It explains how to use EachIn to loop through an entire array, but what's the command to just read one element from the array?

In other words, how would I go about doing:

camera.x = (first item from the array WayPointList)
camera.y = (second item from the array WayPointList)
camera.speed = (third item from the array WayPointList)

Also, are these arrays one-based or zero-based? (I.E. Is the first item in the array refered to by the number 0 or 1?)

Thanks.


Tom Darby(Posted 2006) [#2]
To read a specific element in an array, use the numeric index. For example, WayPointList[0] would return 1536, WayPointList[1] would return 2560, and so on.


WarpZone(Posted 2006) [#3]
Hmmm. That's the first thing I tried, but it gave me a compiler error:

"Compile Error: Incorrect number of array dimensions"

Since the array keyword in the blitzmax help file doesn't explain the array-related compiler errors or all of the array functionality, I have no idea how to interpret this.

I guess flash's documentation has just spoiled me. I'm too used to being able to F1 a keyword and getting a cross-referenced hyperlinked dictionary for my efforts. :P

EDIT: Oh, wait... I think the problem is my array constructor.

WayPointList:Int[1536,2560,1,0,0,0]

That's not right. That creates a multidimensional array, doesn't it? :P I wanted a single-dimmensional array with those explicit values.

Edit again: I found it in the help I badmouthed earlier. :P For those of you who might have the same problem...

Global WayPointList:Int[]=[1536,2560,1,0,0,0] 


..is apparently the correct way to do it.

Thanks, guys. :) Sorry for bothreing you.


H&K(Posted 2006) [#4]
ok right off Global WayPointList:Int[]=[1536,2560,1,0,0,0]
makes no sence

What are you trying to do?

Make a TYPE with all the (int) fields you want, THEN, make an array of that type.
type whatever
field xcoor:int
Field ycoor:int
Field speed:int etc
EndType
On the 0 or one thing, they start at 1 when defining them, and at 0 when accessing them


ImaginaryHuman(Posted 2006) [#5]
Shouldn't it be:

Global WayPointList:Int[]=[1536,2560,1,0,0,0]

and

Print WayPointList[0] 'Print first x value


Perturbatio(Posted 2006) [#6]
WayPointList:Int[1536,2560,1,0,0,0]


This creates an array of 1536 x 2560 x 1 items!

try:
WayPointList:Int[] = [1536, 2560, 1, 0, 0, 0]


*edit*

that's what I get for playing naked war before answering :)


H&K(Posted 2006) [#7]
@Angel

It still makes no sence
Global WayPointList:Int[]=[1536,2560,2,1,1,1]

Edit. Have I made a realy stupid mistake here?


Perturbatio(Posted 2006) [#8]
Have I made a realy stupid mistake here?


Was the rhetorical? ;)


WarpZone(Posted 2006) [#9]
I dunno. Blitz help says Global WayPointList:Int[]=[1536,2560,2,1,1,1] should work.


'Auto arrays' may be created using the syntax:
[Element1,Element2 etc...]

This returns a 1 dimensional array containing the specified elements, for example:

Local int_array[]=[1,2,3,4,5]



It compiles. No crashes. No errors. Go figure.


Perturbatio(Posted 2006) [#10]
yes, it's fine.

see http://www.blitzwiki.org/index.php/Arrays#Auto_Arrays


H&K(Posted 2006) [#11]
Even if I am wrong about that ;) I still think;

You should definitaly make a type for all of the fields you want in ANY single dimention array. And make an array of that type.


Perturbatio(Posted 2006) [#12]
You should definitaly make a type for all of the fields you want in ANY single dimention array.


what? even for an array of ints?


H&K(Posted 2006) [#13]
Yes. If its a single dimention array of TWO ints, then yes

I dont do array:Int[NumberofIntPairs,2]
i do Array:BiIntPair[NumberOfIntPairs]


Grey Alien(Posted 2006) [#14]
make an array of types.


Dreamora(Posted 2006) [#15]
On how to create it:

global waypoints:Int[2*NumberOfIntPaires]

And use it:

x = waypoint[indexOfTheWayPoint*2]
y = waypoint[indexOfTheWaypoint*2 + 1]

BlitzMax arrays are zero based (0 to length-1)

Btw: Filling arrays with [value,value,value] is a bad idea if you plan to use it realtime, beause this is extremely slow, compared to create it with new and fill it manually!


WarpZone(Posted 2006) [#16]
Okay, I finally managed to find an example of this "array of types" technique, but it's in B3D.

Type Test
 field x, y
end type

Dim MyType.Test( 100 )

For l = 0 to 100
  MyType(l)  = new Test
  MyType(l)\x = rand(10)
  MyType(l)\y = rand(10)
next


How do convert this to BMX?

(Sorry for asking stupid questions, but I've never done this before and the documentation is no help.)


Azathoth(Posted 2006) [#17]
Arrays in Blitzmax are not 1 element more.
Type Test
 Field x, y
End Type

Global MyType:Test[101] 

For l = 0 To 100
  MyType[l]  = New Test
  MyType[l].x = Rand(10)
  MyType[i].y = Rand(10)
Next