Custom type embedded loop

Blitz3D Forums/Blitz3D Beginners Area/Custom type embedded loop

Chaduke(Posted 2008) [#1]
Is it possible to iterate through a list of a custom type with a "for each" loop and then do the same within that loop?

For instance, say I have a custom type called particle.

Type particle
Field x,y,w,h
End Type

Now I create a bunch of particles and I want to run collision routines on them.

for p.particle = each particle
for c.particle = each particle
if c <> p and rectsoverlap(p\x,p\y,p\w,p\h,c\x,c\y,c\w,c\h) then
; collision takes place
end if
next
next

I'm trying to do something like this but as soon as I reference the variable c I get an error about it not being a custom type. Is there a different way to do this?


Ross C(Posted 2008) [#2]
You should be able to do that fine. It must be something in your collision code. For instance, if you collide and end up deleting a type object, whilst the other is using it, it won't exist.

for instance:

p.particle = 4th object
c.particle = 3rd object

No collision

p.particle = 4th object
c.particle = 4th object

Collision! Delete c.particle!

This would delete the object p.particle is pointing to, therefore upon :

p.particle = 4th object
c.particle = 5th object

p.particle no longer exists. This problem will occur till the outer loop , p.particle, has moved on to the next object in the type list, assuming it hasn't been deleted that is.

To prove it works:

Type part
Field x,y
End Type


For loop = 1 To 200

	p.part = New part
	p\x = Rand(0,20)
	p\y = Rand(0,20)
	
Next


For p.part = Each part

	For c.part = Each part
	
		If c\x = p\x Then
			Print p\x
		End If
		
	Next
	
Next