Syntax Help with types in arrays

BlitzMax Forums/BlitzMax Beginners Area/Syntax Help with types in arrays

Rico(Posted 2008) [#1]
Hi. This is bugging me. Please Help! :) Why can't I do this? I want to be able to put items into the array 'points' - without having to do a 'pl[1]=New pt' previous to every entry. I have used this syntax with other types (in Box2D) and it works ok. Must be doing something simple wrong. Thanks.

Global pl:pt[]=New pt[10]

Type pt
	Field x:Int,y:Int
End Type

pl:pt[1].x=7
End


If I use the above syntax, I get a null object error.


Reactor(Posted 2008) [#2]
Are you sure you aren't confused? You're trying to reference a value in an empty array. You need to create an instance of the type in the array. I don't see how it'd be any different with Box2D.


Brucey(Posted 2008) [#3]
If you are referring to something like Query:Int(aabb:b2AABB, shapes:b2Shape[]), where you pass in an array defined to the size you want, but empty, then the populating of it is done by the module.

Otherwise, as Reactor says, you need to create a New instance for each entry.

Of course, to make your life a tad easier you can :

Type pt
	Field x:Int,y:Int

	Function Create:pt(x:int = 0, y:int = 0)
		Local this:pt = New pt
		this.x = x
		this.y = y
		Return this
	End Function
End Type

...

pl:pt[1] = pt.Create(7, 5)



Note : Defining default values for the parameters just makes those parameters optional, allowing you to do things like pt.Create()


Reactor(Posted 2008) [#4]
That's a cool idea :)


Rico(Posted 2008) [#5]
Yes I'm definitely confused :) You can do this in Box2D, but I can see its not really the same now.

Local sd1Vertices:b2Vec2[] = New b2Vec2[3]
Local p1:b2Vec2 = Vec2(-11.33333333,13.5)
Local p2:b2Vec2 = Vec2(-1.8,24.0)
Local p3:b2Vec2 = Vec2(-17.7555656565,23.6666666666)
sd1Vertices[0] = p1
sd1Vertices[1] = p2
sd1Vertices[2] = p3


Thanks for the help!