quick question

BlitzMax Forums/BlitzMax Beginners Area/quick question

why doesn't this work??

'FIRST ATTEMPT AT BASIC OOP
Graphics 1280, 1024
SeedRnd(MilliSecs())

AutoMidHandle 1
Global List:TList = New TList
Type dude

	Field x, y, rot
	
	Method New()
		x = Rnd(0, 1280)
		y = Rnd(0, 1024)
		rot = Rnd(0, 360)
		List.AddLast(Self)
	End Method
	
	Method draw()
		For dude = EachIn List
			SetRotation(rot)
			DrawRect(x, y, 15, 15)
		Next
	End Method
	
	Method update()
		If KeyDown(KEY_LEFT) Then rot:-1
		If KeyDown(KEY_RIGHT) Then rot:+1
		draw()
	End Method

End Type

Global d:dude = New dude

While Not KeyHit(KEY_ESCAPE)

	d.update()
	Flip

Wend
End 


Because you haven't declared a variable called dude.

Local D:Dude
For D=EachIn List


oops, thanks!

so how can I do that except spawning multiple dudes, all of which are affected by the left/right arrow keys?
(sorry, totally new to bmax)

If you only want one dude then only create one.
If you want multiple then try having a create function and calling it multiple times.
You can get a lot from :
Beginners guide to BlitzMax
and Learning Object-Oriented Programming in Blitzmax


so why doesn't this work:
'FIRST ATTEMPT AT BASIC OOP
Graphics 1280, 1024
SeedRnd(MilliSecs())

AutoMidHandle 1
Global List:TList = New TList
Type dude

	Field x, y, rot
	
	Method New()
		x = Rnd(0, 1280)
		y = Rnd(0, 1024)
		rot = Rnd(0, 360)
		List.AddLast(Self)
	End Method
	
	Method draw()
	
		SetRotation(rot)
		DrawRect(x, y, 15, 15)
		
	End Method
	
	Method update()
		If KeyDown(KEY_LEFT) Then rot:-1
		If KeyDown(KEY_RIGHT) Then rot:+1
		Local d:dude
		For d = EachIn List
			draw()
		Next
	End Method
	
	Function Create:dude()
		Return New dude
	End Function

End Type

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

While Not KeyHit(KEY_ESCAPE)

	Cls
	d.update()
	Flip

Wend
End 


While Not KeyHit(KEY_ESCAPE)

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

Wend


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 :)

This is what I would do with your code :
1) Get used to using Superstrict it makes you define each variable. At first this is a pain but solves problems before they happen.
2) Move you Dude list into the Due type. It is more self-contained that way.
3) Have a Dude update function so you don't need to loop a list in the mainloop.
4) Have your create loop handle the number to create rather than your main code.

'FIRST ATTEMPT AT BASIC OOP
SuperStrict

Graphics 1280, 1024
SeedRnd(MilliSecs())

AutoMidHandle 1
Type dude
	Global List:TList = New TList

	Field x:Int, y:Int, rot:Int
	
	Function update()
		For Local all:dude=EachIn list
			all.update_each
		Next
	End Function
	
	Method New()
		x = Rnd(0, 1280)
		y = Rnd(0, 1024)
		rot = Rnd(0, 360)
		List.AddLast(Self)
	End Method
	
	Method draw()
	
		SetRotation(rot)
		DrawRect(x, y, 15, 15)
		
	End Method
	
	Method update_each()
		If KeyDown(KEY_LEFT) Then rot:-1
		If KeyDown(KEY_RIGHT) Then rot:+1
		Self.draw()
	End Method
	
	Function Create:dude(num_to_create:Int)
		For Local x:Int= 0 To num_to_create-1
			Local temp:dude = New dude
		Next
	End Function

End Type

dude.Create(100)

While Not KeyHit(KEY_ESCAPE)

	Cls
	dude.update()
	Flip

Wend
End