Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::core::HotReload Class Reference

Watches files and reloads each one with its own function when it changes. More...

#include <aegis2d/core/hot_reload.hpp>

Public Types

using Reload = std::function< void(const std::filesystem::path &path)>
 What to do with a changed file.
 

Public Member Functions

 HotReload (bool enabled, std::chrono::milliseconds interval=std::chrono::milliseconds{250})
 Creates a watcher with no file.
 
bool enabled () const noexcept
 Tells whether files are watched.
 
void watch (const std::filesystem::path &path, Reload reload)
 Starts watching a file, as it is now.
 
std::size_t size () const noexcept
 Files watched.
 
std::size_t update ()
 Reloads the files that changed since they were last seen.
 

Detailed Description

Watches files and reloads each one with its own function when it changes.

A texture, a JSON file or anything read from disk registers how to reload it with watch(); update(), called once per frame, looks at the files at most every interval and calls the reload function of each file whose time or size changed. A reload that throws (a file caught half-written, an image that does not decode) is logged and the file stays watched, so the next save is tried again; the game keeps what it had. Off, as in release builds by default, watch() and update() do nothing.

textures.watch(hot); // every texture file loaded
hot.watch(config_path, [&](const auto& path) { config = core::Config::from_file(path); });
// Each frame:
hot.update();
static Config from_file(const std::filesystem::path &path)
Reads and parses a JSON file.
Watches files and reloads each one with its own function when it changes.
Definition hot_reload.hpp:40
void watch(const std::filesystem::path &path, Reload reload)
Starts watching a file, as it is now.
constexpr bool hot_reload_by_default
Whether to turn hot reload on: in debug builds (NDEBUG not defined).
Definition hot_reload.hpp:23

Member Typedef Documentation

◆ Reload

using aegis2d::core::HotReload::Reload = std::function<void(const std::filesystem::path& path)>

What to do with a changed file.

Constructor & Destructor Documentation

◆ HotReload()

aegis2d::core::HotReload::HotReload ( bool  enabled,
std::chrono::milliseconds  interval = std::chrono::milliseconds{250} 
)
explicit

Creates a watcher with no file.

Parameters
enabledfalse to do nothing, such as hot_reload_by_default in a release build.
intervalLeast time between two looks at the files; zero looks at every update().

Member Function Documentation

◆ enabled()

bool aegis2d::core::HotReload::enabled ( ) const
noexcept

Tells whether files are watched.

Returns
The value given to the constructor.

◆ size()

std::size_t aegis2d::core::HotReload::size ( ) const
noexcept

Files watched.

Returns
How many different files; 0 when off.

◆ update()

std::size_t aegis2d::core::HotReload::update ( )

Reloads the files that changed since they were last seen.

Returns
How many reloads succeeded; failures are logged, not thrown.

◆ watch()

void aegis2d::core::HotReload::watch ( const std::filesystem::path &  path,
Reload  reload 
)

Starts watching a file, as it is now.

Watching a file again replaces its reload function, so it is reloaded once per change whoever registers it, however often. A reload function may watch more files, as when a level file that changed brings new textures.

Parameters
pathFile to watch; it may not exist yet, its first appearance is a change.
reloadCalled with path by update() when the file changes.

The documentation for this class was generated from the following file: