Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify and fix 'CBA_fnc_setVarNet' and 'CBA_fnc_publicVariable' #471

Merged
merged 4 commits into from
Aug 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 40 additions & 52 deletions addons/network/fnc_publicVariable.sqf
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/*
/* ----------------------------------------------------------------------------
Function: CBA_fnc_publicVariable

Description:
CBA_fnc_publicVariable does only broadcast the new value if it doesn't exist in missionNamespace or the new value is different to the one in missionNamespace.
Broadcast a variables value to all machines. Used to reduce network traffic.

Checks also for different types. Nil as value gets always broadcasted.

Should reduce network traffic.
Does only broadcast the new value if it doesn't exist in missionNamespace or
if the new value is different to the one in missionNamespace.
Nil as value gets always broadcasted.

Parameters:
_pv - Name of the publicVariable [String]
_value - Value to check and broadcast if it is not the same as the previous one, code will always be broadcasted [Any]
_varName - Name of the public variable <STRING>
_value - Value to broadcast <ANY>

Returns:
True if if broadcasted, otherwise false [Boolean]
True if if broadcasted, otherwise false <BOOLEAN>

Example:
(begin example)
Expand All @@ -22,57 +22,45 @@ Example:
(end)

Author:
Xeno
*/
// #define DEBUG_MODE_FULL
Xeno, commy2
---------------------------------------------------------------------------- */
//#define DEBUG_MODE_FULL
#include "script_component.hpp"

params ["_pv","_value"];
params [["_varName", "", [""]], "_value"];

if (typeName _pv != typeName "") exitWith {
WARNING("The first parameter is not of type string!");
if (_varName isEqualTo "") exitWith {
WARNING("Variable name is wrong type or undefined");
false
};

private ["_var","_s"];
_var = missionNamespace getVariable _pv;

if (isNil "_var") exitWith {
TRACE_2("Broadcasting",_pv,_value);
missionNamespace setVariable [_pv, _value];
publicVariable _pv;
true
};
private _currentValue = missionNamespace getVariable _varName;

_s = if (typeName _value != typeName _var) then {
TRACE_2("Different typenames",_var,_value);
false
if (isNil "_currentValue") then {
if (isNil "_value") then {
TRACE_1("Not broadcasting. Current and new value are undefined",_varName);
false // return
} else {
TRACE_2("Broadcasting previously undefined value",_varName,_value);
missionNamespace setVariable [_varName, _value];
publicVariable _varName;
true // return
};
} else {
switch (typename _value) do {
case "BOOL": {
((_var && {_value}) || {(!_var && {!_value})})
};
case "ARRAY": {
(_var isEqualTo _value)
};
case "CODE": {
false
if (isNil "_value") then {
TRACE_1("Broadcasting nil",_varName);
missionNamespace setVariable [_varName, nil];
publicVariable _varName;
true // return
} else {
if (_value isEqualTo _currentValue) then {
TRACE_3("Not broadcasting. Current and new value are equal",_varName,_currentValue,_value);
false // return
} else {
TRACE_2("Broadcasting",_varName,_value);
missionNamespace setVariable [_varName, _value];
publicVariable _varName;
true // return
};
case "SCRIPT": {
false
};
default {
(_var == _value)
};
}
};
if (_s) exitwith {
TRACE_2("Not broadcasting, _var and _value are equal",_var,_value);
false
};
};

TRACE_2("Broadcasting",_pv,_value);
missionNamespace setVariable [_pv, _value];
publicVariable _pv;

true
92 changes: 41 additions & 51 deletions addons/network/fnc_setVarNet.sqf
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/*
/* ----------------------------------------------------------------------------
Function: CBA_fnc_setVarNet

Description:
Same as setVariable ["name",var, true] but only broadcasts when the value of var is different to the one which is already saved in the variable space.
Broadcast a object variable value to all machines. Used to reduce network traffic.

Checks also for different types. Nil as value gets always broadcasted.

Should reduce network traffic.
Does only broadcast the new value if it doesn't exist in the object namespace or
if the new value is different to the one in object namespace.
Nil as value gets always broadcasted.

Parameters:
_object - Name of a marker [Object, Group]
_variable - Name of the variable in variable space [String]
_value - Value to check and broadcast if it is not the same as the previous one, code will always be broadcasted [Any]
_object - Object namespace <OBJECT, GROUP>
_varName - Name of the public variable <STRING>
_value - Value to broadcast <ANY>

Returns:
True if broadcasted, otherwise false [Boolean]
True if if broadcasted, otherwise false <BOOLEAN>

Example:
(begin example)
Expand All @@ -23,57 +23,47 @@ Example:
(end)

Author:
Xeno
*/
Xeno, commy2
---------------------------------------------------------------------------- */
//#define DEBUG_MODE_FULL
#include "script_component.hpp"

params ["_object","_variable","_value"];
params [["_object", objNull, [objNull, grpNull]], ["_varName", "", [""]], "_value"];

// does setVariable public also work for other types ??
if (typeName _object != "OBJECT" && {typeName _object != "GROUP"}) exitWith {
WARNING("The first parameter is not of type object or group!");
if (isNull _object) exitWith {
WARNING("Object wrong type, undefined or null");
false
};

private ["_var","_s"];

_var = _object getVariable _variable;

if (isNil "_var") exitWith {
TRACE_3("Broadcasting",_object,_variable,_value);
_object setVariable [_variable, _value, true];
true
if (_varName isEqualTo "") exitWith {
WARNING("Variable name is wrong type or undefined");
false
};

_s = if (typeName _value != typeName _var) then {
TRACE_2("Different typenames",_var,_value);
false
private _currentValue = _object getVariable _varName;

if (isNil "_currentValue") then {
if (isNil "_value") then {
TRACE_2("Not broadcasting. Current and new value are undefined",_object,_varName);
false // return
} else {
TRACE_3("Broadcasting previously undefined value",_object,_varName,_value);
_object setVariable [_varName, _value, true];
true // return
};
} else {
switch (typename _value) do {
case "BOOL": {
((_var && {_value}) || {(!_var && {!_value})})
};
case "ARRAY": {
(_var isEqualTo _value)
if (isNil "_value") then {
TRACE_2("Broadcasting nil",_object,_varName);
_object setVariable [_varName, nil, true];
true // return
} else {
if (_value isEqualTo _currentValue) then {
TRACE_4("Not broadcasting. Current and new value are equal",_object,_varName,_currentValue,_value);
false // return
} else {
TRACE_3("Broadcasting",_object,_varName,_value);
_object setVariable [_varName, _value, true];
true // return
};
case "CODE": {
false
};
case "SCRIPT": {
false
};
default {
(_var == _value)
};
}
};
};
if (_s) exitwith {
TRACE_2("Not broadcasting, _var and _value are equal",_var,_value);
false
};

TRACE_3("Broadcasting",_object,_variable,_value);
_object setVariable [_variable, _value, true];

true
75 changes: 75 additions & 0 deletions addons/network/test_network.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "script_component.hpp"
SCRIPT(test_network);

// 0 spawn compile preprocessFileLineNumbers "\x\cba\addons\network\test_network.sqf";
// test has to be done with an existing "player" object (not 3den!!)

private ["_funcName", "_result"];

_funcName = "CBA_fnc_publicVariable";
LOG("Testing " + _funcName);

TEST_DEFINED("CBA_fnc_publicVariable","");

X1 = nil;
X2 = nil;

_result = ["X1", nil] call CBA_fnc_publicVariable;
TEST_FALSE(_result,_funcName);

_result = ["X1", 1] call CBA_fnc_publicVariable;
TEST_TRUE(_result,_funcName);

_result = ["X1", 2] call CBA_fnc_publicVariable;
TEST_TRUE(_result,_funcName);

_result = ["X1", 2] call CBA_fnc_publicVariable;
TEST_FALSE(_result,_funcName);

_result = ["X2", 2] call CBA_fnc_publicVariable;
TEST_TRUE(_result,_funcName);

_result = ["X1", nil] call CBA_fnc_publicVariable;
TEST_TRUE(_result,_funcName);

_result = ["X1", nil] call CBA_fnc_publicVariable;
TEST_FALSE(_result,_funcName);

////////////////////////////////////////////////////////////////////////////////////////////////////

_funcName = "CBA_fnc_setVarNet";
LOG("Testing " + _funcName);

TEST_DEFINED("CBA_fnc_setVarNet","");

player setVariable ["X1", nil];
player setVariable ["X2", nil];
cba_logic setVariable ["X1", nil];
cba_logic setVariable ["X2", nil];

_result = [objNull, "X1", 1] call CBA_fnc_setVarNet;
TEST_FALSE(_result,_funcName);

_result = [player, "X1", nil] call CBA_fnc_setVarNet;
TEST_FALSE(_result,_funcName);

_result = [player, "X1", 1] call CBA_fnc_setVarNet;
TEST_TRUE(_result,_funcName);

_result = [player, "X1", 2] call CBA_fnc_setVarNet;
TEST_TRUE(_result,_funcName);

_result = [player, "X1", 2] call CBA_fnc_setVarNet;
TEST_FALSE(_result,_funcName);

_result = [cba_logic, "X1", 2] call CBA_fnc_setVarNet;
TEST_TRUE(_result,_funcName);

_result = [player, "X2", 2] call CBA_fnc_setVarNet;
TEST_TRUE(_result,_funcName);

_result = [player, "X1", nil] call CBA_fnc_setVarNet;
TEST_TRUE(_result,_funcName);

_result = [player, "X1", nil] call CBA_fnc_setVarNet;
TEST_FALSE(_result,_funcName);