Would this global data persist between script calls/turns?

OuroborosOrder

Warlord
Joined
Apr 3, 2016
Messages
124
Location
Alabama
Say I am trying to do something like:

Code:
-- UnitTracker.Lua
-- Created By Ouroboros
-- 4/8/2016

g_pUnitData = {}
g_pUnitTurnLog = {};

function SetClassesToTrack()
	for pUnit in GameInfo.Units() do
		if pUnit.Class == "UNITCLASS_SWORDSMAN" or pUnit.Class == "UNITCLASS_SPEARMAN" or pUnit.Class == "UNITCLASS_WARRIOR" then
			-- Will this persist across calls to this script each turn?
			table.insert(g_pUnitData, {pUnit.ID = {pUnit.Type, pUnit.Class});
		end
	end
end

function TrackUnits(iPlayer)
	local iTurn = Game.GetGameTurn();
	local pPlayer = Players[iPlayer];
	
	if iTurn == 0 then
		SetClassesToTrack();
	end
	
	for pUnitData in g_pUnitData do
		if pPlayer.HasUnitOfClassType(iUnitID.Class) then
			table.insert(iTurn, pUnitData);
		end
	end	
	
	print("Unit Class Tracker:");
	print(g_pUnitTurnLog);
	-- We could do other things with this type of data, including creating an in game unit-turn tracking log for the player.
end
-- Register the function with the do turn event which is run on each player turn. 
GameEvents.PlayerDoTurn.Add(TrackUnits);

Would the global LUA tables created in this script persist between turns/calls to the script in game? I was thinking this might be a good way to reduce overhead.
 
Your pUnit is a unit (something that moves around on the map), NOT a row in a database table. Therefore you need to use the methods on the Lua Unit object (see the BNW API reference I linked you previously) and not "column'esqe" access

pUnit.Class is wrong - you're treating the Lua Unit object as if it was a database row, it should be pUnit:GetUnitClassType() (which contrary to the expectation from the name gives the ID value from the UnitClasses database table that the Lua unit is an instance of)

"for pUnitData in g_pUnitData do" will fail with an error. g_pUnitData is a table, for expects an iterator, you need to use "for i, pUnitData in ipairs(g_pUnitData) do"

pPlayer.HasUnitOfClassType(...) should be pPlayer:HasUnitOfClassType(...)

There are probably other mistakes, I stopped looking (as my compile has finished)

But to answer the persistence question, yes it will persist across turns. No it will not persist across game save/load.
 
Back
Top Bottom