Calling method within class..

Monkey Forums/Monkey Programming/Calling method within class..

ErikT(Posted 2011) [#1]
I'm trying to convert Pete Rigz' collision module from Blitzmax. Almost there but there's still one problem.

This generates an error in the last line: "module 'tlMatrix2 not found"

Class tlBox

	Field vertices:tlVector2[]
	Field tformvertices:tlVector2[]
	Field normals:tlVector2[]
	Field tformmatrix:tlMatrix2 = New tlMatrix2.Create()


Fair enough. In most cases I can separate the New tlMatrix2 creation from the Create method like so:

	tformmatrix:tlMatrix2 = New tlMatrix2
	tformmatrix.Create()


Except that in this case the Create method is called within the same class. This throws the following error in the last line: "Syntax error - expecting class member declaration."

Class tlBox

	Field vertices:tlVector2[]
	Field tformvertices:tlVector2[]
	Field normals:tlVector2[]
	Field tformmatrix:tlMatrix2 = New tlMatrix2
	tformmatrix.Create()



What can I do to fix this? Help appreciated.


JIM(Posted 2011) [#2]
Simple.

Make a method called "New" in tlBox (if there isn't one already there) and place the call there:

Method New()
    tformmatrix.Create()
End



ErikT(Posted 2011) [#3]
Okay, how about if I have other Field declarations of different types calling the Create method a bit further down?

	Field boxoffset:tlVector2 = New tlVector2
	boxoffset.Create(0, 0)
	Field scale:tlVector2 = New tlVector2
	scale.Create(1, 1)
	Field velocity:tlVector2 = New tlVector2
	velocity.Create(0, 0)


EDIT: Ok I plopped them all into the New() method. So far so good. Thanks for the help :)


JIM(Posted 2011) [#4]
As far as I can tell, the "Create" method was a workaround for custom constructors.

You can clean the code up if you use the "New" method instead. That means that instead of "Create" you should just call the method "New" in those classes (tlMatrix2, tlVector2d, etc.).

Blitzmax only supported default constructors ("New()"). Monkey supports custom constructors ("New()", "New(int, int)", "New(float, int, string)", etc.). If you create custom constructors for your classes, that should be much cleaner and easier to work with.

Your code will look like this:

	Field boxoffset:tlVector2 = New tlVector2(0, 0)
	Field scale:tlVector2 = New tlVector2(1, 1)
	Field velocity:tlVector2 = New tlVector2(0, 0)


Also, as far as I know, monkey does not accept initializing fields in the declaration. This should NOT work:

Type Test
	Field A:Int = 5
End


If you make a new instance, it should have the field A either undefined or 0.

The correct way to do it is:

Type Test
	Field A:Int

	Method New()
		A = 5
	End
End



Samah(Posted 2011) [#5]
Also, as far as I know, monkey does not accept initializing fields in the declaration. This should NOT work:

Monkey does support it. The catch is that if you don't access the field anywhere in your code, the monkey compiler strips it out.

Function Main:Int()
	Local t:Test = New Test
	Print "t.a="+t.a
	Return 0
End

Class Test
	Field a:Int = GetValue()
	
	Function GetValue:Int()
		Print "blah"
		Return 5
	End
End

Prints "blah" and "t.a=5"

If you comment out the Print call in Main, it doesn't print anything. If you move the a = GetValue() assignment into the default constructor for Test, it will correctly print "blah".


ErikT(Posted 2011) [#6]
Hmm that's interesting. I tried to put all the code from the Create method into the New method. It looks like this now:

	#rem
		bbdoc: Create a New #tlBox
		returns: New tlBox
		about: Creates a New Bounding box that you can use For collision checking And adding To a #tlQuadTree. The x And y coordinates represent
		the top left corner of the bounding box. You can also assign some data To the boundary as handy way To store some extra info about the boundary.
	#End
	Method New(x:Float, y:Float, w:Float, h:Float, layer:Int = tlLAYER_1, Data:Object = Null)
		If w < 0
			x+=w
			w = Abs(w)
		End If
		If h < 0
			y+=h
			h = Abs(h)
		End If
		vertices = New tlVector2[4]
		handle.x = w / 2
		handle.y = h / 2
		vertices[0] = New tlVector2(-handle.x, -handle.y)
		vertices[1] = New tlVector2(-handle.x, h - handle.y)
		vertices[2] = New tlVector2(w - handle.x, h - handle.y)
		vertices[3] = New tlVector2(w - handle.x, -handle.y)
		normals = New tlVector2[4]
		tformvertices = New tlVector2[4]
		For Local c:Int = 0 To 3
			normals[c] = New tlVector2(0, 0)
			tformvertices[c] = New tlVector2(0, 0)
		Next
		handle.x = 0
		handle.y = 0
		tl_corner = New tlVector2(0, 0)
		br_corner = New tlVector2(0, 0)
		world = New tlVector2(x + w / 2, y + h / 2)
		UpdateNormals()
		TForm()
		collisionlayer = layer
		_data = Data
		Return Self
	End Method



But I get greeted with an error when I compile for a line above it:

Field handle:tlVector2 = New tlVector2(0, 0)


The error reads: Unable to find overload for New(Int, Int). Converting to float makes no difference.


Samah(Posted 2011) [#7]
Silly question, but is there a Method New(Int,Int)?
Show us your tlVector2 constructors. :)


therevills(Posted 2011) [#8]
BTW Erik, you dont need to return self in constructors...

Strict

Function Main:Int()
	Local foo:Foo = New Foo()
	Local bar:Foo = New Foo(200, 200)
	Print foo.x
	Print bar.x
End

Class Foo
	Field x%, y%
	
	Method New()
		x = 100
		y = 100	
	End
	
	Method New(x%, y%)
		Self.x = x
		Self.y = y
	End
End



ErikT(Posted 2011) [#9]
@Samah
Er, not so silly actually :) I forgot all about the fact that several classes used a Create method. Now it's fixed and everything works! Collisions work, drawing debug shapes work, yippee!

Will clean it up a bit, do a small example and then release. Thanks all :)


caffeinekid(Posted 2013) [#10]
What does "Syntax Error: expecting class member declaration" even mean and what does it apply to because I'm getting it and it doesn't give you a clue to what it's even referring to, just gives you number of the final line and doesn't point you in the right direction.


Jesse(Posted 2013) [#11]
did you forgot to include "Function Main" in your main file or are you running your main file or did you miss spelled "Main"?


skid(Posted 2013) [#12]
What can I do to fix this? Help appreciated.


You just need to change new Matrix.Create() to new Matrix().Create()