-
Notifications
You must be signed in to change notification settings - Fork 9
ExtenderFunctions
Extender functions allow you to "add" new functions to existing types, including built-in AGS types, which lets you call them as if they were part of that type.
Let's look at the following example:
function Scream(Character *c)
{
c.Say("AAAAARRRRGGGGHHHHHH!!!!");
}
This is a regular function that accepts a Character as a parameter, and makes it say something. You would call this function as:
Scream(cEgo); // pass cEgo character as a function parameter
Now, the extender function would instead be declared like:
function Scream(this Character*)
{
this.Say("AAAAARRRRGGGGHHHHHH!!!!");
}
Notice the this
keyword. It means that we are addressing a character, from which we called this function.
The extender function may now be called as:
cEgo.Scream(); // call the function Scream from character cEgo
Extender functions exist purely for convenience (in programming this is called "syntax sugar"). They do not add any new abilities, rather than to be able to call the function from an object instead of passing this object as a parameter. They use this
keyword to address the object they were called from. Besides that, extender functions are just like ordinary functions: they may have multiple parameters, and return some value.
The common use-case for extender functions is writing custom behavior for built-in types. Consider following, more sophisticated example:
function SayAndAnimate(this Character*, String text, int view, int loop)
{
this.LockView(view);
this.Animate(loop, 4, eRepeat, eNoBlock);
Overlay *bg_speech = this.SayBackground(text);
while (bg_speech.IsValid)
{
Wait(1); // keep updating the game
}
this.UnlockView();
}
player.SayAndAnimate("Hello, my name is Roger.", VROGERANIMS, 5);
Above will make character run a custom animation and display a speech (using Background speech to keep animation going in this example, but there may be other solutions to this).
NOTE: just like with any other functions that your write, remember to declare the function as import in the script header, if you like it to be usable in other scripts throughout your game. See "Importing a function" for more details.
Since AGS 3.4.0 you may also create static extender functions, that is functions that are called directly from a type, rather than an actual object of that type. Static extender declaration is a bit different, for example:
int AbsInt(static Maths, int value)
{
if (value < 0)
return -value;
return value;
}
and you use such function as:
int x = Maths.AbsInt(-10);
Getting Started in AGS
Editor
- New Game templates
- Editor Preferences
- General Settings
- Default Setup
- Colours Editor
- Room Editor
- Character Editor
- Cursor Editor
- Dialog Editor
- Font Preview
- GUI Editor
- Inventory Items Editor
- View Editor
- Sprite Manager
- Music and sound
- Voice speech
- Script Modules
- System limits
- Log Panel
- Plugins
- Other Features
Engine
Scripting
- Scripting Tutorial
- Scripting Language
-
Scripting API
- Script API Overview
- Standard Constants
- Standard Enumerated Types
- Standard Types
- Game variables
- Global arrays
- Global event handlers
- repeatedly_execute / repeatedly_execute_always
- Custom dialog options rendering
- Global functions: general
- Global functions: message display
- Global functions: multimedia actions
- Global functions: palette operations
- Global functions: room actions
- Global functions: screen effects
- Global functions: wait
- AudioChannel functions and properties
- AudioClip functions and properties
- Camera functions and properties
- Character functions and properties
- DateTime functions and properties
- Dialog functions and properties
- DialogOptionsRenderingInfo functions and properties
- Dictionary functions and properties
- DrawingSurface functions and properties
- DynamicSprite functions and properties
- File functions and properties
- Game functions and properties
- GUI functions and properties
- GUI control functions and properties
- GUI Button functions and properties
- GUI InvWindow functions and properties
- GUI Label functions and properties
- GUI List Box functions and properties
- GUI Slider properties
- GUI Text Box functions and properties
- Hotspot functions and properties
- Inventory item functions and properties
- Maths functions and properties
- Mouse functions and properties
- Object functions and properties
- Overlay functions and properties
- Parser functions
- Region functions and properties
- Room functions and properties
- Screen functions and properties
- Set functions and properties
- Speech functions and properties
- String functions
- System functions and properties
- TextWindowGUI functions and properties
- ViewFrame functions and properties
- Viewport functions and properties
- Obsolete Script API
- Event Types
- Key code table
- Audio in script
Legal Notice
Getting in touch
Misc