Makeing instances and updating them.

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

Ok I've read all tutorials and I'm still confused. I payed my 80 dollars for this powerfull engine so I would like to get just the basics down.

Let me set the scene:
I used to be a HARDCORE Game Maker programmer user so converting to BlitzMax presents some hurdles for me.YOU see in Game Maker you simply premade each object and you used a one line command to make new instances of the object.

The one glaring problem I'm having is wrapping my mind around maintaining instances through the "Tlists".

I know you make a type and that sets the general framework for all objects created from that type. I know you have to save each instance into a list and you must update the lists using a "for" loop. I also realize that you must code a deletion function to remove the instance from the list when you no longer want it to exist.

Could someone please show me how to create an instance of a type object and how to properly make a holding list, and maintain instances of the objects in the holding list?
Please comment on everything I just try and fail, try and fail. I simply don't understand how to declare and create instances and maintain their update functions.
Please assist if you can. I'm REALLY excited about learning more BlitzMax.

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.

Thanks, this helps alot. If I have any problems implimenting it into my game I'll tell you.

ok here is my bullet type it looks good to me but it creates an error saying type global initializers must be constant.

Type Bullet
Field x,y,direction,speed
Global Bullet_List:TList = CreateList()

Function create(x,y,direction,speed)
temp_shot:Bullet= New Bullet
temp_shot.x=x
temp_shot.y=y
temp_shot.direction=direction
temp_shot.speed=speed
ListAddLast(Bullet_List,temp_shot)
EndFunction

Function draw()
Local temp_shot:Bullet=Bullet(Bullet_List.last())
SetColor 235,200,124
DrawOval 32,32,x-16,y-16
EndFunction

Function update()
Local temp_shot:Bullet=Bullet(Bullet_List.last())
temp_shot.draw()
' make the x and y move
temp_shot.x:+ speed*Cos(direction)
temp_shot.y:+ speed*Sin(direction)
EndFunction

Function destroy()
ListRemove(Bullet_List,Self)
EndFunction

EndType

Just out of curiosity why are you using functions to update and draw your Bullets. You really should look at methods.

If you are adding them to a list you can just cycle through the list with Eachin and update and draw them that way.

I toyed with your code to give you an example.

Type Bullet
	Field x,y,direction,speed
	 
	Global Bullet_List:TList = New Tlist
	 
	Function create(x,y,direction,speed)
		Local temp_shot:Bullet= New Bullet
			temp_shot.x=x
			temp_shot.y=y
			temp_shot.direction=direction
			temp_shot.speed=speed
		ListAddLast(Bullet_List,temp_shot)
	EndFunction

	Method  Draw()
		SetColor 235,200,124
		DrawOval X-8,Y-8,16,16
	End Method 

	Method update()
		draw()
		x:+ speed*Cos(direction)
		y:+ speed*Sin(direction)
		If Y<0
			Destroy()
		End If 
	End Method 

	Method destroy()
		ListRemove(Bullet_List,Self)
	End Method 
EndType

Graphics 640,480
Repeat 
	Cls
	If KeyHit(Key_Space)
		Bullet.Create(Rand(640),440,270,2)
	End If 
	For Local B:Bullet=EachIn Bullet.Bullet_List
		B.Update()
	Next 
	Flip
Until KeyHit(Key_Escape)



the code and still got the same error.
I seem to get an error is this just my computer?
( I hope you aren't annoyed by my slow learning curve)

I think you need a BlitzMax update, see the first thread in the BlitzMax Updates and Betas forum for download links.

Thanks it appears to work fine now.

Ryan,

No problem on my end....It took me and is still taking me time to get my head around OOP. It turns out that it is a real time saver and programming follows my thought process better instead of having to rethink an Idea to program it.

So if you need any help I would be more than happy to help in my limited way. :)

Best Regards,
Eric