Preferences
Preferences is the PHP API for small engine-managed local settings.
Namespace:
namespace Lenga\Engine\Core;
final class Preferences
Use Preferences for values such as volume, language, fullscreen, mouse sensitivity, and tiny non-critical flags. Do not use it for full save-game data or sensitive information.
Preferences are loaded from an engine-managed per-user location. Script code reads and writes values through the Preferences API instead of choosing an arbitrary file path.
Methods
getInt
public static function getInt(string $key, int $defaultValue = 0): int
Returns the saved integer value for $key.
If the key does not exist, Lenga returns $defaultValue.
setInt
public static function setInt(string $key, int $value): void
Sets an integer preference in memory.
Call save() when you want to write the change to disk.
getFloat
public static function getFloat(string $key, float $defaultValue = 0.0): float
Returns the saved float value for $key, or $defaultValue when the key does not exist.
setFloat
public static function setFloat(string $key, float $value): void
Sets a float preference in memory.
getString
public static function getString(string $key, string $defaultValue = ''): string
Returns the saved string value for $key, or $defaultValue when the key does not exist.
setString
public static function setString(string $key, string $value): void
Sets a string preference in memory.
getBool
public static function getBool(string $key, bool $defaultValue = false): bool
Returns the saved boolean value for $key, or $defaultValue when the key does not exist.
setBool
public static function setBool(string $key, bool $value): void
Sets a boolean preference in memory.
hasKey
public static function hasKey(string $key): bool
Returns true when a preference exists for $key.
deleteKey
public static function deleteKey(string $key): void
Removes one preference from memory.
Call save() to persist the removal.
deleteAll
public static function deleteAll(): void
Removes all preferences from memory.
Call save() to persist the reset.
save
public static function save(): bool
Writes the current in-memory preferences to disk.
Returns true when the save succeeds.
Example
The code below belongs in a Behaviour class attached to a normal GameObject.
<?php
namespace Game\Scripts;
use Lenga\Engine\Core\Behaviour;
use Lenga\Engine\Core\Preferences;
final class SettingsController extends Behaviour
{
public function start(): void
{
$volume = Preferences::getFloat('audio.masterVolume', 0.8);
// Apply $volume to your audio settings here.
}
public function setMasterVolume(float $volume): void
{
Preferences::setFloat('audio.masterVolume', $volume);
Preferences::save();
}
}
Usage Notes
Preferences is for small local settings and simple scalar state.
It is not a save-game system, a local database, or a secure secret store.