quick question

BlitzMax Forums/BlitzMax Beginners Area/quick question

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

For n = 0 To 100
	d:dude = dude.Create()
Next

While Not KeyHit(KEY_ESCAPE)

	Cls
	d.update()
	Flip

Wend


Each time in the loop you create a new dude variable, d, and store in it a New dude. But each time in the loop you are overwriting d with a link to the next dude.

So after the loop you have overwritten d 99 times and the d variable contains a link to the last dude that you made. You probably noticed that only one dude updates? That is why. d does not contain all the dudes, only one. Each time you given a new dude, it forgets the old one.

(But the dude does still exist.. it's in the list, see below)

While Not KeyHit(KEY_ESCAPE)

	Cls
	For Local d:dude = EachIn List
		d.update()
	Next
	Flip

Wend


This will work because of the list you've made! Yipee!!

Each time you make a new dude it is added to the list. Unlike d, List can contain more than one dude - it has 100!

The for loop used by Htbaa shows how to access each item in the list. It tells the program to creatue a variable d, (which can only hold one dude remember, but that's ok because it's temp variable). For each dude in the list, it is stored temporarily in d. Then the update method is used on d. Because of the for loop, though, every dude is updated.

Hopefully I've explained this so that you can understand it. The key thing is to understand where all these dudes are going (in the list) and how this works, and exactly what each variable means.

In your for loop to create the dudes you could even just say dude.Create() on its own, without d:dude =, because you never actually use that d :)