method or function in types
BlitzMax Forums/BlitzMax Beginners Area/method or function in types
| ||
When does one use a method over a function in a type or vice versa? I see example where a type has both methods and function and I done understand why when it appears a function can do what the method is doing. |
| ||
A method belongs to a type instance. A function does not. This is fine: myType.Foo() Type myType Function Foo() Print "Bar" End Function End Type This is not: myType.Foo() Type myType Method Foo() Print "Bar" End Method End Type ...instead... Local m:myType = new myType 'create a type instance m.Foo() Type myType Method Foo() Print "Bar" End Method End Type |
| ||
Ok that explains why the 'create' item is always a function in the examples I look at because they are creating a instance not working with a instance. |
| ||
Yep, you can use a function to create an instance - particularly useful if you need to pass parameters at the time of creation (which you can't do using the New() method), note you don't *have* to call the function "Create", i.e:Local m:myType m = myType.init(50,100) m.Foo() Type myType Field x:int Field y:int Function init:myType(x:int, y:int) Local m:myType = new myType m.x = x m.y = y Return m End Function Method Foo() Print "X: " + Self.x Print "Y: " + Self.y End Method End Type One more thing you might want to know, about globals in Types. The code below is the same as above, except this type class keeps an internal counter of how many type instances have been created: Local m:myType m = myType.init(50,100) m.Foo() Type myType Global instancesCreated:Int Field x:Int Field y:Int Function init:myType(x:Int, y:Int) Local m:myType = New myType m.x = x m.y = y Self.instancesCreated:+1 'Self here refers to the type itself (cos we're in a function) Return m End Function Method Foo() Print "X: " + Self.x 'Self here refers to the instance of the type (cos we're in a method) Print "Y: " + Self.y Print "Instances created: " + myType.instancesCreated End Method End Type Globals and Functions belong to the type class. Fields and Methods belong to an instance of the type class. |
| ||
can that global be accessed by anyone or only from the type? |
| ||
Within the Type... it exists for all instances... I use it for Caching Particles for example... Type Particle ' accessed by all Particles Const PID_Missile:int=0 Const PID_Explosion:int=1 ' added for fluf Global CacheSize:int=1200 Global ParticleCache:Particle[CacheSize] Global ParticleCacheCT:int=0 ' accessed only by instance Field x:float=0 Field y:float=0 Field ID:int=PID_Missile ' accessed through instance Method PushParticle() x=0 ' access to instance fields y=0 ID=PID_Missile '... End Method Method SetPosition(x:float,y:float) self.x=x ' self reference needed because x is out of scope due to passed variable self.y=y End Method ' accessed through type or instance Function PopParticle:Particle() '... End Function End Type |
| ||
I just posted that above and its showing posted 2 days ago |
| ||
I just posted that above and its showing posted 2 days ago Indeed. Didn't you know, New Zealand is in a time-warp? :-p |
| ||
I just posted that above and its showing posted 2 days ago Posting times are always off every time a new month starts. It'll be back to normal in a day or two. |