Functions vs Methods

Monkey Forums/Monkey Programming/Functions vs Methods

vicente(Posted 2012) [#1]
So, I'll start a few newbie questions from now on.. first: Whats the difference between Functions and Methods?


slenkar(Posted 2012) [#2]
methods are part of a class, functions are global

methods can be overridden by other classes that extend the original class


vicente(Posted 2012) [#3]
I'm trying to call the method "update" of class Player from OnUpdate, but i got the following error when i run (it builds w/o error):

Monkey Runtimer Error : TypeError: Cannot call method 'm_update' of null


Global player:Player

...

Method OnUpdate:Int()	
	player.update()	
	Return 0
End

...

Class Player

	Method update:Void()


	End
	
	Method render:Void()

	End

End

If I change Method update:void() to Function update:void() it works, but i can't access class variables.

What could be wrong?


c.k.(Posted 2012) [#4]
Try something like

[monkeycode]
Global player:Player = New Player
[/monkeycode]

You could also put it in your OnCreate method.

[monkeycode]
Global player:Player

Method OnCreate:VOID()
player = New Player
End Method
[/monkeycode]

Probably.


vicente(Posted 2012) [#5]
I already have player = New Player on "onCreate" method


Origaming(Posted 2012) [#6]
it's strange
Monkey Runtimer Error : TypeError: Cannot call method 'm_update' of null

is cause you are not called
[monkeycode]player = New Player[/monkeycode]

take a look



Samah(Posted 2012) [#7]
@slenkar: methods are part of a class, functions are global

Careful with your terminology there. Global functions are not the same as static functions. Static functions also belong to a class, but they don't require an instance to call them.