GameEvents.UnitSetXY() overhead

Pazyryk

Deity
Joined
Jun 13, 2008
Messages
3,584
I've resisted this (so far) out of fear of overhead. But it's just too tempting.

Has anyone used this and payed attention to game speed effects? The Lua part of my code should be very efficient:

Code:
local g_unitsToWatch = {}
for iPlayer = 0, GameDefines.MAX_PLAYERS - 1 do
	g_unitsToWatch[iPlayer] = {}
end

local function OnUnitSetXY(iPlayer, iUnit, x, y)
	if g_unitsToWatch[iPlayer][iUnit] then
		--stuff happens
	end
end
GameEvents.UnitSetXY(OnUnitSetXY)

The majority of units will not be indexed in g_unitsToWatch, so it is just a quick index check that resolves to nil. But I suspect the overhead here would actually be on the C++ side (how the DLL passes off to Lua). If it can do that 1000 times/sec, OK. If < 100, not so OK.
 
Just run some tests, and 25,000 calls to LuaSupport::CallHook() for a 4 parameter event execute in less than a second
 
I use UnitSetXY pretty frequently -- my Madoka civilization uses it to track the locations of all of her Great People, the Sayaka civilization uses it to grant promotions upon entering enemy territory based on Influence, and my Liquid Ocelot civilization uses it for a set of promotions which give negative promotions to nearby enemy units.

I run several of these calls, and I only notice performance issues with the call if there are print statements in the function. I get around this by commenting out every print statement in my Lua scripts when I publish my mods, and uncomment them when I need to debug.
 
I only notice performance issues with the call if there are print statements in the function. I get around this by commenting out every print statement in my Lua scripts when I publish my mods, and uncomment them when I need to debug.

Code:
local bDebug = false

function dprint(...)
  if (bDebug) then
    print(string.format(...))
  end
end

no need to comment/uncomment, just use dprint instead and set bDebug as appropriate
 
Back
Top Bottom