FunctionSpeaker is a lightweight C++ library that provides a flexible and type-safe implementation of the multicast delegate pattern. It allows you to easily create event-driven systems in your C++ applications.
- Type-safe delegate system
- Support for member functions with multiple arguments
- Easy-to-use multicast delegate for invoking multiple callbacks
To use FunctionSpeaker in your project, simply include the FunctionSpeaker.h header file in your C++ project.
#include "FunctionSpeaker.h"Here's a simple example demonstrating how to use FunctionSpeaker with a health system in a game:
#include "FunctionSpeaker.h"
#include <iostream>
// Player class with health
class Player {
public:
Player(int initialHealth) : health(initialHealth) {}
void TakeDamage(int damage) {
health -= damage;
if (health < 0) health = 0;
onHealthChanged.ExecuteAll(health);
}
void Heal(int amount) {
health += amount;
if (health > 100) health = 100;
onHealthChanged.ExecuteAll(health);
}
int GetHealth() const { return health; }
MultiCastDelegate<int> onHealthChanged;
private:
int health;
};
// UI class to display health
class HealthUI {
public:
void UpdateHealth(int newHealth) {
std::cout << "Health UI updated. New health: " << newHealth << std::endl;
}
};
int main() {
Player player(100);
HealthUI healthUI;
// Register the UpdateHealth function to be called when health changes
player.onHealthChanged.Add(&healthUI, &HealthUI::UpdateHealth);
// Simulate game events
std::cout << "Player takes 30 damage" << std::endl;
player.TakeDamage(30);
std::cout << "Player heals 20 health" << std::endl;
player.Heal(20);
return 0;
}This example demonstrates how to:
- Create a
Playerclass with health and anonHealthChangeddelegate. - Create a
HealthUIclass with anUpdateHealthmethod. - Register the
UpdateHealthmethod to be called when the player's health changes. - Trigger health changes and observe the automatic UI updates.
The main class you'll interact with. It allows you to register multiple callbacks and execute them all at once.
MultiCastDelegate<Args...> myDelegate;myDelegate.Add(instance, &Class::Method, args...);myDelegate.ExecuteAll(args...);