Methods
BlitzMax Forums/BlitzMax Beginners Area/Methods
| ||
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 ! |
| ||
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? |
| ||
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 ^_^ |
| ||
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. |
| ||
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? :) |
| ||
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.... |
| ||
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. |
| ||
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.. |
| ||
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... |
| ||
I don't, I prefer them as part of the type so I can find them easily, although I see your point. |
| ||
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. |
| ||
strangly - I just posted a newbie tutorial on my site that touches on this! |
| ||
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 ! |
| ||
Congratulations! :)![]() |
| ||
nice badge! :p |
| ||
Hey I want one of those! |
| ||
Hehe, thanks a lot WendellM :oD |
| ||
Online badge-maker: http://www.officerstore.com/store/custom_shields/badge_category.cfm |