• You've discovered RedGuides, an EverQuest multi-boxing and scripting community 🧙‍♀️⚙️. We want you to play several EQ characters at once, come join us and say hello! 👋

  • A TLP without truebox has thawed (Very Vanilla ready)
    Frostreaver

Problem - Triggering a function when the last mob in Xtarget dies

Joined
Mar 20, 2024
RedCents
1,051¢
I am struggling triggering a function called "ResetCombatMode()" , which is meant to be triggered just once, immediately as the last mob in the XTarget list dies off. Basically, when the XTarget list goes from 1 mob to 0 mobs.

I have tried different coding logics, all of them involving some sort of flag true/false to trigger the function from inside the code. The problem that I am having is that, no matter what coding logic I apply, the function triggers every time the code routine cycle runs and there's zero mobs in the XTarget list. I cannot make it to trigger just once immediately upon killing off the last mob.

Can anyone help me with the right logic approach please?
 
set a flag to true once a mob is on the xtarget list, and then only if that flag is true will it run the code if there is no mobs on xtarget, and in that code you also set the flag to false
 
Yes, the flag approach is what I have tried. The problem is that, at the top of my code, I declare the variable local XTargetResetFlag = false so while I set it to true mid-way through the code, it always resets back to false every time the code routine cycles over and over.
 
Sure, my bad. So far, I have this code which I have put together with the help of chatGPT:

Code:
-- Initialize a flag to track if the function has been triggered
local functionTriggered = false

-- Function to check the XTarget list and trigger an action when the last monster dies
function CheckXTargetList()
    -- Get the number of XTarget entries
    local xTargetCount = mq.TLO.Me.XTarget() -- Adjust this as per your setup if needed

    -- Check if there are no active targets and the function has not been triggered yet
    if xTargetCount == 0 then
        if not functionTriggered then
            -- Trigger the function
            OnLastMonsterDied()
            -- Set the flag to true to ensure the function is only triggered once
            functionTriggered = true
        end
    else
        -- If there are active targets and the flag is set, it indicates new targets have appeared
        if functionTriggered then
            functionTriggered = false
        end
    end
end

-- Function to be triggered when the last monster in the XTarget list dies
function OnLastMonsterDied()
    print("The last monster in the XTarget list has died!")
    -- Add more custom logic here
end

-- Main loop to perform actions
while true do
    CheckXTargetList()
    mq.delay(1000) -- delay in milliseconds to check every second
end

The problem, I think, is that every time the code cycles, the parameter local functionTriggered = false resets back to false, undoing the functionTriggered = true flag I have stored waiting to be activated when the last mob in XTarget dies.
 
Sure, my bad. So far, I have this code which I have put together with the help of chatGPT:

Code:
-- Initialize a flag to track if the function has been triggered
local functionTriggered = false

-- Function to check the XTarget list and trigger an action when the last monster dies
function CheckXTargetList()
    -- Get the number of XTarget entries
    local xTargetCount = mq.TLO.Me.XTarget() -- Adjust this as per your setup if needed

    -- Check if there are no active targets and the function has not been triggered yet
    if xTargetCount == 0 then
        if not functionTriggered then
            -- Trigger the function
            OnLastMonsterDied()
            -- Set the flag to true to ensure the function is only triggered once
            functionTriggered = true
        end
    else
        -- If there are active targets and the flag is set, it indicates new targets have appeared
        if functionTriggered then
            functionTriggered = false
        end
    end
end

-- Function to be triggered when the last monster in the XTarget list dies
function OnLastMonsterDied()
    print("The last monster in the XTarget list has died!")
    -- Add more custom logic here
end

-- Main loop to perform actions
while true do
    CheckXTargetList()
    mq.delay(1000) -- delay in milliseconds to check every second
end

The problem, I think, is that every time the code cycles, the parameter local functionTriggered = false resets back to false, undoing the functionTriggered = true flag I have stored waiting to be activated when the last mob in XTarget dies.

From my general programming knowledge, it looks like you have two variables. One used in your parent script (everything you posted that is not in a function) and the one used in the CheckXTargetList function.

What I see this doing is:
- Setting the script local functionTriggered to false
- Setting the two functions
- Running the main loop
- Main loop runs the CheckXTargetList
- Function see 0 mobs
- Function sees an if/else on an undeclared variable
- Function does else and declares functionTriggered as false (local to the function) or it could just error out and not doing anything except move on.
- Function collapses destroying everything just set
- Main loop delays
- Main loop repeats

When I work on nested ifs I use echo/consol log commands to show what is happening at each step and comment out the ones that are satisfactory.

Now there might be some things with mq that would change some of this (my google search on how to program in mq leave me reluctant to try unless I see working examples I can decipher).


Hope this helps.
 
Last edited:
A possible direction to go in, would be to pass your outer variable to the function and return a true/false value:

functionTriggered = CheckXTargetList(functionTriggered) -- Call the function passing this variable's data

function CheckXTargetList(flaggedXtarget) -- Run the function while setting a variable for the passed data

return true -- Return to the function caller the data specified (true)

return false

I would also combine your if's so they are not nested.
 
Problem - Triggering a function when the last mob in Xtarget dies

Users who are viewing this thread

Back
Top
Cart