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

The settings a console reads and changes by name while the game runs. More...

#include <aegis2d/core/console_variables.hpp>

Public Member Functions

void add (std::string_view name, VarValue default_value, std::string_view description, VarFlags flags={})
 Declares a variable, at its default.
 
template<std::same_as< bool > Bool>
void add (std::string_view name, Bool default_value, std::string_view description, VarFlags flags={})
 Declares a boolean variable.
 
template<std::integral Integer>
requires (!std::same_as<Integer, bool>)
void add (std::string_view name, Integer default_value, std::string_view description, VarFlags flags={})
 Declares a variable from an integer, kept as a 64-bit integer.
 
void add (std::string_view name, const char *default_value, std::string_view description, VarFlags flags={})
 Declares a text variable from a C string.
 
bool contains (std::string_view name) const
 Tells whether a variable is declared.
 
const VarInfo & info (std::string_view name) const
 A variable, as declared, with its value now.
 
template<typename Value >
const Value & get (std::string_view name) const
 Value of a variable, in its type.
 
std::string text (std::string_view name) const
 Value of a variable as text, as set_text() reads it back.
 
VarResult set (std::string_view name, VarValue value)
 Changes a variable, in its type; an integer sets a number too.
 
VarResult set_text (std::string_view name, std::string_view text)
 Changes a variable from text, as a console or a command line gives it.
 
void reset (std::string_view name)
 Puts a variable back at its default, whatever its flags.
 
void reset_all ()
 Puts every variable back at its default.
 
void set_cheats (bool enabled) noexcept
 Turns cheats on or off; cheat variables keep their values.
 
bool cheats () const noexcept
 Tells whether cheats are on.
 
std::vector< std::string_view > names () const
 Names of the variables, in alphabetical order.
 
std::size_t size () const noexcept
 Number of variables.
 

Detailed Description

The settings a console reads and changes by name while the game runs.

Each variable has a name (lower case letters, digits, _ and ., starting with a letter: r_vsync, net.rate), a type fixed when it is declared, a default, a description and flags. Values change in their own type, or from text as a console or a command line gives them (set_text()): 1, 0, true, false, on or off for a boolean, a whole number for an integer (an integer is a number too), any number, read the same in every locale, for a number, and the text itself for a text. A read-only variable keeps its default, and a cheat changes only while cheats are on. Not thread-safe: use it from the game thread.

variables.add("player_speed", 300.0, "Player speed, in pixels per second", {.cheat = true});
variables.set_text("player_speed", "450"); // refused: cheats are off
const double speed = variables.get<double>("player_speed");
The settings a console reads and changes by name while the game runs.
Definition console_variables.hpp:80
const Value & get(std::string_view name) const
Value of a variable, in its type.
Definition console_variables.hpp:147
VarResult set_text(std::string_view name, std::string_view text)
Changes a variable from text, as a console or a command line gives it.
void add(std::string_view name, VarValue default_value, std::string_view description, VarFlags flags={})
Declares a variable, at its default.

Member Function Documentation

◆ add() [1/4]

template<std::same_as< bool > Bool>
void aegis2d::core::ConsoleVariables::add ( std::string_view  name,
Bool  default_value,
std::string_view  description,
VarFlags  flags = {} 
)
inline

Declares a boolean variable.

An exact match for true and false, which MSVC would otherwise pass to the C string overload as a null pointer.

Template Parameters
Boolbool.
Parameters
nameAs add().
default_valueDefault.
descriptionWhat it is for.
flagsSaved, cheat, read-only.
Exceptions
std::invalid_argumentas add().

◆ add() [2/4]

void aegis2d::core::ConsoleVariables::add ( std::string_view  name,
const char *  default_value,
std::string_view  description,
VarFlags  flags = {} 
)

Declares a text variable from a C string.

Parameters
nameAs add().
default_valueDefault text.
descriptionWhat it is for.
flagsSaved, cheat, read-only.
Exceptions
std::invalid_argumentas add(), or if default_value is null.

◆ add() [3/4]

template<std::integral Integer>
requires (!std::same_as<Integer, bool>)
void aegis2d::core::ConsoleVariables::add ( std::string_view  name,
Integer  default_value,
std::string_view  description,
VarFlags  flags = {} 
)
inline

Declares a variable from an integer, kept as a 64-bit integer.

Parameters
nameAs add().
default_valueDefault.
descriptionWhat it is for.
flagsSaved, cheat, read-only.
Exceptions
std::invalid_argumentas add().

◆ add() [4/4]

void aegis2d::core::ConsoleVariables::add ( std::string_view  name,
VarValue  default_value,
std::string_view  description,
VarFlags  flags = {} 
)

Declares a variable, at its default.

Parameters
nameIts name: lower case letters, digits, _ and ., starting with a letter.
default_valueValue it starts with and reset() restores; its type is the variable's.
descriptionWhat it is for.
flagsSaved, cheat, read-only.
Exceptions
std::invalid_argumentif name is not a valid name or is already declared.

◆ cheats()

bool aegis2d::core::ConsoleVariables::cheats ( ) const
noexcept

Tells whether cheats are on.

Returns
false by default.

◆ contains()

bool aegis2d::core::ConsoleVariables::contains ( std::string_view  name) const

Tells whether a variable is declared.

Parameters
nameIts name.
Returns
true if add() declared it.

◆ get()

template<typename Value >
const Value & aegis2d::core::ConsoleVariables::get ( std::string_view  name) const
inline

Value of a variable, in its type.

Template Parameters
Valuebool, std::int64_t, double or std::string, the variable's type.
Parameters
nameIts name.
Returns
Its value now.
Exceptions
std::out_of_rangeif no variable has this name.
std::bad_variant_accessif the variable is of another type.

◆ info()

const VarInfo & aegis2d::core::ConsoleVariables::info ( std::string_view  name) const

A variable, as declared, with its value now.

Parameters
nameIts name.
Returns
Its value, default, description and flags.
Exceptions
std::out_of_rangeif no variable has this name.

◆ names()

std::vector< std::string_view > aegis2d::core::ConsoleVariables::names ( ) const

Names of the variables, in alphabetical order.

Returns
A view of each name, valid while the variables are not added to.

◆ reset()

void aegis2d::core::ConsoleVariables::reset ( std::string_view  name)

Puts a variable back at its default, whatever its flags.

Parameters
nameIts name.
Exceptions
std::out_of_rangeif no variable has this name.

◆ reset_all()

void aegis2d::core::ConsoleVariables::reset_all ( )

Puts every variable back at its default.

◆ set()

VarResult aegis2d::core::ConsoleVariables::set ( std::string_view  name,
VarValue  value 
)

Changes a variable, in its type; an integer sets a number too.

Parameters
nameIts name.
valueNew value.
Returns
VarResult::Changed, or why it was not changed.

◆ set_cheats()

void aegis2d::core::ConsoleVariables::set_cheats ( bool  enabled)
noexcept

Turns cheats on or off; cheat variables keep their values.

Parameters
enabledtrue to let cheat variables change.

◆ set_text()

VarResult aegis2d::core::ConsoleVariables::set_text ( std::string_view  name,
std::string_view  text 
)

Changes a variable from text, as a console or a command line gives it.

Parameters
nameIts name.
textNew value as text, read in the variable's type.
Returns
VarResult::Changed, or why it was not changed.

◆ size()

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

Number of variables.

Returns
How many add() declared.

◆ text()

std::string aegis2d::core::ConsoleVariables::text ( std::string_view  name) const

Value of a variable as text, as set_text() reads it back.

Parameters
nameIts name.
Returns
Such as "true", "42", "0.5" or the text itself.
Exceptions
std::out_of_rangeif no variable has this name.

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