Uveso wrote:Hello Ghoustaq,
you problem was simply to execute the 2nd code block after the 1st block looped over all 3 locations.
The 2nd block needs to be called every "for k = 1, 3 do", not after it.
I optimized the code and removed unneeded locals.
Also added some failchecks in case the triggerunit is destroyed.
Copy & paste ready script:
- Code: Select all
local ScenarioUtils = import('/lua/sim/ScenarioUtilities.lua')
function OnPopulate()
ScenarioUtils.InitializeArmies()
ForkThread(HealthAdjustor)
end
function HealthAdjustor(self)
local location
local entities
local unitsA
local unitsB
local health
local delta
local ArmyBrain
while true do
for k = 1, 3 do
location = ScenarioUtils.MarkerToPosition('BuffArea_' .. k)
entities = GetUnitsInRect( Rect(location[1]-11, location[3]-11, location[1]+11, location[3]+11) )
if entities then
unitsA = EntityCategoryFilterDown(categories.MOBILE, entities)
unitsB = EntityCategoryFilterDown(categories.HYDROCARBON + categories.MASSEXTRACTION, entities)
end
if unitsB then
-- check first if the trigger unit is still present
if unitsB[1] and not unitsB[1].Dead and not unitsB[1]:BeenDestroyed() then
ArmyBrain = unitsB[1]:GetAIBrain() -- unitsB[1] is the first unit inside the unitsB table
for _, instigator in unitsA do
if VDist3(instigator:GetPosition(), location) <= 8 then
health = instigator:GetHealth()
delta = health*0.05
if instigator:GetAIBrain()~= ArmyBrain then
instigator:AdjustHealth(instigator, -delta)
else
instigator:AdjustHealth(instigator, delta)
end
end
end
end
end
end
WaitTicks(10) -- waits 1 second. Can be reduced to 1 tick if needed
end
end
@Uveso
Thank you indeed! You did a great job After solving this problem, I can go on mapping now