I want to be able to pass a function from a class called Game
inside of a namespace called Application
to another function inside a class called Button
inside the namespace Engine
however I'm unsure how to do it and how to call the function.
Background information, the Game class has an entity that, when clicked I want to call a function that is stored in the Button component.
Game.h
namespace Application
{
class Game
{
void Print(Engine::ButtonData data)
{
if (data.buttonPressed == Engine::MouseButton::Left)
std::cout << "Left\n";
else if (data.buttonPressed == Engine::MouseButton::Right)
std::cout << "Right\n";
}
e1->button = new Engine::Component::Button(Print); // This is just where the function is passed to the button class for storage
};
}
Button.h
- Only data is to be stored in this class
namespace Engine
{
namespace Component
{
class Button
{
public:
Button() {}
Button(void(* callback)(ButtonData))
: m_Callback(callback)
{}
void(* m_Callback)(ButtonData);
};
}
}
Entity Manager
- Where the function needs to be called
comp->m_Callback(ButtonData(
Mouse::GetCurrentButton(),
std::pair<int, int>(Mouse::GetMouseX(), Mouse::GetMouseY()),
comp->m_Callback)
);
Any help would be great, thanks.