Simple addon to run some code upon pressing a keyboard button

With the death of extended macros in TWW, I’m looking to transfer a lengthy macro into an addon to do the same thing. Nothing super fancy, just a non-combat helper macro that unfortunately requires a lot of characters.

Anyone have a simple block of lua for an addon to basically execute its code upon pressing a particular keyboard button?

Depending on the code, it’s probably easier to put your code into a unique global function as an addon an just /run that from the macro.

I use this for my worldmarker spam macro.
https://wago.io/WLcXW-0JN

Haven’t tested it in prepatch.

I see. So I put my code into one of my simpler existing addons like this:

function GavMacroCode()
  [my code]
end

Then in game, made a macro that says /run GavMacroCode. Nothing seems to happen, but no errors. I’m sure I’m doing something wrong. Any advice?

/run GavMacroCode()

Looks like the new /click limitations have broken my implementation.

Ive been trying to find where these limitation are spelled out. So far, all I have come across is this: https://warcraft.wiki.gg/wiki/Patch_11.0.0/API_changes#Click_events

It’s not “spelled out” but you can see some author discussion around it that worked it’s way through the Beta process.
https://github.com/Stanzilla/WoWUIBugs/issues/552

1 Like

Lol :upside_down_face: thx, that worked!

A word of caution, a function created like this is global so make sure you use a unique name. You have but I thought is was worth pointing out.

Second if you want more functions you can limit that by putting your functions in a table like this

GavMacroCode = {}
function GavMacroCode:function1()
   //Code
end
function GavMacroCode:function2()
   //Code
end

then you can call it with
/run GavMacroCode:function1()
and
/run GavMacroCode:function2()
This will give you as many functions you need but only one global variable.

1 Like

Apologies for resurrecting this thread, but all of a sudden with 11.1.5, this isn’t working anymore. Weird thing is, no errors are thrown. Just… nothing happens when clicking the macro. Any ideas?

Simple test type
/run print(GavMacroCode)

Replace the function name with whatever the name the of the function is you want to test.

This script should print function:xxxxxxxx where xxx is a bunch of numbers/letters.

If it doesn’t then the function doesn’t exists beacause … reasons (you removed/disabled the addon, an error stopped the function being created…?)

If you do get the “function” printed out then your function probably has been effected by a change in the API so we would need to see the code.
https://warcraft.wiki.gg/wiki/Patch_11.1.5/API_changes

If you use a table as in GavMacroCode:function1()
then you would
/run print(GavMacroCode.function1)` using a dot to replace the colon.

Nice, that’s super helpful. The script did return a function ID. Odd that it would be an issue with the function code, since I would assume an API change would throw an error of some kind. I perused the change log and couldn’t spot anything that I’m using in my code (below), but I must be missing something.

Basically using it to sell all gray items to vendors. Wonder if this has something to do with the new ability to set different colors for the various item quality types, since this keys off the default color for grays?

EDIT: Ah, wonder if this is it (from the change log):

“For cases where item quality colors are displayed in strings, new markup support has been added in the form of |cnIQn:text|r where n corresponds to a numeric [Enum.ItemQuality] value.”

I’m too tired to look into it now, lol, but I’m thinking this is probably the cause.

c = C_Container
if GetMerchantNumItems()>0 then
  for b = 0, 4 do
	for s = 1, c.GetContainerNumSlots(b) do
	  local n = c.GetContainerItemLink(b,s)
	  if n and string.find(n,"ff9d9d9d") then
		DEFAULT_CHAT_FRAME:AddMessage("Sold: "..n)
		c.UseContainerItem(b,s)
	  end
	end
  end
end

if n and string.find(n,"ff9d9d9d") then
This is looking for a color code in the link text so that’s probably it.

I haven 't change my colors and looking for \124cff9d9d9d|Hitem (essentially the same thing) finds the default grey.

If you change your default grey item colour, you will need to update the function.

It looks like you might be able to replace
if n and string.find(n,"ff9d9d9d") then

with

if n and string.find(n, ColorManager.GetColorDataForItemQuality(Enum.ColorOverride.ItemQualityPoor).color:GenerateHexColor()) then

And not worry about what colour you change the poor items too (unless you have several item rarities using the same colour and then you’re gonna loose some stuffs you didn’t intend to :wink:). For retail only atm.

To check you can run

/run print(ColorManager.GetColorDataForItemQuality(Enum.ColorOverride.ItemQualityPoor).color:GenerateHexColor())

and then open the color options, click the the ColorPicker for the required quality (poor in this case) and make sure the values are the same (the # value in the picker). Differing upper/lower case alphabet characters are not significant and the picker won’t have the ff (alpha) prefix)

I haven’t changed any of my colors. I can’t figure out why the original code isn’t doing anything. :frowning:

Try this:

  c = C_Container
  if GetMerchantNumItems()>0 then
    for b = 0, 4 do
  	for s = 1, c.GetContainerNumSlots(b) do
  	  local n = c.GetContainerItemLink(b,s)
  	  local color
  	  if ColorManager then -- custom item quality colours TWW 11.1.5 atm.
  	      color = format("\124cnIQ%s:", Enum.ItemQuality.Poor)
  	  else
  	      color = "ff9d9d9d" -- every other earlier version
  	  end
  	  if n and strfind(n, color) then
  	      DEFAULT_CHAT_FRAME:AddMessage("Sold: "..n)
  	      c.UseContainerItem(b,s)
  	  end
  	end
    end
  end

That should work for all client versions using the new item quality link field in TWW

It looks like with the new Item Quality link field, the quality color itself is no longer added.

That did it! Thanks. U da best :slight_smile:

Any chance I could see your code? I don’t really want to write from scratch and learn LUA for it.