Creating array of an user-defined type?

BlitzMax Forums/BlitzMax Beginners Area/Creating array of an user-defined type?

NickFalk(Posted 2007) [#1]
Hi there, just got my hands on BlitzMax and feel like I've returned home. (Used to be an avid Blitz Basic -fan way back in the Amiga days). I've finally seen the light when it comes to OOP, as I've had a hard time seeing the benefits earlier. Well enough with the chit-chat, my virgin post, and first "dummie"-question is as follows:

I want to create a star-field by making an array of the type "star" which contains just the x and y positions plus a "draw" method. At the moment my code looks like this:

Graphics 1024,768,0,0

Type tstar
Field x : Int
Field y : Int
Method draw()
Plot x,y
End Method
End Type

Global star:tstar[]
star=New tstar[100]

For counter = 0 To 99
star[counter].x = rand (1, 1024)
star[counter].y = rand (1, 768)
star[counter].draw()
Next

Flip

Repeat Until KeyDown (key_escape)


Checks out OK until I try running it, returning the error-message: "Unhandled Exception: Attempt to access field or method of Null object."

Oh, and BlitzMax 1.24 - Intel-Mac OSX 10.4.9


Gabriel(Posted 2007) [#2]
For counter = 0 To 99
star[counter].x = rand (1, 1024)
star[counter].y = rand (1, 768)
star[counter].draw()
Next


Should be

For counter = 0 To 99
star[counter]=New tstar
star[counter].x = rand (1, 1024)
star[counter].y = rand (1, 768)
star[counter].draw()
Next


You've initialized the array, but not each object.


NickFalk(Posted 2007) [#3]
Ah, thanks a billion, still a bit fresh when it comes to types, instances objects etc.

Cheers! :)