Simple Expression: Difference between revisions
Killzone Kid (talk | contribs) (→SQF Equivalent of factor: correct function) |
Killzone Kid (talk | contribs) (→SQF Equivalent of factor: format) |
||
Line 44: | Line 44: | ||
== SQF Equivalent of <tt>factor</tt> == | == SQF Equivalent of <tt>factor</tt> == | ||
SQF_fnc_factor = | |||
{ | { | ||
[[private]] _fnc_interpolateCommon = | |||
{ | |||
[[params]] ["_c", "_cMin", "_cMax", "_vMin", "_vMax"]; | |||
[[if]] (_c < _cMin) [[exitWith]] { _vMin }; | |||
[[if]] (_c > _cMax) [[exitWith]] { _vMax }; | |||
(_c - _cMin) * (1 / (_cMax - _cMin)) * (_vMax - _vMin) + _vMin | |||
}; | |||
[[params]] ["_c", "_cMin", "_cMax"]; | |||
[[if]] (_cMin < _cMax) [[then]] | |||
{ | |||
[_c, _cMin, _cMax, 0, 1] call _fnc_interpolateCommon | |||
} | |||
[[else]] | |||
{ | |||
[_c, _cMax, _cMin, 0, 1] call _fnc_interpolateCommon | |||
}; | |||
}; | }; | ||
Example: <code>[0.2, 0.1, 0.7] [[call]] SQF_fnc_factor; // 0.166667</code> | Example: <code>[0.2, 0.1, 0.7] [[call]] SQF_fnc_factor; // 0.166667</code> |
Revision as of 12:58, 28 April 2019
Simple expressions are not as flexible as normal scripts, but they are compiled for a very efficient execution.
Available Operators
randomGen a | random value from 0 to a (randomGen 5) |
randomGen expr | random value from 0 to expression value (randomGen daylight) |
a factor [x,y] | a<=x: 0 a>=y: 1 x<=a<=y: ratio between x and y Note: works fine even for x>y (calculated as 1-a factor [y,x]) |
a interpolate [xFrom,xTo,resFrom,resTo] | Introduced in ArmA 2 1.05. interpolate result based on input value.
Equivalent to x factor [xFrom,xTo] * (resTo-resFrom) + resFrom |
a min b | see min |
a max b | see max |
a - b | see a-b |
a + b | see a+b |
a * b | see a*b |
a / b | see a/b (for A3, not sure if others) |
abs a | see abs (introduced in Arma 3 patch 1.68) |
Description: a,b can be any simple expression. x,y can be a constant expression only (i.e. expression with a type Number).
Since Arma 3 1.67 the following operators are also available (v ... controller value):
- abs(v) - absolute value
- sqr(v) - square value
- sqrt(v) - square root value
- pow(v, a) - power
- x envelope(a,b,c,d) - trapezoid envelope with output <0;1> (substitution for "(v factor(a,b))*(v factor(d,c))"
SQF Equivalent of factor
SQF_fnc_factor = { private _fnc_interpolateCommon = { params ["_c", "_cMin", "_cMax", "_vMin", "_vMax"]; if (_c < _cMin) exitWith { _vMin }; if (_c > _cMax) exitWith { _vMax }; (_c - _cMin) * (1 / (_cMax - _cMin)) * (_vMax - _vMin) + _vMin }; params ["_c", "_cMin", "_cMax"]; if (_cMin < _cMax) then { [_c, _cMin, _cMax, 0, 1] call _fnc_interpolateCommon } else { [_c, _cMax, _cMin, 0, 1] call _fnc_interpolateCommon }; };
Example: [0.2, 0.1, 0.7] call SQF_fnc_factor; // 0.166667
SQF Equivalent of interpolate
SQF_fnc_interpolate =
{
params ["_c", "_cMin", "_cMax", "_vMin", "_vMax"];
if (_c < _cMin) exitWith { _vMin };
if (_c > _cMax) exitWith { _vMax };
private _res = (_c - _cMin) * (1 / (_cMax - _cMin)) * (_vMax - _vMin) + _vMin;
[1 - _res, _res] select (_cMin < _cMax)
};
Example: [0.2, 0.1, 0.7, 0, 100] call SQF_fnc_interpolate; // 16.6667
Simple Expressions - How do they work?
The following is an example taken from the Eden Editor: Entity Context Menu.
Example:
conditionShow = "hoverObjectCanFly * (1 - hoverObjectFlying)";
Entry will only show if the object you are hovering over can fly and the object isn't flying.
Let's take a closer look at the example.
hoverObjectCanFly and hoverObjectFlying are two boolean values, which can either be true (0) or false (1).
If hoverObjectCanFly is true (1) and hoverObjectFlying is false (0) the resulting equation looks like this:
1 * (1-0) = x
1 * 1 = x
x = 1
The result will be true (1), therefore the entry will be shown.