ACE_Settings: Added a function to change settings on runtime (e.g. for modules)

This commit is contained in:
Nicolás Badano 2015-02-03 01:45:04 -03:00
parent 857b45453a
commit 2c0d2013d4
4 changed files with 49 additions and 19 deletions

View File

@ -4,6 +4,20 @@
// Load settings from profile
call FUNC(loadSettingsFromProfile);
// Listens for global "SettingChanged" events, to update the force status locally
["SettingChanged", {
PARAMS_2(_name,_value);
if !(count _this > 2) exitWith {};
_force = _this select 2;
if (_force) then {
_settingData = [_name] call FUNC(getSettingData);
if (isNull _settingData) exitWith {};
_settingData set [6,_force];
};
}] call FUNC(addEventhandler);
// hack to get PFH to work in briefing
[QGVAR(onBriefingPFH), "onEachFrame", {
if (time > 0) exitWith {

View File

@ -1,11 +1,14 @@
/*
* Author: CAA-Picard
* Set a setting if it was not previosuly forced. Force if neccesary.
* Change the value of an existing setting if it was not previously forced. Force if neccesary.
* If executed on clients it has local effect.
* If executed on server it can have global effect if the last parameter is set to true.
*
* Arguments:
* 0: Setting name (String)
* 1: Value (Any)
* 2: Force it? (Bool) (Optional)
* 3: Broadcast the change to all clients (Bool) (Optional)
*
* Return Value:
* None
@ -16,25 +19,40 @@
EXPLODE_2_PVT(_this,_name,_value);
if !(isServer) exitWith {};
private ["force"];
_force = false;
if (count _this > 2) then {
_force = _this select 2;
};
// Check if it's already forced and quit
if (missionNamespace getVariable [format ["%1_forced", _name], false]) exitWith {};
_settingData = [_name] call FUNC(getSettingData);
// Check if the variable is already defined
if (isNil _name) then {
// Add the variable to a list on the server
GVAR(settingsList) pushBack _name;
};
// Exit if the setting does not exist
if (isNull _settingData) exitWith {};
// Update the variable and publish it
// Exit if the setting is already forced
if (_settingData select 6) exitWith {};
// Exit if the type is not right
if (typeName _value != typeName (missionNamespace getVariable _name)) exitWith {};
// Force it if it was required
_settingData set [6, _force];
// Exit if the value didn't change
if (_value isEqualTo (missionNamespace getVariable _name)) exitWith {};
// Update the variable
TRACE_2("Variable Updated",_name,_value);
missionNamespace setVariable [_name, _value];
publicVariable _name;
missionNamespace setVariable [format ["%1_forced", _name], _force];
publicVariable format ["%1_forced", _name];
if (isServer && {count _this > 3} && {_this select 3}) then {
// Publicize the new value
publicVariable _name;
// Raise event globally, this publicizes eventual changes in _force status so clients can update it locally
["SettingChanged", [_name, _value, _force]] call FUNC(globalEvent);
} else {
// Raise event locally
["SettingChanged", [_name, _value, _force]] call FUNC(localEvent);
};

View File

@ -49,7 +49,7 @@ if (isNil _name) then {
// Read entry and cast it to the correct type
_value = [_optionEntry, _typeName] call _fnc_getValueWithType;
// Init the variable and publish it
// Init the variable
missionNamespace setVariable [_name, _value];
// Add the setting to a list on the server
@ -92,7 +92,7 @@ if (isNil _name) then {
// Read entry and cast it to the correct type
_value = [_optionEntry, _typeName] call _fnc_getValueWithType;
// Update the variable and publish it
// Update the variable
missionNamespace setVariable [_name, _value];
// Force the setting if requested

View File

@ -43,7 +43,5 @@ case (MENU_TAB_COLORS): {
if (_changed) then {
profileNamespace setVariable [_name, _newValue];
missionNameSpace setVariable [_name, _newValue];
["SettingChanged", [_name, _newValue]] call EFUNC(common,localEvent);
TRACE_2("Variable Updated",_name,_newValue);
[_name, _newValue] call EFUNC(common,setSetting);
};