KTech 1.1.0
C++ 2D terminal game engine library
Loading...
Searching...
No Matches
KTech::Time::Invocation Struct Reference

Calls a function after a given time. More...

#include <invocation.hpp>

Public Member Functions

 Invocation (Engine &engine, const std::function< bool()> &callback)
 Construct an Invocation.
 
 ~Invocation ()
 Safely deregister invocation from Time.
 
void Invoke (long time, Measurement measurement)
 Invoke your callback function.
 
void Cancel ()
 Cancel the current invocation.
 

Public Attributes

Engineengine
 Parent Engine.
 
std::function< bool()> m_callback
 Function to invoke.
 
bool m_active = false
 Whether currently invoked (true: invoked, false: stationary).
 
long m_timePassed = 0
 How much time (in microseconds) actually passed since invocation started until now (if active), or until invocation ended (if inactive).
 
long m_duration = 0
 How much time (in microseconds) should pass before your function gets called (if active), or 0 (if inactive).
 

Detailed Description

Calls a function after a given time.

This class uses RAII to prevent cases where your member functions are called after their parent structure was deconstructed from memory.

For example:

class MyObject : Object
{
Invocation myInvocation;
// A member function to invoke.
bool InvokedMember()
{
// Access some memory...
m_pos = Point(25, 25);
m_textures.resize(10);
return true;
}
MyObject()
: Object(engine, Point(0, 0)), myInvocation(engine, [this]{ return InvokedMember(); })
{
// Invoke the member function:
myInvocation.Invoke(1, Time::Measurement::seconds);
}
};
int main()
{
//...
// Construct a `MyObject` instance within a temporary scope:
{
MyObject myObject;
}
// Scope ends so `myObject` is destructed right away. `myObject.myInvocation` is also destructed, so even though it was just invoked, `myObject.InvokedMember()` will not be called.
//...
}
World structure that comprises Textures and Colliders, and exists within Layer.
Definition object.hpp:37
Point m_pos
World position.
Definition object.hpp:44
Engine & engine
Parent Engine.
Definition object.hpp:39
std::vector< Texture > m_textures
Textures.
Definition object.hpp:45
2D vector, mostly used to store positions and directions.
Definition point.hpp:30
Calls a function after a given time.
Definition invocation.hpp:68
void Invoke(long time, Measurement measurement)
Invoke your callback function.
Definition invocation.cpp:82

Constructor & Destructor Documentation

◆ Invocation()

KTech::Time::Invocation::Invocation ( Engine & engine,
const std::function< bool()> & callback )

Construct an Invocation.

Parameters
engineParent Engine.
callbackYour callback function. It should return a bool explained in Output::ShouldRenderThisTick().
See also
Output::ShouldRenderThisTick()

Member Function Documentation

◆ Invoke()

void KTech::Time::Invocation::Invoke ( long time,
Measurement measurement )

Invoke your callback function.

Can be invoked once at a time. Meaning, invoking while already invoked will restart the current invocation (rather than ). For example:

void Loop()
{
// Invoke this function after a second:
static Invocation invocation{engine, Loop, 1, Measurement::seconds};
invocation.Invoke();
// Print when the game quits how many times this function was called:
static unsigned int counter = 0;
engine.output.outputOnQuit.push_back(std::to_string(++counter));
}
int main()
{
// Call `Loop()` twice:
Loop();
Loop();
}
// Expected `outputOnQuit`:
// 1
// 2
// 3
Complete engine containing all engine components.
Definition engine.hpp:41
Output output
Output engine component.
Definition engine.hpp:49
std::vector< std::string > outputOnQuit
Vector of strings to print when the game quits (specifically, in Output::~Output()).
Definition output.hpp:45
Engine & engine
Parent Engine.
Definition invocation.hpp:69
Parameters
timeDuration to wait for before calling your function.
measurementThe time measurement for your given time.
See also
Time::Measurement

The documentation for this struct was generated from the following files: