Skip to content

Instantly share code, notes, and snippets.

Created Dec 25, 2020
What would you like to do?
#include "CallbackTimer.h"
#include "Urho3D/Core/Context.h"
#include "Urho3D/DebugNew.h"
CallbackTimer::CallbackTimer(Context* context)
: LogicComponent(context)
// Only the scene update event is needed: unsubscribe from the rest for optimization
void CallbackTimer::SetCallback(const std::function<void()>& callback, float time, bool repeat)
callback_ = callback;
timeToRemove_ = time;
repeat_ = repeat;
currentTime_ = 0.0f;
void CallbackTimer::Update(float timeStep)
currentTime_ += timeStep;
if (currentTime_ >= timeToRemove_)
// Keep this object from being removed in the callback function.
SharedPtr<CallbackTimer> self(this);
// Do callback
if (repeat_)
currentTime_ = 0.0f;
#pragma once
#include <Urho3D/Scene/LogicComponent.h>
using namespace Urho3D;
/// Callback timer.
class CallbackTimer : public LogicComponent
URHO3D_OBJECT(CallbackTimer, LogicComponent);
/// Construct.
explicit CallbackTimer(Context* context);
/// Set time before remover.
void SetCallback(const std::function<void()>& callback, float time, bool repeat=false);
/// Handle update. Called by LogicComponent base class.
void Update(float timeStep) override;
/// Time seconds before removed.
float timeToRemove_ = 0.0f;
/// Current time seconds;
float currentTime_ = 0.0f;
/// If timer should be repeated.
bool repeat_ = false;
/// Callback function.
std::function<void()> callback_;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment