Passing objects

BlitzMax Forums/BlitzMax Beginners Area/Passing objects

Hi - I continue my scary journey into OOP! I'm trying to pass an object (an instance of a type if I understand it correctly) to a method. I'm then assigning that instance to another instance (if you are following this) within the ProcessButtons Method. However, when I debugstop after the currentScreen=Button.TargetScreen, the currentScreen=NULL which is obviously causing an error.

The code is below, is there a particular way to assign currentScreen another Type?

Essentially I want the player to click on a button, and depending on the button clicked will change the screen that is drawn (referenced by currentScreen). Thanks for any help as always.

Strict

Const	STATIC		=0
Const	ANIMATED	=1
Const	iScreenWidth = 800
Const	iScreenHeight =600


Graphics iScreenWidth ,iScreenHeight ,0


Type TScreen
	Field sName:String
	Field background:TImage 
	
	' Create a list of buttons belonging to the screen
	Field buttonList: TList
			
	'Creates a list of screens
	Global ScreenList: TList	
	
	Function UICreateScreen:TScreen(name:String, image:String)
		Local NewScreen:	TScreen
		NewScreen	=	New TScreen
		NewScreen.sName	=	name$
		NewScreen.background	=	LoadImage(image$)

		' Verfiy files exist and are of correct size
		Assert NewScreen.background Else "Cannot find file '"+image$+ "'."
		Assert NewScreen.background.width=iScreenWidth Else "Background: "+image$+" size does Not match screen width."
		Assert NewScreen.background.Height=iScreenHeight Else "Background: "+image$+" size does Not match screen height."
		If Not ScreenList Then ScreenList:TList = CreateList()
	
		' Once all checks are done add screen to list
		ScreenList.AddLast ( NewScreen)
				
		'Create a list of buttons which will be a part of this screen
		If Not NewScreen.buttonList Then NewScreen.buttonList:TList= CreateList()

		'This should return the object just created so we can access it at a later date
		Return NewScreen
	EndFunction
	
	Method DrawScreen()
		DrawImage background,0,0
	EndMethod
	
	Method AddButton(name:String, x:Int,y:Int, image:String, buttonType:Int, TargetScreen:TScreen)
		DebugStop
		Local NewButton:TButton
		NewButton= New TButton
		NewButton.sName$=name$
	
		NewButton.x=x
		NewButton.y=y
		NewButton.y=y
		NewButton.targetScreen=TargetScreen
		        
		SetMaskColor (0,0,0)
		If buttonType=STATIC
			NewButton.normal = LoadImage(image$)
		Else
			NewButton.normal = LoadAnimImage(image$,NewButton.iCellWidth,NewButton.iCellHeight,0,NewButton.iTotalFrames+1,DYNAMICIMAGE|MASKEDIMAGE)
		EndIf
		Assert NewButton.normal Else "Cannot find file '"+image$+ "'."
		
		buttonList.AddLast ( NewButton )
	End Method
	
	
	Method DrawButtonScreen()

		For Local Button:TButton = EachIn buttonList 
			DrawImage Button.normal,Button.x,Button.y
		Next
	EndMethod
	
	Method ProcessButtons()
		For Local button:TButton = EachIn buttonList
			If MouseX()>button.x And MouseX()<button.x+button.normal.width
				If MouseY()>button.y And MouseY()<button.y+button.normal.Height
					Print "Rollover option"
					If MouseDown(1)   
						Print "Clicked Option"
						DebugStop
							 currentScreen=button.targetScreen
					EndIf
				EndIf
			EndIf
		Next
	EndMethod
EndType


Type TButton
	Field sName:String
	Field x:Int
	Field y:Int
	Field normal:TImage							'This will be a anim image. If we only want it to be a static image only have one frame
	Field targetScreen:TScreen=Null
	Field iCellWidth:Int=169  
	Field iCellHeight:Int=75
	Field iTotalFrames:Int=0					'Frames must be set to 0 if there is only 1 frame
		

EndType

                   
' So first what we do is initialise all the screens via this method. And the code will determine which screen to draw
Global MainMenu: TScreen
Global HelpScreen: TScreen  
Global currentScreen:TScreen

'DebugStop
MainMenu=TScreen.UICreateScreen("MainMenu","background.png")    
MainMenu.AddButton("Help", 100,200, "click.png", STATIC,HelpScreen)


HelpScreen=TScreen.UICreateScreen("Options","background2.png")
HelpScreen.AddButton("Test", 100,200, "click.png", STATIC,MainMenu)

Print "Number of allocated screens: "+CountList(TScreen.ScreenList)
            

 
currentScreen=MainMenu   'Set default screen to draw
While Not KeyDown(KEY_ESCAPE)
	 
 currentScreen.DrawScreen()
 currentScreen.DrawButtonScreen()   
 currentScreen.ProcessButtons()
 
   
 DrawText "Memory Used: "+(GCMemAlloced()/1024)+" KB",10,550
	 
	Flip
	Cls
Wend



Ok the problem lies within the addbutton method. When I pass an object to it, the object passed is NULL. Still dont understand why though

MainMenu.AddButton("Help", 100,200, "click.png", STATIC,HelpScreen)

This line comes before HelpScreen has been created - i.e: HelpScreen=NULL will be passed to the function.

D'oh. How embarrassing! Thanks!

Nah happens to everyone