creating named "arrays"

Blitz3D Forums/Blitz3D Beginners Area/creating named "arrays"

chwaga(Posted 2008) [#1]
this probably sounds like a noob (at this for over a year and still a noob :D) question, but:

is there i way i could do something like this:

for n = 0 to 10
for m = 0 to 10
;add an object to a type, with a numerical sub-name (#.type) (# being n*m), so that i can connect them together into a graph??
next
next

dense plane creation algorithm, anyone?


Billp(Posted 2008) [#2]
will something like this work, I assume you want to access the data via the position n*m

for n = 0 to 10
for m = 0 to 10
array.type(n*m)\something = something
next
next


chwaga(Posted 2008) [#3]
I need it to work for the array. part. when i tried it, it didn't work


Ross C(Posted 2008) [#4]
You can have an array of types, but i'm sure you have to use blitz arrays. Could be wrong. I'm sure the forums have contained a good number of example on this.

In fact reading over your post i think i'm way off. Can you further explain using what you think would be the way you want it to work, then maybe we can suggest an alternate way?

like:


type waypoint
  field x[10] ; blitz array
  field y[10]
end type

w.waypoint = new waypoint

for loopx = 0 to 10
   for loopy = 0 to 10
      w\x[loopx] = loopx * 10
      w\y[loopy] = loopy * 10
   next
next




chwaga(Posted 2008) [#5]
i'm trying to make a tile of nodes, so I can connect them to eachother in a plane-like format (polyplane?).


Ross C(Posted 2008) [#6]
You could create an array representing all your blocks. Then create a type collection.

Type link
   field from_x, from_y
   field to_x, to_y
End type


Anytime you click a tile/node/box, you create a new type, and set the from_x and from_y field to the co-ords. Then on the second click, you input the destination co-ords into the "to_x" and "to_y" fields.

Then all you need to do is loop through the types and draw a line between them.


Ross C(Posted 2008) [#7]
Ok, here's what i conjured up. It might not be what your after though:

Graphics 800,600
SetBuffer BackBuffer()

Const box_size = 32
Dim boxes(10,10) ; second dimension holds x,y co-ords - 0 = x, 1 = y

Global box = CreateImage(box_size,box_size)
SetBuffer ImageBuffer(box)
	Rect 0,0,box_size,box_size
	Color 50,50,50
	Rect 0,0,box_size,box_size,0
SetBuffer BackBuffer()


Type link
	Field from_x,from_y
	Field to_x, to_y
	Field complete ; 1 = link is complete. 0 = link is waiting for destination co-ords.
End Type


Global start_draw_x = 100
Global start_draw_y = 100
Global mode = 0 ; 0 = first pick
				; 1 = next pick

				
Global first_click = 1

Global pos_x,pos_y

While Not KeyHit(1)



	If MouseHit(1) Then
		If mode = 0 Then ; if first click basically
			If check_click(MouseX(),MouseY()) = True Then
				set_start_point(pos_x, pos_y)
				mode = 1
			End If
		ElseIf mode = 1 Then
			If check_click(MouseX(),MouseY()) = True Then
				add_link(pos_x, pos_y)
			End If
		End If
	End If
	
	draw_boxes()
	draw_links()
	Flip
	
Wend

Function draw_boxes()


	For loopx = 0 To 10
		For loopy = 0 To 10
			DrawImage box, start_draw_x + (loopx*box_size), start_draw_y + (loopy*box_size)
		Next
	Next
	
End Function

Function draw_links()



	For l.link = Each link
	
		If l\complete = 1 Then
		
			Color 100,100,255

			; calculate pixel co-ords. Add box_size * 0.5 so links come from the centre of boxes.
			start_x = start_draw_x + (l\from_x * box_size) + (box_size*0.5)
			start_y = start_draw_y + (l\from_y * box_size) + (box_size*0.5)
			finish_x = start_draw_x + (l\to_x * box_size) + (box_size*0.5)
			finish_y = start_draw_y + (l\to_y * box_size) + (box_size*0.5)
			
			Line start_x,start_y,finish_x,finish_y
			
		Else ; draw a marker for the last position
			
			Color 0,0,0
			start_x = start_draw_x + (l\from_x * box_size) + (box_size*0.5)
			start_y = start_draw_y + (l\from_y * box_size) + (box_size*0.5)
			Oval start_x-1, start_y-1, 2,2
			
		End If
				
	
	Next
	
End Function


Function check_click(x,y)

	For loopx = 0 To 10
		For loopy = 0 To 10
			If RectsOverlap(x,y,1,1, start_draw_x + (loopx*box_size) , start_draw_y + (loopy*box_size), box_size,box_size) Then
				pos_x = loopx
				pos_y = loopy
				Return True
			End If
		Next
	Next
	
End Function

Function set_start_point(x,y)

	l.link = New link
	l\from_x = x
	l\from_y = y
	l\complete = 0
	
End Function

Function add_link(x,y)

	l.link = Last link
	
	l\to_x = x
	l\to_y = y
	l\complete = 1
	
	l.link = New link
	
	l\from_x = x
	l\from_y = y
	l\complete = 0
	
End Function


It basically start with the mode set to 0. This is a one time only event. Once you have clicked to set a position, it creates a type and inputs the start co-ords. For every other click, it inputs the end co-ord into the type object, and create a new link, type object, with the same end co-ords as the last type. This is done in the "add_link" function.