This site makes extensive use of JavaScript.
Please enable JavaScript in your browser.
Live
PTR
10.2.5
PTR
10.2.6
Macro/Script help!
Post Reply
Return to board index
Post by
800521
This post was from a user who has deleted their account.
Post by
pelf
-- set up the function to maintain
quantityToMaintain
of an item
name
by buying
-- however many are necessary to return the
currentQuantity
in the player's bags to
quantityToMaintain
local function BuyMerchantItem(name, quantityToMaintain)
local currentQuantity =
GetItemCount
(name);
if currentQuantity < quantityToMaintain then
for i = 1, 100 do
if name ==
GetMerchantItemInfo
(i) then
BuyMerchantItem
(i, quantityToMaintain - currentQuantity);
end
end
end
end
-- buy all the stuff you want, by name
BuyMerchantItem("Instant Poison ", 20);
BuyMerchantItem("Deadly Poison ", 20);
BuyMerchantItem("Wound Poison", 40);
BuyMerchantItem("Crippling Poison", 20);
Cool macro. I renamed some stuff with short names and added some API links. Does that help?
The only thing I'm curious about is why there's a space after the word Poison in Instant and Deadly. Either that's a typo and it doesn't matter or the name that GetMerchantItemInfo returns actually has a spurious space in it that the macro has to account for. I'm gonna add this to my macro post.
Also, you could save space modifying it to buy by ID instead of name.
Post by
800521
This post was from a user who has deleted their account.
Post by
Nipah
Yes, the spaces were accidental. :P
This helps a lot, but I am still confused by a couple of things. What does the line "for i = 1, 100 do" mean? Also what is the purpose of using "==" in "n==GetMerchantItemInfo" instead of just one "=" like all the rest of the named variables?
I can sort of answer the first question.
i = 1 sets up the variable, starting at one.
100 sets up how many times it will go through that loop.
So if the name variable (the poisons) == the name of the merchant item in slot (i), then it will buy X of them (where X = quantityToMaintain).
If the name variable != (not equal) the name of the merchant item in slot (i), then i is incremented by 1 (i++) {though this isn't stated in the if statement, I'm assuming this is built in}, and the loop is run again.
== checks to see if its equal to something. so "if blue == color" is a check to see if its equal, while "color = blue" means that the variable named color holds the value of "blue".
... maybe someone with more ability can explain that bit better.
Post by
Ketho
you can play around in the
Lua Live Demo
to test out how the language works ;)
For loop:
1
,
2
,
3
,
4
for blah = 1, 10 do
print(blah)
end
Relational Operators:
1
,
2
,
3
local a = 7
local b = 7
print( a == b ) -- evaluates to true
print( a ~= b ) -- evaluates to false
Post by
pelf
Also what is the purpose of using "==" in "n==GetMerchantItemInfo" instead of just one "=" like all the rest of the named variables?
==
is a comparison operator for equality.
If you look at the API for GetMerchantItemInfo, it says this...
name, texture, price, quantity, numAvailable, isUsable, extendedCost = GetMerchantItemInfo(index)
If you don't do anything about it, the first thing in the list of returned values is the thing that's used, so name is what it returns. The line says...
if name == GetMerchantItemInfo(i) then
name
is the one that was passed in, so it's saying, "If the name that was passed to the function matches the name of the merchant item currently being looked at (by the
for
loop), then..."
Post by
TheReal
...and to complete the explanation,
=
is used as an assignment operator. It's only used to assign a value to a variable.
Post by
800521
This post was from a user who has deleted their account.
Post by
Zxian
While the thread does have it's educational merits, there's a great, super-lightweight addon for this called
GetReagents-Lite
. :)
Post by
INVGI
-- set up the function to maintain
quantityToMaintain
of an item
name
by buying
-- however many are necessary to return the
currentQuantity
in the player's bags to
quantityToMaintain
local function BuyMerchantItem(name, quantityToMaintain)
local currentQuantity =
GetItemCount
(name);
if currentQuantity < quantityToMaintain then
for i = 1, 100 do
if name ==
GetMerchantItemInfo
(i) then
BuyMerchantItem
(i, quantityToMaintain - currentQuantity);
end
end
end
end
-- buy all the stuff you want, by name
BuyMerchantItem("Instant Poison ", 20);
BuyMerchantItem("Deadly Poison ", 20);
BuyMerchantItem("Wound Poison", 40);
BuyMerchantItem("Crippling Poison", 20);
.
Look like smart thing, but how i can place it in 255 symbol limit?
p.s.
Adapted for 3.3.5a (183 symbols):
/script local function Buy(n, m) local q = GetItemCount(n); if q < m then for i = 1,100 do if n == GetMerchantItemInfo(i) then BuyMerchantItem(i,m-q); end end end end Buy("*****",20)
Post Reply
You are not logged in. Please
log in
to post a reply or
register
if you don't already have an account.