~~NOTOC~~
<=[[4rpl:start| Index]]
====== RefreshUnitLosCache ======
RefreshUnitLosCache(<-unit)
===== Description =====
Causes the unit to perform line of sight calculations based on the LOS settings. The results are stored and used by [[FindUnitEnemy]]. The LOS cache should be updated when the unit is moved, but not more frequently than absolutely necessary due to it's high performance cost. The best time to update LOSCache is when the unit comes to a stop or travels a certain distance.
----
====IMPORTANT NOTES====
RefreshUnitLOSCache is dependent on the range of the unit being defined. This is important if you are not using the built-in range parameter within the CPACK Unit Manager and are instead using code to define range via [[SetUnitRange]]. Always make sure to initialize the range before RefreshingUnitLOSCache or you will get many inconsistencies with the capability of [[FindUnitEnemy]] until range is defined for the unit before a LOSCache refresh.
The range of the unit is a major factor in the performance intensity of calling this API. It is highly discouraged to use this API for any unit with a range larger than 100. If you however still do, the best way to deal with the performance intensity is to have each unit call RefreshUnitLosCache at a slightly different frame from each other to prevent major stutters and make the performance impact unnoticeable.
For additional reference, do not use this API for a unit with a range more than 200. Any much higher and this API on it's own even when called by a single unit has enough performance impact to cause a noticeable stutter. Besides, you should only really need a max of 200 range for any moving unit if it can go off map, even less if it can't. 300 range should be map-sized most of the time too and best suited for units like the Bertha which don't need this API due to being stationary and having indirect line of sight, being mortar like, or other such units which use [[GetMaxCreeperCell]] or [[GetMaxCreeperCellWithDamage]] instead of this far more expensive API.
In fact, when using [[GetMaxCreeperCell]] but you still want line-of-sight as a factor for a non-mortar-like unit, you can use the API [[GetTerrainLOS]] but the unit will only target a specific location so you will need to fill in the details on how it reacts to having a blocked line-of-sight.
There isn't much difference between a unit with 200 range and a unit with 300 range for most maps and circumstances anyways. So please consider this when using this API.
===== Examples =====
RefreshUnitLOSCache(self)
#This unit doesn't have range set within the CPACK manager.
$RANGE:30
:Once
SetUnitRange(self <-RANGE) #This must be before LOSCache!
RefreshUnitLOSCache(self)
<=[[4rpl:start| Index]]