Issue inheriting TGadget properties

BlitzMax Forums/MaxGUI Module/Issue inheriting TGadget properties

Saxon1974(Posted 2007) [#1]
Hello,

I am new to Blitzmax so Im still learning.....anyway.

I am trying to create a Tile map editor gui using Maxgui. Im am creating a type for a window and then I am trying to create a canvas based on the dimensions of that window object.

The problem is that it appears that the MainWindow object I created is null when I try and use a gadget function on it.

If you create an object that extends the base Tgadget class doesnt it inherit all of its properties\methods\functions?

Once it gets to the line of code to create the canvas it says attempt to access field or method of null object.

Here is my code;

Global MainWindow:TNewWindow = New TNewWindow
MainWindow.Create("The Main Window",wx,wy,GAME_WIDTH,GAME_HEIGHT,MainWindow)

Global newcanvas:Tgadget= CreateCanvas(0,0,ClientWidth(Mainwindow),ClientHeight(Mainwindow),Mainwindow)

Repeat
Mainwindow.UpdateState()

Until AppTerminate()

End

Type TMyGadget Extends TGadget

Field Tgad:TGadget
Field Tgadx:Int
Field Tgady:Int
Field Tgadh:Int
Field Tgadw:Int
Field TgadParent:Tgadget

'Method UpdateState() Abstract



End Type

Type TNewWindow Extends TMyGadget

Field Tgad:TGadget

Method Create(wCaption:String,wX:Int,wY:Int,wWidth:Int,wHeight:Int,wParent:TNewWindow)

Tgad= CreateWindow(wCaption,wX,wY,wWidth,wHeight,,WINDOW_TITLEBAR | WINDOW_RESIZABLE | WINDOW_MENU | WINDOW_STATUS)
'Local text$= "My current size (w,h) is " + ClientWidth(Tgad)+ "," + ClientHeight(Tgad)
'SetStatusText Tgad, text$


End Method


Method UpdateState()

'AddHook EmitEventHook, Mainhook


WaitEvent()
Select EventID()

Case EVENT_WINDOWCLOSE
' FreeGadget TileCanvas
End

' Case EVENT_WINDOWSIZE
'
' ResizeWindows


' Case EVENT_MOUSEMOVE



' Case EVENT_GADGETPAINT
' SetGraphics CanvasGraphics(Maincanvas)
' SetViewport 0, 0, ClientWidth(Mainwindow), ClientHeight(Mainwindow)
' Cls

Case EVENT_APPTERMINATE
End
End Select

'Flip

End Method

End Type


Saxon1974(Posted 2007) [#2]
Sorry, I didnt type the error fully.

It says Unhandled exception:attempt to access field or method of null object failed


impixi(Posted 2007) [#3]
You need to initialise several variables. Add this to the beginning of your code:

SuperStrict

Local wx:Int = 10
Local wy:Int = 10
Local GAME_WIDTH:Int = 640
Local GAME_HEIGHT:Int = 480



It should then compile without errors.

Tip: Use SuperStrict or Strict in all your projects.


Saxon1974(Posted 2007) [#4]
I guess I should have included the whole code:)

I did already have this at the top

SuperStrict

Global GAME_WIDTH:Int=800
Global GAME_HEIGHT:Int=600

Local wx:Int=(GadgetWidth(Desktop())-GAME_WIDTH)/2
Local wy:Int=(GadgetHeight(Desktop())-GAME_HEIGHT)/2


skidracer(Posted 2007) [#5]
Don't extend TGadget (it is already extended by the platform gui driver such as TWin32Gadget TCocoaGadget etc.)

Just store the reference returned by CreateWindow etc. in your objects.


H&K(Posted 2007) [#6]
SuperStrict

Global GAME_WIDTH:Int=800
Global GAME_HEIGHT:Int=600

Local wx:Int=(GadgetWidth(Desktop())-GAME_WIDTH)/2
Local wy:Int=(GadgetHeight(Desktop())-GAME_HEIGHT)/2 
Global MainWindow:TNewWindow = New TNewWindow
MainWindow.Create("The Main Window",wx,wy,GAME_WIDTH,GAME_HEIGHT,MainWindow)

Global newcanvas:Tgadget= CreateCanvas(0,0,ClientWidth(Mainwindow.Tgad),ClientHeight(Mainwindow.Tgad),Mainwindow.Tgad)

Repeat 
Mainwindow.UpdateState()

Until AppTerminate()

End

Type TMyGadget 

Field Tgad:TGadget
Field Tgadx:Int
Field Tgady:Int
Field Tgadh:Int
Field Tgadw:Int
Field TgadParent:Tgadget

'Method UpdateState() Abstract



End Type

Type TNewWindow Extends TMyGadget



Method Create(wCaption:String,wX:Int,wY:Int,wWidth:Int,wHeight:Int,wParent:TNewWindow)

Tgad= CreateWindow(wCaption,wX,wY,wWidth,wHeight,,WINDOW_TITLEBAR | WINDOW_RESIZABLE | WINDOW_MENU | WINDOW_STATUS)
'Local text$= "My current size (w,h) is " + ClientWidth(Tgad)+ "," + ClientHeight(Tgad)
'SetStatusText Tgad, text$


End Method 


Method UpdateState()

'AddHook EmitEventHook, Mainhook 


WaitEvent()
Select EventID()

Case EVENT_WINDOWCLOSE
' FreeGadget TileCanvas
End 

' Case EVENT_WINDOWSIZE
' 
' ResizeWindows


' Case EVENT_MOUSEMOVE



' Case EVENT_GADGETPAINT
' SetGraphics CanvasGraphics(Maincanvas) 
' SetViewport 0, 0, ClientWidth(Mainwindow), ClientHeight(Mainwindow) 
' Cls

Case EVENT_APPTERMINATE
End
End Select

'Flip

End Method 



Saxon1974(Posted 2007) [#7]
Ah, that did it, thanks H&K! To everyone else as well for suggestions.

So since Tgad is a TGadget object thats part of Object TNewWindow and MAinWindow was created as a TNewWindow type, that allows us access to the TGadget functions of Tgad going through the high level qualifier of Mainwindow.Tgad. Interesting.

Does this really buy me anything doing it this way or make my code any better? I mean I could just use a basic Mainwindow:TGadget=createwindow etc....statement and then just use all the TGadget functions strait from there. That is simpler to do.

I guess it all depends on how I structure it, if I put all my basic Gadget properties in my main gadget object TMyGadget and derive all other gadget objects from that its a good thing because I dont have to recreate the entire object structure each time.

Anyways, just wondering as Im a bit new the Object Oriented way of doing this, but I wanna do things this way as I am assuming reusing as much code as possible means more efficient code during run time.

Thanks all!


Dreamora(Posted 2007) [#8]
I don't know what you are trying to achieve, but if you are after OO MaxGUI, NeolCover created a wrapper for that which you can find in the code archives when I remember correctly.


Saxon1974(Posted 2007) [#9]
Cool, I will take a look at that wrapper in the code section. I want to write my own app, but having some examples would be great.