Settings and other runtime variables can be stored in the Variables service. This provides generic storage for untyped variables which can be set and queried. Like other services the variable service is retrievable through the Context pointer.
See List of COGS variables for detailed information about all COGS variables available at runtime.
Creating a new variable by adding it to the variable service:
// Add our new variable to the service.
context->variables->add("myVariable", "myValue");
// Fetch a pointer to the same variable.
auto myVar = context->variables->get("myVariable");
// Fetch the value.
myVar->getValue() == "myValue"; // true
Since variables are not typed, they can be queried for their underlying string value at any time. However, in most cases it is useful to treat the value as a specific type.
Supported types include:
The typed values are derived from the underlying string representation, and can be retrieved like the following:
// Create a boolean variable.
auto bVar = context->variables->add("myBoolean", "true");
if (bVar->get()) {
// Executes...
}
// Create a floating point variable.
auto fVar = context->variables->add("myFloat", "1.0");
if (fVar->get() == 1.0f) {
// Executes...
}
// Treat as integer
if (fVar->get() == 1) {
// Executes
}