Methods

BlitzMax Forums/BlitzMax Beginners Area/Methods

wedoe(Posted 2006) [#1]
I'm really sweating and have a headache over "Methods", I can't see
the difference between "Method" and "Function", could someone explain
it like if you were talking to a blonde or something....

When looking at example code I see sometimes the one is used
and sometimes the other and I can't wrap my head around what
is the reason for the choices.

I'm very well familiar with functions !


LarsG(Posted 2006) [#2]
methods require you to have made an instance of the object first.. (normally with the new command)..
functions works even though you havent made an object first.

did that make sense?


CS_TBL(Posted 2006) [#3]
methods belong to a type instance, functions don't need to belong to type instances..

observe:
type bah
  method poo()
    debuglog "poo"
  end method
  function pee()
    debuglog "pee"
  end method
end type

bah.pee

local yuk:bah=new bah

yuk.poo


orso .. :P hope it works, didn't really test orso ^_^


WendellM(Posted 2006) [#4]
And one of the implications of what LarsG mentions is that methods can automatically access the fields of the instance that they belong to, while functions can't. Functions can still access type instance fields, but you have to pass them in as parameters, whereas they're always available to methods.


Blitzer101(Posted 2006) [#5]
I tend to think of Methods as being part of a Type and that Types are used to create user defined Objects.

While I can see Types as possibly being more useful from a game making POV I'm also finding them very useful with regards to making a Module (eventually I hope!). I say this because effectively the Methods within my type/object are a child of the main object and therefore all the functions of that type quite clearly belong to that type because they all share the same parent object - I hope this makes sense? To do this (and it could be done) using purely functions would take this relationship away...

Using the Type/Object "commands"...

test% = date.IsLeapYear()
test2% = date.DayCount()

This is clear that both IsLeapYear and DayCount are part of the date object I've created, whereas...


Using Functions as "commands"...

test% = IsLeapYear()
test2% = DayCount()

Only does not really show that these two "commands" are related. If you get what I mean? :)


wedoe(Posted 2006) [#6]
If you'd been here you would have (almost) literally seen the lightbulb above my head light up :D
Thank's all, actually I had to read all the posts to "get it all", phew, one small step for a man....


Grey Alien(Posted 2006) [#7]
So far, I've only needed to use functions in types to actually create them. Everything else is methods as it acts on actual instances.


CS_TBL(Posted 2006) [#8]
And even that is a matter of choice, I personally prefer global create-functions as opposed to create-functions in a type, to be somewhat more uniform with the internal blitz commands..


bradford6(Posted 2006) [#9]
I use a create function in most types as a constructor.

I am sure there are other uses of functions within types but i am not aware of them.

here is one but the method seems like a better fit...




Grey Alien(Posted 2006) [#10]
I don't, I prefer them as part of the type so I can find them easily, although I see your point.


z80jim(Posted 2006) [#11]
The other day I used Function in a way that I haven't before and I'm pretty thrilled with the results.

I needed a routine to move an object across the screen. Various objects would need to do this. So instead of a global propel function I created a TProjectile Type. It has no fields, just the Propel function that receives a couple parameters.

My various objects instantiate TProjectile if they need to and call it.

I know I could actually create a TProjectile object with fields and all that but I didn't want to create a whole new object and have to create it and have fields etc. For the couple places Propel is used this was quick and simple.

Kind of like creating a TMath Type that could just contain some math functions.


Chris C(Posted 2006) [#12]
strangly - I just posted a newbie tutorial on my site that touches on this!


wedoe(Posted 2006) [#13]
What can I say, now that I finally "Get it" I feel
like THE MASTER OF METHODS, he he, and I really
can't see why I didn't get it before since it's
actually very simple when finally I wrapped my
head around it. Use them all the time already ! :o)
Like having a revelation I guess, thanks again all !

Thinking of the thousands of hours I have spent over
the years programming without Methods (or even types
before B3D) makes me a bit sad now, kinda, he he !


WendellM(Posted 2006) [#14]
Congratulations! :)




LarsG(Posted 2006) [#15]
nice badge! :p


Grey Alien(Posted 2006) [#16]
Hey I want one of those!


wedoe(Posted 2006) [#17]
Hehe, thanks a lot WendellM :oD


WendellM(Posted 2006) [#18]
Online badge-maker: http://www.officerstore.com/store/custom_shields/badge_category.cfm