Simply run one Widget / Gadget to get that value every 10 gameframe / drawframes, and pass that table to everything else in your game globally. If you have some special-needs operations you need to do after that, sort it at the Widget / Gadget level if possible.
This very simple (if boring) optimization of Widgets and Gadgets that need to get the list of visible Units can result in very significant CPU savings, by reducing the total number of searches by large amounts.
Here's a simple example, for Widgets:
First, include a master Widget that provides the info:
Code: Select all
function widget:GetInfo()
return {
name = "Visible Units",
desc = "Speedup for VisibleUnits() callin",
author = "Argh",
date = "August 23, 2009",
license = "Public Domain, or the least-restrictive rights of your country of residence",
layer = 0,
enabled = true -- loaded by default?
}
end
local GetDrawFrame = Spring.GetDrawFrame
local GetVisibleUnits = Spring.GetVisibleUnits
local ALL_UNITS = Spring.ALL_UNITS
local frame, oldframe = 0,0
WG.visible = {}
function widget:DrawWorld()
frame = GetDrawFrame()
if frame > oldframe then
--Spring.Echo("running now",frame)
WG.visible = GetVisibleUnits(ALL_UNITS,nil,false)
oldframe = frame + 10
end
endCode: Select all
function widget:DrawWorldPreUnit()
--Set up basic OpenGL parameters here
for i=1,#WG.visible do
local unitID = WG.visible[i]
id = GetUnitDefID(unitID)
if UsesReallyCoolFX[id] then
--display lists, shaders, whatever here
end
end
glResetState()
endI saw almost 15 FPS back with P.U.R.E. on a typical "city" map (i.e., high CPU load), which is a huge savings on lower-end hardware, so I thought I'd share this, as it's easy, if boring, to implement it and update your Widgets / Gadgets.
