Jump to content

MrBill

Members
  • Posts

    38
  • Joined

  • Last visited

Community Answers

  1. MrBill's post in Scripting Help Needed. was marked as the answer   
    Thanks to all for weighing in on this topic.
    I found some of the negative issues in Chump's original code and kept trying to get this all to work.  Here are some key points that I learned:
    This death of a group thing is a LOT more involved than the simple Group Dead flag available in the editor.  And I'm only talking about airborne groups here. The checking logic needs to check multiple times and the Bandits are going down in flames. Testing this code took forever as I had to fly to do it...(boo hoo!) 😉 I had a GREAT time working through all this and now have a working script to share. Here's the working script.  Assumes groups are single unit are defined and late activated.  Hope this helps and have fun!
    local groupList = {"Bandit1", "Bandit2", "Bandit3"}
    local activatedGroups = {}
    local function allGroupsDead()
        for _, groupName in pairs(activatedGroups) do
            trigger.action.outTextForCoalition(coalition.side.BLUE, groupName .. " Being checked for death status...", 20)
            if Group.getByName(groupName) then
                return false
            end
        end
        return true
    end
    local function checkAllDead()
        if allGroupsDead() then
            trigger.action.outTextForCoalition(coalition.side.BLUE, "All targets (Groups) have been destroyed!", 20)
        else
            trigger.action.outTextForCoalition(coalition.side.BLUE, "Some Groups still Alive, Checking again soon...", 20)
            timer.scheduleFunction(checkAllDead, {}, timer.getTime() + 30)
        end
    end

    local function activateGroup()
        local groupName
        local counter = 0
        repeat
            groupName = groupList[math.random(#groupList)]
            counter = counter + 1
        until not activatedGroups[groupName] or counter > 100
        activatedGroups[groupName] = groupName
        -- if we don't use the counter this locks up in an infinite loop
        if counter > 100 then 
            trigger.action.outTextForCoalition(coalition.side.BLUE, groupName .. " No more groups to activate, waiting for death!", 20)
            checkAllDead()
            return
        end
        local group = Group.getByName(groupName)
        trigger.action.activateGroup(group)
        trigger.action.outTextForCoalition(coalition.side.BLUE, groupName .. " has been activated!", 20)
        return group
    end
    local function dumpEventInfo(event)
        local m = {}
        m[#m+1] = "Event ID: "
        m[#m+1] = event.id
        if event.initiator then 
            m[#m+1] = "\nInitiator : "
            m[#m+1] = event.initiator:getName()
        end
        if event.weapon then 
            m[#m+1] = "\nWeapon : "
            m[#m+1] = event.weapon :getTypeName()
        end 
        if event.target then 
            m[#m+1] = "\nTarget : "
            m[#m+1] = event.target :getName()
        end 
        trigger.action.outText(table.concat(m), 20)
    end
    local e = {}
    function e:onEvent(event)
        local m = {}
        if event.id == world.event.S_EVENT_KILL then
            trigger.action.outText(event.target:getName() .. " Killed by (KILL EVENT )" .. event.initiator:getName(), 20)
            if allGroupsDead() then
                trigger.action.outTextForCoalition(coalition.side.BLUE, "All targets (Groups) have been destroyed!", 20)
            else
                local delay = math.random(1 * 60, 3 * 60)
                timer.scheduleFunction(activateGroup, {}, timer.getTime() + delay)
            end
        end
        --dumpEventInfo(event)
    end
    world.addEventHandler(e)
    activateGroup()

     
×
×
  • Create New...