Extender Methods mean Polymorphism!: Difference between revisions

Link to online manual
*>Monkey 05 06
(Okay so I lied. Also I expanded on some ideas.)
(Link to online manual)
 
Line 1: Line 1:
So you think AGS doesn't support polymorphism? Well up until AGS 3.0 that would be a correct assumption. Okay we may be getting a tad bit ahead of ourselves here. Just what is this "polymorphism" anyway?
So you think AGS doesn't support polymorphism? Well up until AGS 3.0 that would be a correct assumption. Okay we may be getting a tad bit ahead of ourselves here. Just what is this "polymorphism" anyway?


Polymorphism is a concept specifically dealing with data structures ({{link|Script language keywords|struct}}s in AGS), inheritance, overloading, and other concepts which may seem largely foreign.
Polymorphism is a concept specifically dealing with data structures [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags44.htm#struct structs] in AGS), inheritance, overloading, and other concepts which may seem largely foreign.


===Inheritance===
===Inheritance===
Inheritance is the idea that we're going to define a struct that ''inherits'' functions and properties from another type. For an example of this we can look at AGS's built-in {{link|GUI control functions and properties|GUIControl.AsType|GUI control}} types. Each of these types ''derive'' functions and properties from the ''base'' [[GUI control functions and properties|GUIControl]] type.
Inheritance is the idea that we're going to define a struct that ''inherits'' functions and properties from another type. For an example of this we can look at AGS's built-in [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags56.htm#GUIControl.AsType GUI control] types. Each of these types ''derive'' functions and properties from the ''base'' [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags56.htm GUI control] type.


For example if we had a struct defined such as:
For example if we had a struct defined such as:
Line 18: Line 18:
   };
   };


Although struct '''b''' only defines the property ''ib'' it will also have the property ''ia'' which is inherited by the ''extends'' keyword. This is where properties such as {{link|GUI Label functions and properties|Label.X}} and {{link|GUI Button functions and properties|Button.Width}} come from; they are inherited from the GUIControl type.
Although struct '''b''' only defines the property ''ib'' it will also have the property ''ia'' which is inherited by the ''extends'' keyword. This is where properties such as [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags59.htm Label.X] and [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags57.htm Button.Width] come from; they are inherited from the GUIControl type.


===Overloading===
===Overloading===
The idea of ''overloading'' is that you're going to have two (or more) functions with the ''same name''. So how then do you know which version of the function is going to get called? Well, this can be determined by a number of factors but AGS makes it easy. In AGS you can ''only'' overload a function via an [[Extender functions|extender method]].
The idea of ''overloading'' is that you're going to have two (or more) functions with the ''same name''. So how then do you know which version of the function is going to get called? Well, this can be determined by a number of factors but AGS makes it easy. In AGS you can ''only'' overload a function via an [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags38.htm extender method].


You can't define two extenders for the same struct with the same name. However what you can do is define two extenders with the same name for two different structs. So you could implement a Fade function for both the Character and Object types. Both functions are called Fade, but AGS would know which one you're calling just by how you call it. So if you put cEgo.Fade it would know to call the Character function, and oKey.Fade would call the Object function.
You can't define two extenders for the same struct with the same name. However what you can do is define two extenders with the same name for two different structs. So you could implement a Fade function for both the Character and Object types. Both functions are called Fade, but AGS would know which one you're calling just by how you call it. So if you put cEgo.Fade it would know to call the Character function, and oKey.Fade would call the Object function.