Select Random (Friendly) Target

I’m trying to create a macro to cast Power Infusion on a random mage in my raid group. I’d like the macro to target a player and then cast the spell.

Is it possible to create a macro to select a random target from either a list of names or a list of positions in either party or raid?

I’ve read through a lot of descriptions and can’t seem to piece together how to do it. The rand function will return a number, but I can’t associate that with a player. Likewise, the /tar command will work for a specific player, but not a list.

To be clear, I’m not looking to cast a random spell, or else /castrandom would work just fine. I’m looking to cast one spell on a randomly chosen person.

Nothing automated can make decisions about targeting and casting during combat. So you would need some logic that runs before hand while you are out of combat to decide who to cast on, then you can execute that cast later when you’re in combat.

The major problem with that, though, is you wouldn’t be able to know if your target is in range or alive at the time of the cast.

So it would be fairly tricky if not impossible to do what you’re asking properly.

1 Like

Well, you have some options:

While you’re unable to specify a range of a specific class from random, you could take advantage of Focus and TargetFriend.

If there’s a specific player that you want to maintain Power Infusion with:

#showtooltip Power Infusion
/clearfocus [@focus,dead][@focus,noexists][@focus,nohelp]
/focus [@focus,noexists]
/use [@focus,help] Power Infusion

While you can absolutely use a completely random system, you are open to targeting a player that is unable to utilise the bonus spell damage/healing.

In which case, could use:

/targetfriend
/cast Power Infusion
/targetlasttarget

But again, would not recommend this method, but is completely automated to cast on anyone, whether they can use, or not.

There is of course, a mouseover option, where you either mouse over the player, or raid frame, while pressing the corresponding keybind.

#showtooltip
/cast [@mouseover,help] Power Infusion; Power Infusion

This allows you the flexibility of choice, but may not me as automated as you’re asking. Also, not an option if you are clicking on the spell, as opposed to keybind.

So with some options here, I’d personally consider the first option, utilising Focus. You could even check if adding a /targetlasttarget line on the last row could work.

Wow. I don’t even know where to begin…

  1. The OP is using Classic where there is no focus
  2. Why would you focus and then cast Power Infusion on an enemy?
  3. Your second macro doesn’t work at all if the player has an enemy target. What’s the point of that?
  4. A plain mouseover macro doesn’t address the problem the OP is trying to solve at all.

None of these “options” are options at all since they are in no way relevant to what the OP is trying to achieve.

There may indeed be some creative way to achieve the requested functionality, but there is nothing in this post that even comes close to acknowledging what was asked.

You’re right, I don’t play Classic, and wasn’t aware of Focus not being in the game.
Fair call.

While I have stuffed up in the Focus department, I haven’t completely dismissed what the OP is asking, and tried to help in ways that could be an option, like @mouseover (which does work).

While I have covered the final question to cast one spell on a randomly chosen person, I find that using the /castfriendly option is too random for the reasons mentioned.

No, the OP didn’t ask for a mouseover…
But instead of blindingly stating what can’t be done, this is showing the options that can be utilised. Of which, @mouseover is a legit option, just not what they were specifically after.

1 Like

The following is not fully tested, but it targets a random group member when you use /click TargetRandomGroupMember in a macro (can use it just before a spell cast). It won’t/can’t choose only people in range and it won’t prevent choosing the same person twice. To make it for mage-only, change the isTargetable function as mentioned in the comments:

-- This creates a secure button to target a random group (raid or party) member
-- To use: /click TargetRandomGroupMember
-- To be more selective on who to target, update the function isTargetable()

-- return true if the given unit ("raid22","party3") should be a candidate for targeting
local function isTargetable(unit)
    return true -- change to return UnitClass(unit)=="Mage" to target only random mages
end
  
-- generates a list of units as attributes randomUnit1, randomUnit2, ..., randomUnit[randomNumUnits]
-- that are the units to target, such as randomUnit1="raid3", randomUnit2="raid14", etc
local function updateUnits(button)
    -- can't set unit attributes in combat
    if InCombatLockdown() then
        return
    end
    button:SetAttribute("randomNumUnits",0)
    -- start building list of units again
    local numUnits = 0
    local groupType = IsInRaid() and "raid" or "party"
    for i=1,groupType=="raid" and 40 or 5 do
        local unit = (groupType=="party" and i==5) and "player" or groupType..i
        if UnitExists(unit) and isTargetable(unit) then
            numUnits = numUnits + 1
            button:SetAttribute("randomUnit"..numUnits,unit)
        end
    end
    button:SetAttribute("randomNumUnits",numUnits)
end

-- /click TargetRandomGroupMember will click this button that's never made visible
local button = CreateFrame("Button", "TargetRandomGroupMember", UIParent, "SecureActionButtonTemplate")

-- set up events so updateUnits is called when logging in or the group changes
button:RegisterEvent("PLAYER_LOGIN")
button:RegisterEvent("GROUP_ROSTER_UPDATE")
button:SetScript("OnEvent",updateUnits)

-- the /click will target a to-be-chosen unit (chosen in the pre-click wrap below)
button:SetAttribute("type","target")

-- set up pre-click wrap to pick a random unit (or the player if no candidates)
SecureHandlerWrapScript(button,"OnClick",button,[[
    local numUnits = self:GetAttribute("randomNumUnits") or 0
    self:SetAttribute("unit",numUnits>0 and self:GetAttribute("randomUnit"..random(numUnits)) or nil)
]])

Paste the above code to addon.bool.no to turn it into an addon.

If an addon author wants to give it a shot, you’re welcome to take this code and create your own /targetrandom slash command that will take a list of names (or classes) as arguments, and then add a /click to the macro immediately after the unsecure /targetrandom. (The addon Select does this if you need an example how.)

4 Likes

Creative posts like yours are the main reason I love to read the macro forum. Bravo.

is there a way to work the /targetfriend into a cast marco
ive tryed /cast [@targetfriend] and [@targetfriendplayer]

#showtooltip
/targetfriend
/cast SPELL

That is what i am currently running i was just trying to see if there was a way to make it a one liner