Posted on: 09-24-2012 - Updated on: 09-27-2012
World of Warcraft macros and addons are built around a programming language called LUA. Macros are just tiny pieces of code compared to addons, but they can do just about as much as an addon can – if you can fit it inside for 255 characters. In this tutorial guide, I’ll show you how to make a simple LUA scripted macro that retrieves the amount of achievements you have completed. Here’s the completed achievement macro that I’ll be updating more at a later time.
To run some LUA code in your macros, first start with /run or /script. That tells the macro to execute the LUA programming. Next, we’ll look at something simple, like printing some text to your chat panel. The print() function is just like echo in PHP or console logging in javascript. It simply writes some text, variable name, or return to the chat window:
/run print("hi noob")
That will print “hi noob” in your chat window. I’m funny huh? Ok, next we’ll look at printing the results of a function. We’ll take GetNumCompletedAchievements() function. This returns two things: The number of achievements there are in the game and the number that you have completed. The way we handle it having two returns is like this:
/run a,b = GetNumCompletedAchievements(); print(a,b)
That will set both returns to those variable names in the order of the documentation. It will then print them. Two things to note here. One, we separate lines of code with a semicolon (;) and two, you don’t have to specify both variables. So if you just wanted to see the total achievements, just use one variable.
If you don’t know what variable scope is, the basics is that you only want your variables (a and b from above) to be available inside this macro instance. The reason for this, is you don’t want to be a jerk and be the only person that ever uses a or b and you don’t want to conflict with addons that may be using a or b. So, we need to add “local” before declaring a variable. This tells the macro that a and b are only used inside this macro scope.
/run local a,b = GetNumCompletedAchievements(); print(a,b)
As you probably know, the If statement is probably the pinnacle of all addon development and programming in general. Here’s the syntax for it:
/run local x=3; if x>1 then print("noobsauce") end
I can really be funny at times, I know. Its just to make sure you stay reading. That’s the LUA macro If statement. It says if x is greater than 1, then do this print thing. Pretty simple. Got it? Good. Now let’s make a Wow macro that’s something useful.
So, there’s a function for getting achievement data, GetNumCompletedAchievements(). Let’s see how many achievements we have left to complete. The rest of this tutorial guide is dedicated to actually making something you will use and no more bad jokes.
/run local a,b = GetNumCompletedAchievements(); if a >b then print('You have completed ',b, ' out of ' ,a,' achievements') ; x= b/a*100;print('That is only ',x,' percent'); end
That macro prints out the screenshot below. I’ve added a rounding method to make it more appealing and added it to our list of macros. See the final achievement macro here. Sort of a handy macro and was super easy to create using LUA. You can find the entire API Blizzard has made on numerous websites. I suggest finding one you like and looking through all the different functions there are. When you write a new macro, be sure to come submit it here!
Hi
Been trying forever to get someone to help me code a script for a WHISPERED macro which states “May it serve you well, my Lady!”
I know how to do it with a simple “say it in white”, but I don’t know how to set up a WHISPER macro.
Can someone help me, please?
Thanks so much!