2015-01-30 21:56:45 +00:00
|
|
|
/*
|
2015-03-24 04:18:00 +00:00
|
|
|
* Author: esteldunedain
|
2015-02-03 04:45:04 +00:00
|
|
|
* 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.
|
2015-01-30 21:56:45 +00:00
|
|
|
*
|
|
|
|
* Arguments:
|
|
|
|
* 0: Setting name (String)
|
|
|
|
* 1: Value (Any)
|
|
|
|
* 2: Force it? (Bool) (Optional)
|
2015-02-03 04:45:04 +00:00
|
|
|
* 3: Broadcast the change to all clients (Bool) (Optional)
|
2015-01-30 21:56:45 +00:00
|
|
|
*
|
|
|
|
* Return Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Public: No
|
|
|
|
*/
|
|
|
|
#include "script_component.hpp"
|
|
|
|
|
2015-05-07 19:51:03 +00:00
|
|
|
private ["_name","_value", "_force", "_settingData","_failed"];
|
2015-02-03 05:37:13 +00:00
|
|
|
_name = _this select 0;
|
|
|
|
_value = _this select 1;
|
2015-01-30 21:56:45 +00:00
|
|
|
|
2015-02-03 05:37:13 +00:00
|
|
|
private ["_force"];
|
2015-01-30 21:56:45 +00:00
|
|
|
_force = false;
|
|
|
|
if (count _this > 2) then {
|
|
|
|
_force = _this select 2;
|
|
|
|
};
|
|
|
|
|
2015-02-03 04:45:04 +00:00
|
|
|
_settingData = [_name] call FUNC(getSettingData);
|
2015-01-30 21:56:45 +00:00
|
|
|
|
2015-02-03 04:45:04 +00:00
|
|
|
// Exit if the setting does not exist
|
2015-02-03 05:37:13 +00:00
|
|
|
if (count _settingData == 0) exitWith {};
|
2015-02-03 04:45:04 +00:00
|
|
|
|
|
|
|
// Exit if the setting is already forced
|
|
|
|
if (_settingData select 6) exitWith {};
|
|
|
|
|
2015-02-03 05:51:41 +00:00
|
|
|
// If the type is not equal, try to cast it
|
2015-02-03 18:46:33 +00:00
|
|
|
_failed = false;
|
2015-02-03 05:51:41 +00:00
|
|
|
if ((typeName _value) != (_settingData select 1)) then {
|
2015-02-03 18:46:33 +00:00
|
|
|
_failed = true;
|
|
|
|
if ((_settingData select 1) == "BOOL" and (typeName _value) == "SCALAR") then {
|
|
|
|
// If value is not 0 or 1 consider it invalid and don't set anything
|
|
|
|
if (_value == 0) then {
|
|
|
|
_value = false;
|
|
|
|
_failed = false;
|
|
|
|
};
|
|
|
|
if (_value == 1) then {
|
|
|
|
_value = true;
|
|
|
|
_failed = false;
|
|
|
|
};
|
2015-02-03 05:51:41 +00:00
|
|
|
};
|
2015-02-03 18:46:33 +00:00
|
|
|
if ((_settingData select 1) == "COLOR" and (typeName _value) == "ARRAY") then {
|
2015-02-03 05:51:41 +00:00
|
|
|
_failed = false;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
if (_failed) exitWith {};
|
2015-02-03 04:45:04 +00:00
|
|
|
|
|
|
|
// Force it if it was required
|
|
|
|
_settingData set [6, _force];
|
2015-01-30 21:56:45 +00:00
|
|
|
|
2015-02-03 04:45:04 +00:00
|
|
|
// Exit if the value didn't change
|
|
|
|
if (_value isEqualTo (missionNamespace getVariable _name)) exitWith {};
|
|
|
|
|
|
|
|
// Update the variable
|
|
|
|
TRACE_2("Variable Updated",_name,_value);
|
2015-01-30 21:56:45 +00:00
|
|
|
missionNamespace setVariable [_name, _value];
|
2015-02-03 04:45:04 +00:00
|
|
|
|
|
|
|
if (isServer && {count _this > 3} && {_this select 3}) then {
|
2015-02-03 05:51:41 +00:00
|
|
|
// Publicize the new value
|
|
|
|
publicVariable _name;
|
2015-02-03 04:45:04 +00:00
|
|
|
|
2015-02-03 05:51:41 +00:00
|
|
|
// Raise event globally, this publicizes eventual changes in _force status so clients can update it locally
|
|
|
|
["SettingChanged", [_name, _value, _force]] call FUNC(globalEvent);
|
2015-02-03 04:45:04 +00:00
|
|
|
} else {
|
2015-02-03 05:51:41 +00:00
|
|
|
// Raise event locally
|
|
|
|
["SettingChanged", [_name, _value, _force]] call FUNC(localEvent);
|
2015-02-03 04:45:04 +00:00
|
|
|
};
|