Interfaces
Flax Scripting API supports using interfaces on classes to splitting unrelated functionality into separate interfaces. This comes useful when desiging larger game systems or when ensuring the code can be extended.
Interface declaration
Declaring an interface is quite similar to declaring a normal class: simply use API_INTERFACE tag. Then you can declare virtual function to be part of the interface.
#pragma once
#include "Engine/Scripting/ScriptingType.h"
API_INTERFACE() class GAME_API IMyInterface
{
DECLARE_SCRIPTING_TYPE_MINIMAL(IMyInterface);
// Interface virtual method
API_FUNCTION() virtual float GetSpeed(const Vector3& v) = 0;
};
Tip
Interfaces with an abstract method (deleted virtual) that don't contain API_FUNCTION tag are not supported.
Interface implementation
The next step is to define interface inheritance on an class and implement all it's methods. Interfaces can be implemented on any Scripting Object such as Script, Actor, PersistentScriptingObject. Interface methods are virtual thus they can be overridden also in C# and Visual Scripting.
#pragma once
#include "IMyInterface.h"
#include "Engine/Scripting/Script.h"
// Script that implements custom interface
API_CLASS() class GAME_API InterfaceInCpp : public Script, public IMyInterface
{
API_AUTO_SERIALIZATION();
DECLARE_SCRIPTING_TYPE(InterfaceInCpp);
float GetSpeed(const Vector3& v) override
{
// Implement interface method
return v.Length();
}
};
Interface calling
#pragma once
#include "IMyInterface.h"
#include "Engine/Scripting/Script.h"
API_CLASS() class GAME_API InterfaceInCpp : public Script
{
API_AUTO_SERIALIZATION();
DECLARE_SCRIPTING_TYPE(InterfaceInCpp);
// Pointer to object with interface implementation (can be set from other script in C++ or C# or VS)
API_FIELD() ScriptingObject* MyInterface = nullptr;
void OnUpdate() override
{
// Cast object into interface
auto interface = ToInterface<IMyInterface>(MyInterface);
if (interface)
{
// Call interface method (works with both C++ and C# interface implementations)
interface->GetSpeed(Vector3::One);
}
}
};
Interface reference
Scripting objects (such as scripts or actors) can implement interfaces and be referenced in scripts using a dedicated type ScriptingObjectInterfaceReference:
#include "Engine/Scripting/ScriptingObjectInterfaceReference.h"
ScriptingObjectInterfaceReference<IMyInterface> Interface;
Note
This works only when the referenced interface is a scripting object identified by its unique ID and can be serialized.