DynamicDelegate

A Dynamic Delegate like in the Unreal framework


Project maintained by DanielBortfeld Hosted on GitHub Pages — Theme by mattgraham

Back to Main Page

Dynamic Delegate (C++)

Define a dynamic delegate like in the Unreal framework, but without using the Unreal framework! The delegate can be subscribed by a pair of a YourBaseClass* and a function to call on the base class. Broadcast() will call the functions on the respective objects. You will find the useage below and the source code in the repository.

View Delegate Source Code on GitHub

// (c) Daniel Bortfeld 2016 - 2017

// GameObject.cpp

void GameObject::Destroy()
{
	std::cout << Name << " was destroyed" << std::endl;
	delete this;
}

// Main.cpp

#include "Delegate.h"

DECLARE_DELEGATE(DestroyDelegate)

int main()
{
	DestroyDelegate Destroy;
    
	GameObject* gameObject2 = new GameObject("GO 1");
	GameObject* gameObject3 = new GameObject("GO 2");
	GameObject* gameObject4 = new GameObject("GO 3");
    
    printf_s("Adding function GameObject::Destroy of GO 1, 2 & 3 to delegate Destroy...\n");
	Destroy.Add<GameObject>(gameObject2, &GameObject::Destroy);
	Destroy.Add<GameObject>(gameObject3, &GameObject::Destroy);
	Destroy.Add<GameObject>(gameObject4, &GameObject::Destroy);
    
    // ...
    
    printf_s("Broadcasting delegate Destroy...\n");
	Destroy.Broadcast();
    
    return 0;

Output

Adding function GameObject::Destroy of GO 1, 2 & 3 to delegate Destroy…
Broadcasting delegate Destroy…
GO 1 was destroyed
GO 2 was destroyed
GO 3 was destroyed


Further examples:


Back to Main Page