Skip to content

Interfaces

Grisgram edited this page Aug 16, 2025 · 6 revisions

In an object oriented language, interfaces are a major design part, a kind of contract for the class, which methods it has to offer. In contrast to base classes, a class may implement as many interfaces as it likes, but it can only have one base class (single inheritance). It is important to use the correct terminology. You derive your class from a base class, but you implement interfaces.

GML does not have interfaces as you know them from Java or C#. But raptor offers something that is at least as close as possible to real interfaces.

implement and implements

Those two public functions are the interface mechanic of raptor.

You may implement an interface (or many interfaces)

  • In any Game Object
  • In any struct class (i.e. a constructor) function

Let's take a look, what they do by showing you an example:

Imagine this situation in your adventure game:

  • You have skills for close combat and for ranged combat
  • Every skill may be passive or active
  • Every skill may be self-targeting, single-targeting or AoE

If you would try to get this to work only with inheritance, you get a problem. It is not a hierarchy, it's a matrix, a grid. There are 7 possible stats listed above, and each skill may have any combination of them. So how would you design your object model?

Is close/ranged the base? Then you have a close-aoe, close-singletarget, close-active, close-passive, close-active-aoe, close-passive-aoe and so on. Lots of combinatations. And then all of that again for the ranged skills.

It is situations like those, where interfaces are getting strong.

What if you had an ICloseCombat, IRangedCombat, IPassiveSkill, IActiveSkill, ISelftargeting, ISingleTarget and IAoESkill interface and you could put them freely together at will?

Define an interface

To define an interface, you simply write a constructor function. That's it. Just make sure your functions are not static. If you need to execute code when constructing (and not only declare things), the ctor function will help you. More on this a bit below.

Base Class

To get this object model to work with interfaces, you should declare one common base class for all the Skills. Lets call it Skill (it could also be a game object, there's no difference, interfaces work with both types of instances).

// Skill Base Class (if this is an object, see this as the "Create" event

// For simplicity, we declare empty base methods
// You will understand shortly, why, just read on!
function Skill() constructor {
    select_target   = function() {}
    passive_trigger = function() {}
    run_skill       = function(_target) {}

    // A generic function will perform the run_skill for every target
    execute = function(_target) {
        with(_target)
            other.run_skill(self);
    }

    // Just as an example: A BROADCAST receiver could trigger
    // the passive skill. It's up to you, this is just an example
    BROADCASTER.add_receiver(..., function() {
        passive_trigger();
    });
}

Declare the Interfaces

Now that we have a base class, we can create some interfaces to reflect the skill's structure

Getting started

Raptor Tools (Work in Progress)

Raptor Free Modules

Image Raptor Pro Modules

Image Raptor Pro Libraries

Clone this wiki locally