Page tree
Skip to end of metadata
Go to start of metadata




Introduced in

Version:1.31

Description

Description:Evaluates an expression using a cache to increase speed.

The cache holds the results of the evaluation, so the next time the same expression is evaluated, the results are read from the cache, as opposed to being evaluated again.

The cache works based on the input only. It stores the output that is the result of a given input. Any time the exact same input is given, the exact same output is returned. This means the function only works correctly for expressions where the same input always yields the same output (for example, regardless of time, position of entities, and other factors). The code itself is not actually checked by the cache. The user should ensure that the same code is used whenever a given cache name is used.

Syntax

Syntax:[input, code, cache] call fn_vbs_cachedEval
Parameters:
  • input: Anything - Parameter(s) to pass to the code in the _this variable.
  • code: Code - Expression to evaluate, with the input passed in the _this variable.
  • cache: String or Array - Array or name of global variable used as cache (must first be initialized by fn vbs cacheInit).
Return Value:Anything - Result of the expression: input call code.

Warning

When using functions in VBS versions older than 3.4, certain limitations and requirements should be kept in mind - the main ones being that capitalization of the function name is crucial, and that #include "\vbs2\headers\function_library.hpp" has to be included in every script that utilizes it. More Category VBS Scripting Functions.

Examples

Examples:
["mycache", 100, 150, 2, true] call fn_vbs_cacheInit;
_ret = 0;
while {_ret < 0.75} do
{
	_ret = [getdammage player, getdammage bob, getdammage john], fn_vbs_arithmeticMean, "mycache"] call fn_vbs_cachedEval;
	sleep 1;
};
mycache = nil;

In this example, the mean damage level of player, bob, and john is evaluated repeatedly. They are all likely to stay at the same damage level (0) for quite some time. This means that the mean is only calculated the first time. After that, the result is not recalculated, but rather returned from the cache. Only when the damage levels change, the result is recalculated.

In this situation, not much is gained by using a cache, because the CPU cost of the expression is quite small (it is quick to calculate the mean). However, if the code being evaluated is very complex, and the input is often identical, then using a cache could result in a noticeable performance increase.

Note: The cache is manually cleared at the end of this example. This can be done at any time to reset the cache.

Additional Information

See also: fn vbs cacheInit, fn vbs cacheDiag
Multiplayer:
Problems:

Notes