Today we resume our C++ Fundamentals lessons by looking at interfaces and how they can help simply our life by allowing for common contracts to be shared by different game actors.
Tags are essentially a collection of strings that can be applied to an Actor or an Actors Component and then referenced by other Actors or parts of the game world by those tags.
Very similar to using tags on a blog to include metadata for search engines to query … like this blog !
Tags ?! We don’t need to no stinking tags !
Sure you do, and they are really easy to use which is why this post is going to be super short.
Take a look, in this case we are displaying all tags for a specific actor.
// iterate over all of our actors
for(TActorIterator<AActor> ActorIterator(GetWorld()); ActorIterator; ++ActorIterator)
{
AActor* Actor = *ActorIterator;
// ensure actor is not null
// ignore self if found
// ensure we find actors of a specific interface only
if(Actor && Actor != this && Actor->GetClass()->ImplementsInterface(UInteractiveActor::StaticClass()))
{
// display all available tags for an actor
for(FName Tag : Actor->Tags)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Cyan, Tag.ToString());
}
}
}
We can also just check for a specific tag without iterating over all of them by using ActorHasTag method.
Today we resume our C++ Fundamentals lessons by looking at interfaces and how they can help simply our life by allowing for common contracts to be shared by different game actors.
An interface is an abstract definition of a contract. What this means is an interface defines how our class can be interfaced ( hah ! ) with while keeping the implementation details away.
More than that it allows us to create a common method signature between various disparate objects.
Consider I have an interface that has a method on it called “DoStuff” and I have two objects, Dog and Person, that inherit this interface. When I call Dog->DoStuff he may perform a trick while if I call the same method of Person->DoStuff he may tell me to fuck off and to stop bothering them. But from the point of view of the code interacting with those actors it’s the same behavior.
This allows for a lot of re-use and generic implementations while keeping the details of those implementations specific.
This all seems very abstract, show me code !
So let’s take a look at how an interface class is structured
UINTERFACE(MinimalAPI)
class UInteractiveActor : public UInterface
{
GENERATED_BODY()
};
/**
*
*/
class UE4FUNDAMENTALS09_API IInteractiveActor
{
GENERATED_BODY()
// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "Interact")
void Interact();
};
As you can see it’s pretty thin in definition, we simply have a class that inherits from UInterface and then defines the method signature of “Interact”.
Excellent now we are cooking with fire, now what ?
Once we have the interface in place we now start including it on our objects. Let’s take a look at our InteractiveProp.
UCLASS()
class UE4FUNDAMENTALS09_API AInteractiveProp : public AActor, public IInteractiveActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AInteractiveProp();
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Interact")
class UStaticMeshComponent* BaseMesh;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "Interact")
void Interact(); // prototype declaration
virtual void Interact_Implementation() override; // actual implementation of our interact method
private:
bool bIsBig;
};
The major thing to note is that in order to leverage the interface, we modify our class signature to bring in “IInteractiveActor”
class UE4FUNDAMENTALS09_API AInteractiveProp : public AActor, public IInteractiveActor
Then we overwrite the Interact method by first declaring the prototype method signature and then the implementation one. Please note that the “_Implementation” is important and has to be exact for the reflection mechanic in Unreal to process your interface + implementation.
So as you can see the implementation is pretty easy to understand.
We are creating a timer from our Time Manager object.
We are then registering this call back to use our Timer Handle.
Then we assign a callback method to be triggered at the end of our countdown.
Then finally we set that the countdown duration is 5 seconds.
Excellent ! Let’s blow some things up.
Timers can also be used to trigger modifications to specific values every so often. In this case we are going to simulate a fuse that will count down every second before it triggers an event.
First we are going to call the timer as we did previously, except this time we are going to call a new method and have this timer run every second.