Makeing instances and updating them.

BlitzMax Forums/BlitzMax Beginners Area/Makeing instances and updating them.

Well first, welcome to the forum.

I've used game maker quite a bit recently and the way it handles object threw me so I can relate.

Incase you didn't already know instances of objects don't have to be stored in a list. May times it's useful to do so, but it's not required.

Here's a little list example:
Framework BRL.Basic

'A little dummy Type
Type MyObject
	Field num:Int
EndType

'Make a List
Local MyList:TList = New TList
Local temp:MyObject

'Populate it
For Local i:Int = 0 To 6
	temp = New MyObject
	temp.num = i
	MyList.AddLast(temp)
Next

'Loop through to test it
For temp = EachIn MyList
	Print temp.num
Next

'remove a specific one
For temp = EachIn MyList
	If temp.num = 3 Then MyList.Remove(temp)
Next

Print 'space
For temp = EachIn MyList
	Print temp.num
Next

'Clear the list
MyList.Clear()

'At this point we can still use the list, but I'm going to delete it
MyList = Null

'Now the list and everything in it is deleted or rather the memory they used is freed
'The variable is still useable though so we could make a new one.


Personally I like to keep my lists outside of the instances they hold. For example I would make an EnemyManager type that had a list to hold all my enemies. That way I could make more than one list of enemies if I wanted.

On the other hand you could put the list inside the type if you only want one list. Also you'll only want to do this for things that are always manipulated all at once. I think this is what you're wanting:
Framework BRL.Basic

Type MyObject
	Field num:Int
	Global List:TList
	
	Function Create:MyObject(n:Int)
		Local temp:MyObject = New MyObject
		temp.num = n
		Return temp
	EndFunction
	
	Method New()
		If MyObject.List = Null Then MyObject.List = New TList
		MyObject.List.AddLast(Self) 
	EndMethod

	Method Destroy()
		List.Remove(Self)
	EndMethod
	
	'Draw can be done the same way as update
	Method Update()
		'Do some updating
		num:+ 1
	EndMethod
	
	Function UpdateAll()
		For Local temp:MyObject = EachIn List
			temp.update()
		Next
	EndFunction
	
	Function PrintAll()
		Print 'space
		For Local temp:MyObject = EachIn List
			Print temp.num
		Next
	End Function
EndType

	
'Now we can use it like this
Local temp:MyObject
For Local i:Int = 0 To 9
	temp = New MyObject
	temp.num = i
Next

'Create functions can clean things up
MyObject.Create(17)
MyObject.Create(4)
MyObject.Create(1)

'Now we can do some standard demo stuff
MyObject.PrintAll()
MyObject.UpdateAll()
MyObject.PrintAll()


'We can still operate directly on the List
Local largestNum:Int = 0
Local largestObject:MyObject = Null
For temp = EachIn MyObject.List
	If temp.num > largestNum Then
		largestNum = temp.num
		largestObject = temp
	EndIf
Next

'kill the largest
largestObject.Destroy()
'prove it
MyObject.PrintAll()


This definetly isn't the end all be all list example, but hopefully you got something out of it.