Having difficulty with Arrays of Types...

Blitz3D Forums/Blitz3D Beginners Area/Having difficulty with Arrays of Types...

The r0nin(Posted 2004) [#1]
The documentation in Blitz3d lists the possibility of doing an array of types. However, the code given is both brief and does not explain the proper steps for using them. This is what it shows in the Blitz3d docs:

You can create variables or arrays of custom types using a '.' type tag followed by the type name. For example: 

Global mine.MyType Dim all_mine.MyType( 100 )


Now, I have a particular need for an array of types. I need to record the x, y, and z positions of multiple units over a period of movement. For example, I need to plot out the x, y, and z of Unit1 for 5 consecutive seconds, while also tracking the x, y, and z of Unit2 over the same 5 seconds. So each second, I would like to store the position of each unit, until I have 5 seconds worth of data. So far, I can code up to the following point, but I don't know how to get to any of the individual values in the array. Help!

Type t_unit_loc
Field x
Field y
Field z
End Type

Dim unit_loc.t_unit_loc(5)

For val=1 To 2
unit_loc.t_unit_loc(5) = New t_unit_loc
Next

;This gets me 2 arrays of types to use with unit1 and
;unit2, but how do I put the values in each x, y, and z?



Tiger(Posted 2004) [#2]
Type t_unit_loc
Field x
Field y
Field z
End Type

Dim unit_loc.t_unit_loc(5)

For val=0 To 1
unit_loc.t_unit_loc(val) = New t_unit_loc
Next

unit_loc(0)\x=5
unit_loc(0)\y=10

unit_loc(1)\x=15
unit_loc(1)\y=20


For val=0 To 1
	DebugLog "Unit loc:"+val
	DebugLog unit_loc(val)\x
	DebugLog unit_loc(val)\y
Next

WaitKey



Here is how to do it.


PowerPC603(Posted 2004) [#3]
You can try this:

; Create a type which holds the x, y and z values
Type t_unit_loc
Field x,y,z
End Type

; Create an array of types
; Note that the array holds the memory-address of the types,
; so you will have to create all type-instances and put the
; handle of that type in the array
Dim Unit1.t_unit_loc(5)

; Create 5 type-instances and put the handle in the array
For i = 1 to 5
Unit1.t_unit_loc(i) = New t_unit_loc
Next

; After this, you can refer to the variables in the array
; this way:
Unit1(1)\x = 55
Unit1(2)\y = 10
Unit1(5)\z = 159

; Print the contents of the array

For i = 1 to 5
Print Unit1(i)\x
Print Unit1(i)\y
Print Unit1(i)\z
Print ""
Next

WaitKey()


Note that this example creates only 1 array called "Unit1",
it holds the handles (memory-addresses) of the types "t_unit_loc",
and has 5 indexes.

When the printout comes, you see the data in the entire array.
Each block of three lines displays the x,y and z values of each index (you can see that there are 5).
Actually the array has 6 indexes (0-5), but I haven't used index 0.


The r0nin(Posted 2004) [#4]
Thanks! Both of those posts really helped clear it up for me! My problem was where to put the parenthetical array reference...


The r0nin(Posted 2004) [#5]
Hmmmm. This opens a different can of worms.

Let's say that I have a player and 3 enemys, all of type t_Tank on a chessboard. If I wanted to store the values of the next five moves they made, I couldn't just use an array of Types, could I? For example, the first array (call it "board_position"), even if it had 5 spaces in it's array, couldn't be stepped through using For-Each, could it.

Here's what I'm thinking:

const NUM_VEHICLES=4   ;total number of tanks on the screen

Type t_board_position  ;making the Type to hold the 5 moves on the chessboard
    Field x,y,z
End Type

Dim board_position.t_board_position(5)   ;sets up the board position holders

For tank.t_tank = Each t_tank   ;runs through each tank
    For x=1 to 5
        board_position.t_board_position(x) = new t_board_position  ;sets up the 5 separate board holdes for each
    Next
Next

;OK, now I'm stuck.  How do I know which board_holder to
;use for which tank?  Can I just For-Each my way through 
;them, or would I First-Next?  Something like:

board_position.t_board_position = First board_position

For tank.t_tank = Each t_tank
    For num=1 to 5
    board_position(num)\x=12  ;whatever I wanted to put in
    board_position(num)\y=12
    board_position(num)\z=12
    Next

    board_position= After board_position
Next


Would that work?


big10p(Posted 2004) [#6]
If I understand what you want to do correctly, I'd do it like this:

	Type tankT
		; Put any other tank fields you require here!
	    Field x#[4]	; Arrays to store tank positions:
		Field y#[4]	; x[0],y[0],z[0] is tank's current position,
		Field z#[4]	; x[1],y[1],z[1] is tank's previous postion etc.
	End Type
	
	my_tank.tankT = New tankT
	position_tank(my_tank,1,2,3)	; Set tanks initial position.
	
	While Not KeyHit(1)
		Cls
		If KeyHit(57) Then position_tank(my_tank,Rand(1,100),Rand(1,100),Rand(1,100))
		Text 10,10,"  Current pos: x=" + my_tank\x[0] + " y=" + my_tank\y[0] + " z=" +  my_tank\z[0]
		Text 10,30,"Previous pos1: x=" + my_tank\x[1] + " y=" + my_tank\y[1] + " z=" +  my_tank\z[1]
		Text 10,50,"Previous pos2: x=" + my_tank\x[2] + " y=" + my_tank\y[2] + " z=" +  my_tank\z[2]
		Text 10,70,"Previous pos3: x=" + my_tank\x[3] + " y=" + my_tank\y[3] + " z=" +  my_tank\z[3]
		Text 10,90,"Previous pos4: x=" + my_tank\x[4] + " y=" + my_tank\y[4] + " z=" +  my_tank\z[4]
		Text 10,120,"Presss SPACEBAR to move tank to new position"
		Flip
	Wend
	End
	
Function position_tank(tank.tankT, x#, y#, z#)

	For i = 4 To 1 Step -1
		tank\x[i] = tank\x[i-1]
		tank\y[i] = tank\y[i-1]
		tank\z[i] = tank\z[i-1]
	Next
	
	tank\x[0] = x
	tank\y[0] = y
	tank\z[0] = z
	
End Function



The r0nin(Posted 2004) [#7]
OK... I never thought of doing it that way. But it should work, and probably better than the other way I was trying. The only reason I was trying to use types was because the dynamic memory allocation might help with the overhead (less memory used as the number of "tanks" decrease), but your method has that as well, but all in one type. Thank you!!!!