Types, DIM and deleting

Blitz3D Forums/Blitz3D Beginners Area/Types, DIM and deleting

Cold Harbour(Posted 2004) [#1]
OK, hypothetical situation to illustrate my problem.

In this example I want 20 galaxies. Each galaxy has 100 aliens. Each alien has an X value.

All galaxies must be kept separate and I also need to carefully manage memory use so I don’t start with all the galaxies and aliens defined. Instead I start with just two and add more later. I also need to delete whole galaxies.

To do this I do the following.

Type alien
Field x
End Type

Dim galaxy.alien(20)

For f=1 To 100

	galaxy.alien(1) = New alien
	galaxy(1)\x = Rnd(0,100) 

	galaxy.alien(2) = New alien
	galaxy(2)\x = Rnd(0,100) 

Next

For galaxy.alien(1) = Each alien
	Delete galaxy.alien(1)
Next

For galaxy.alien(2) = Each alien

	DebugLog  galaxy(2)\x
	
Next

Delay 1000


In deleting galaxy.alien(1) I seem to also be deleting galaxy.alien(2).

Anyone see what I’m doing wrong? I only want to delete (1). Thanks a lot.


skn3(Posted 2004) [#2]
You know when you load an image you do this:

MyImage = Loadimage()


Now if you were to then do this:

MyImage = 1234


The image would then become lost because you altered the MyImage value that stored its location in memory.

The same thing applies to blitz types:

alien.aliens = new aliens


This would create an alien, then if you were to do this:

alien.aliens = new aliens


It would create a new alien, but change the "alien.alien" value so the first one gets lost.

Now to bring that into your example:

Dim galaxy.alien(20)


This line will create 20 "values" each of which can point to only 1 alien at a time. As described above, the moment you overwrite one, it becomes lost.

The way you might want to do it is like so:

Type galaxy
	Field a.alien[100]
End Type

Type alien
	Field name$
End Type

;create some galaxies
For i = 1 To 2
	g.galaxy = New galaxy
	;create some aliens for this galaxies
	For ii = 1 To 100
		g\a.alien[ii] = New alien
		g\a.alien[ii]\name$ = "bob the alien"
	Next
Next



Cold Harbour(Posted 2004) [#3]
Thanks a lot skn. Really appreciate that post.

So then I delete the first galaxy with
g.galaxy = First galaxy
Delete g.galaxy

Cool.