BuffArea_1 doesn't work. what's wrong?

Interesting mapping tools and mapping help.

Moderator: Morax

BuffArea_1 doesn't work. what's wrong?

Postby Ghoustaq » 05 Apr 2018, 19:58

I created 2 Buffareas where units will get heal if a massextrator has been bulit there if not they will get damage.

The problem is why Buffarea_2 works and BuffArea_1 doesn't work

Here is my script code.

Code: Select all
local ScenarioUtils = import('/lua/sim/ScenarioUtilities.lua')
local ScenarioFramework = import('/lua/ScenarioFramework.lua')

function OnPopulate()
   ScenarioUtils.InitializeArmies()   
end

function OnStart(self)

   ForkThread(HealthAdjustor)
end

   local BuffAreaName
   local location
   local px, py, pz
   local rect
   local entities
   local unitsA
   local unitsB
   local health
   local preAdjHealth
   local delta
   local ArmyBrain
function HealthAdjustor(self)
    while true do
      for k = 1, 2 do
        BuffAreaName = 'BuffArea_' .. k
        location = ScenarioUtils.MarkerToPosition(BuffAreaName)
        px, py, pz = unpack(location)
        rect = Rect(px-11, pz-11, px+11, pz+11)
        entities = GetUnitsInRect(rect)
          if entities then
           unitsA = EntityCategoryFilterDown(categories.MOBILE, entities)
           unitsB = EntityCategoryFilterDown(categories.HYDROCARBON + categories.MASSEXTRACTION, entities)
          end
      end

      if unitsB ~= nil then
         if table.getn(unitsB) >= 1 then
            for _, supplier in unitsB do
            ArmyBrain = supplier:GetAIBrain()
                   end
            end
            for _, instigator in unitsA do
                if VDist3(instigator:GetPosition(), location) <= 8 then
                    if instigator:GetAIBrain()~= ArmyBrain then
                       health = instigator:GetHealth()
                       delta = health*0.05
                       instigator:AdjustHealth(instigator, -delta)
                    else
                       instigator:AdjustHealth(instigator, delta)
                    end
                end
            end
         end

        WaitSeconds(10)
    end
end
Last edited by Ghoustaq on 06 Apr 2018, 14:41, edited 1 time in total.
User avatar
Ghoustaq
Avatar-of-War
 
Posts: 65
Joined: 25 Dec 2017, 13:54
Has liked: 10 times
Been liked: 0 time
FAF User Name: Ghoustaq

Re: BuffArea_2 doesn't work. what's wrong?

Postby Uveso » 06 Apr 2018, 00:43

Hello Ghoustaq,

got this error:
Code: Select all
WARNING: Error running lua script: ...data\faforever\repo\fa\lua\sim\scenarioutilities.lua(101): ERROR: Invalid marker name- BuffArea_1


I can't check the function without the map / mapmarker.
User avatar
Uveso
Supreme Commander
 
Posts: 1788
Joined: 11 Dec 2015, 20:56
Location: Germany
Has liked: 70 times
Been liked: 291 times
FAF User Name: Uveso

Re: BuffArea_2 doesn't work. what's wrong?

Postby Ghoustaq » 06 Apr 2018, 15:00

Uveso wrote:Hello Ghoustaq,

got this error:
Code: Select all
WARNING: Error running lua script: ...data\faforever\repo\fa\lua\sim\scenarioutilities.lua(101): ERROR: Invalid marker name- BuffArea_1


I can't check the function without the map / mapmarker.


yes, it seems only the last buffarea works no matter how many buffareas exist on the map.
User avatar
Ghoustaq
Avatar-of-War
 
Posts: 65
Joined: 25 Dec 2017, 13:54
Has liked: 10 times
Been liked: 0 time
FAF User Name: Ghoustaq

Re: BuffArea_2 doesn't work. what's wrong?

Postby Uveso » 06 Apr 2018, 15:40

Ghoustaq wrote:yes, it seems only the last buffarea works no matter how many buffareas exist on the map.


no :)

Markers are just stored in a simple table/array.
Maybe you messed up the array index or something else.

This should/can work.

If you like, zip the actual map and post or upload it.
Let me play around with it.
I am sure it's just a typo or something like that.

[Edit] But not today! I will play the whole night Mechwarrior online :D
User avatar
Uveso
Supreme Commander
 
Posts: 1788
Joined: 11 Dec 2015, 20:56
Location: Germany
Has liked: 70 times
Been liked: 291 times
FAF User Name: Uveso

Re: BuffArea_1 doesn't work. what's wrong?

Postby Ghoustaq » 07 Apr 2018, 08:27

@Uveso

Here is my map.Please check it when you are free. ;)
BUFFAREA_TEST1.rar
(295.18 KiB) Downloaded 88 times
User avatar
Ghoustaq
Avatar-of-War
 
Posts: 65
Joined: 25 Dec 2017, 13:54
Has liked: 10 times
Been liked: 0 time
FAF User Name: Ghoustaq

Re: BuffArea_2 doesn't work. what's wrong?

Postby speed2 » 07 Apr 2018, 08:52

Ghoustaq wrote:
Uveso wrote:Hello Ghoustaq,

got this error:
Code: Select all
WARNING: Error running lua script: ...data\faforever\repo\fa\lua\sim\scenarioutilities.lua(101): ERROR: Invalid marker name- BuffArea_1


I can't check the function without the map / mapmarker.


yes, it seems only the last buffarea works no matter how many buffareas exist on the map.

So I cleaned the indentation to make it more readable and this is the result: (no code was changed, just white space) https://pastebin.com/t6sjjxsT

The reason why only the last area works should be visible now. You loop through the 3 areas to set the locals, and after that loop you work with them.
By the end of the for k = 1, 3 do loop, only the last variables are saved.
User avatar
speed2
Contributor
 
Posts: 3189
Joined: 05 Jan 2013, 15:11
Has liked: 636 times
Been liked: 1119 times
FAF User Name: speed2

Re: BuffArea_1 doesn't work. what's wrong?

Postby Ghoustaq » 08 Apr 2018, 18:57

@speed2

what does this mean?

Code: Select all
WARNING: Error running OnRelease script in <deleted object>: ...liance\gamedata\lua.scd\lua\ui\game\construction.lua(832): Game object has been destroyed
         stack traceback:
            [C]: in function `SetAlpha'
            ...liance\gamedata\lua.scd\lua\ui\game\construction.lua(832): in function `OnRolloverEvent'
            ...d alliance\gamedata\mohodata.scd\lua\maui\button.lua(131): in function <...d alliance\gamedata\mohodata.scd\lua\maui\button.lua:122>
User avatar
Ghoustaq
Avatar-of-War
 
Posts: 65
Joined: 25 Dec 2017, 13:54
Has liked: 10 times
Been liked: 0 time
FAF User Name: Ghoustaq

Re: BuffArea_1 doesn't work. what's wrong?

Postby speed2 » 08 Apr 2018, 20:08

Ghoustaq wrote:@speed2

what does this mean?

Code: Select all
WARNING: Error running OnRelease script in <deleted object>: ...liance\gamedata\lua.scd\lua\ui\game\construction.lua(832): Game object has been destroyed
         stack traceback:
            [C]: in function `SetAlpha'
            ...liance\gamedata\lua.scd\lua\ui\game\construction.lua(832): in function `OnRolloverEvent'
            ...d alliance\gamedata\mohodata.scd\lua\maui\button.lua(131): in function <...d alliance\gamedata\mohodata.scd\lua\maui\button.lua:122>

That's some error from the UI, specifically from the construction bar. Not related to the map script. Unless you're modding the UI, you don't need to care about it, nothing game breaking.
User avatar
speed2
Contributor
 
Posts: 3189
Joined: 05 Jan 2013, 15:11
Has liked: 636 times
Been liked: 1119 times
FAF User Name: speed2

Re: BuffArea_1 doesn't work. what's wrong?

Postby Ghoustaq » 09 Apr 2018, 17:53

@speed2

I simplified my code, then all these buffareas works, although I gave up defining 'unitsA' and 'unitsB'.

I wonder why defining unitsA and unitsB or not will make such a distinct difference?

In my original method, I tried to make that if unitsA and unitsB belong to the same army, the mobile units in unitsA will gain HP, if not, they will lose HP.

Code: Select all
local ScenarioUtils = import('/lua/sim/ScenarioUtilities.lua')
local ScenarioFramework = import('/lua/ScenarioFramework.lua')

function OnPopulate()
    ScenarioUtils.InitializeArmies()   

   


local BuffAreaName
local location
local px, py, pz
local rect
local entities
local unitsA
local unitsB
local health
local preAdjHealth
local delta
local ArmyBrain

local self = import('/lua/sim/Entity.lua').Entity()

        for k = 1, 3 do
            BuffAreaName = 'BuffArea_' .. k
            location = ScenarioUtils.MarkerToPosition(BuffAreaName)           
            ForkThread(HealthAdjustor, self, location)

        end
end
HealthAdjustor = function(self, location)
    local px, py, pz = unpack(location)
    local rect = Rect(px-11, pz-11, px+11, pz+11)
    local entities
    while true do
        entities = GetUnitsInRect(rect)
        if entities ~= nil then
            for _, instigator in entities do
                if VDist3(instigator:GetPosition(), location) <= 8 then
                        health = instigator:GetHealth()
                        delta = health*0.05
                        instigator:AdjustHealth(instigator, -delta)
                else
                        instigator:AdjustHealth(instigator, delta)
                end
            end
        end

        WaitSeconds(10)
    end
end

function OnStart(self)
end
User avatar
Ghoustaq
Avatar-of-War
 
Posts: 65
Joined: 25 Dec 2017, 13:54
Has liked: 10 times
Been liked: 0 time
FAF User Name: Ghoustaq

Re: BuffArea_1 doesn't work. what's wrong?

Postby Uveso » 10 Apr 2018, 01:42

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

User avatar
Uveso
Supreme Commander
 
Posts: 1788
Joined: 11 Dec 2015, 20:56
Location: Germany
Has liked: 70 times
Been liked: 291 times
FAF User Name: Uveso

Next

Return to Mapping

Who is online

Users browsing this forum: No registered users and 1 guest