This site makes extensive use of JavaScript.
Please enable JavaScript in your browser.
Live
PTR
10.2.5
PTR
10.2.6
Macro linking weapon.
Post Reply
Return to board index
Post by
Suburu
Does anyone know how to make a macro that links an item? and Thank you :)
Post by
136091
This post was from a user who has deleted their account.
Post by
HighFive
Or you can download this. It's quite more comfortable for the average user.
Post by
RedwoodElf
/run local itemType, itemID, itemLink = GetCursorInfo() if itemID then SendChatMessage("WTS "..(select(2,GetItemInfo(itemID))),"channel",nil,2) end
That will link whatever you have on yoiur mousecursor. If you have nothing on your mouse cursor, it will do nothing.
Post by
pelf
Ooh, I like that one, RE. How about you add a global it saves the item link to which it uses if you don't have an item on the cursor so it kind of "remembers" the last one you picked up.
Post by
RedwoodElf
You mean like this:
/run local itemType, itemID, itemLink = GetCursorInfo() if itemID then XXitemXX=itemID end if XXitemXX then SendChatMessage("WTS "..(select(2,GetItemInfo(XXitemXX))),"channel",nil,2) endIf you have an item on the cursor, it will store the value in "XXitemXX" - then it checks to see if XXitemXX exists (if you have done it before to an item, but haven't got anything on your cursor this session, it will remember, but if you haven't set up an item, it will do nothing)
Post by
HighFive
Or a simple:/run _,XXitemXX=GetCursorInfo()or XXitemXX SendChatMessage(XXitemXX and("WTS "..XXitemXX)or"","CHANNEL",nil,2)
Post by
RedwoodElf
Or a simple:/run _,XXitemXX=GetCursorInfo()or XXitemXX SendChatMessage(XXitemXX and("WTS "..XXitemXX)or"","CHANNEL",nil,2)
Shouldn't there be an "if" statement in there somewhere?
Post by
Wildhorn
This is terribad HighFive...
Here is a working one:
/run _,_,XXitemXX=GetCursorInfo(); SendChatMessage(XXitemXX)
Btw, you need to have the item grabbed for GetCursorInfo() working and the item info is the 3rd, not the 2nd return highfive
Post by
Wanderingfox
This is terribad HighFive...
Here is a working one:
/run _,_,XXitemXX=GetCursorInfo(); SendChatMessage(XXitemXX)
Btw, you need to have the item grabbed for GetCursorInfo() working and the item info is the 3rd, not the 2nd return highfive
Not quite... I'm pretty sure GetCursorInfo() returns nil if there's nothing on the cursor... If that's the case, you'll need:
/run _,_,XXitemXX=GetCursorInfo() or XXitemXX; SendChatMessage(XXitemXX)
Post by
Wildhorn
Wtf is that "or XXitemXX" for? It does absolutely nothing.
And yes GetCursorInfo() return nil if nothing is grabbed, ince why I said was requiring you to grab something.
Or better:
/run _,_,X=GetCursorInfo(); if(X ~= nil) then SendChatMessage(X);end
And at the base... why are we wasting our time on a such macro? Just link it in chat normally with shift + click.
Post by
Wanderingfox
Expanded:
if XXitemXX ~= nil then XXitemXX = GetCursorInfo() else XXitemXX = XXitemXX.
The or XXitemXX prevents it from being set to nil, thus preserving it's information, which was the whole point of changing the macro in the first place. Your macro sets it to nil if you have nothing on the cursor, and then tries to display a nil item.
Post by
Wildhorn
Well, I tested it and "or XXitemXX" did nothing to prevent it to be nil, ince why I asked wtf it was for.
Post by
pelf
Yeah, don't use one-letter variable names in the global space.
And at the base... why are we wasting our time on a such macro? Just link it in chat normally with shift + click.
It's academic. A fun exercise and perhaps someone will learn something about Lua reading it.
So ... if the " or " thing isn't working properly, is it an operator precedence problem? Does the or operator have precedence over assignment? I would assume so, but you could parenthesize it, anyway. I assume you truncated the SCM call for posting brevity, but to expand it ... Also, don't we need to check for the case where the macro was activated before the global was ever set the first time? No cursor item and the global is nil.
/run _,_,XXitemXX=(GetCursorInfo() or XXitemXX); if XXitemXX then SendChatMessage("WTS "..XXitemXX,"CHANNEL",nil,2) end
Right?
Post by
RedwoodElf
Basically, you need some placeholders, since in your "or" case, you are assigning XXitemXX to "_"
/run _,_,XXitemXX=(GetCursorInfo() or _,_,XXitemXX); if XXitemXX then SendChatMessage("WTS "..XXitemXX,"CHANNEL",nil,2) end
Remember, when doing multiple assignments, you need to pass enough values to put the value you want in the right spot.
Post by
HighFive
It would appear that I made a slight miscalculation. I allowed my mind to be corrupted by sleep deprivation!
I did mean to store the link in XXitemXX, that was a typo. I was quite sure the rest was valid until I thought about it earlier today. It will likely not work, but It does raise some interesting questions about Lua syntax that need to test.
To the matter at hand:
lua-users wiki: Expressions Tutorial
said:
The or binary operator also does
not necessarily
return a boolean value (see notes for
and
above).
If the first argument is not false or nil it is returned, otherwise the second argument is returned
. So, a boolean is only returned if the first argument is true or the second argument is a boolean.> = true or false
true
> = true or nil
true
> = "hello" or "there", 1 or 0
hello 1All of the above expressions return the first argument. All of the following expressions return the second argument, as the first is false or nil.> = false or true
true
> = nil or true
true
> = nil or "hello"
helloThis can be a very useful property. For example, setting default values in a function:> function foo(x)
>> local value = x or "default" -- if argument x is false or nil, value becomes "default"
>> print(value, x)
>> end
>
> foo() -- no arguments, so x is nil
default nil
> foo(1)
1 1
> foo(true)
true true
> foo("hello")
hello helloAfter testing I can confirm that my suspicion was proved to be true and XXitemXX does indeed always become nil due to the fact that it's the third var in the list and there is no third value to be assigned (nil get assigned). A fix:/run XXitemXX=select(3,GetCursorInfo())or XXitemXX SendChatMessage(XXitemXX and "WTS "..XXitemXX or "","CHANNEL",nil,2)(If you call SendChatMessage with nil as the first arg it errors, but fails silently if you call it with and empty string)
edit: @RedwoodElf: Hmm, I could've sworn that didn't work when I tested it 1 minute ago. Huh.
Post by
RedwoodElf
Assignments are always made AFTER the result is evaluated (Otherwise, N=N+1 wouldn't work) So N = A or B should assign B if A is nil, assuming the or operation is evaluated before the assignment is made, correct?
Post by
pelf
/run _,_,XXitemXX=(GetCursorInfo() or _,_,XXitemXX); if XXitemXX then SendChatMessage("WTS "..XXitemXX,"CHANNEL",nil,2) end
After testing I can confirm that my suspicion was proved to be true and XXitemXX does indeed always become nil due to the fact that it's the third var in the list and there is no third value to be assigned (nil get assigned). A fix:/run XXitemXX=select(3,GetCursorInfo())or XXitemXX SendChatMessage(XXitemXX and "WTS "..XXitemXX or "","CHANNEL",nil,2)
Even if you can't use _ on the right side of an assignment, I suppose you could still just do this, right?
_,_,XXitemXX=(GetCursorInfo() or nil,nil,XXitemXX);
I'm on the fence as to which of those styles I think is more readable -- multiple assignment or select. Also, when to use semicolons in Lua is still a point of much -- I wouldn't say confusion, but -- contention for my brain. I suppose that the Lua parser can easily determine that the previous statement is done after the end of the assignment and that the next one is a function call; but, it just seems messy to leave it out. Obviously I'm speaking stylistically and not to the fact that everything should be dropped in macros where it can be to shorten them.
----
(If you call SendChatMessage with nil as the first arg it errors, but fails silently if you call it with and empty string)
Neat, that explains that.
Post by
HighFive
Upon further testing, I guess it really didn't work as expected.
I don't know why I didn't pick up on this immediately, the "or" operator takes only the first return value of the function and discards the rest.
For the same reason, your will fail too, pelf. Let me break that into lines for you:_ = GetCursorInfo() or nil
_ = nil
XXitemXX = XXitemXX
Post by
pelf
Alright, so we have to use select(...) to make this work. So, the final macro is the one that was in HF's last significant post:
/run xxlnk=select(3,GetCursorInfo())or xxlnk SendChatMessage(xxlnk and "WTS "..xxlnk or "","CHANNEL",nil,2)
I couldn't resist shortening that variable -- it was nagging me the whole time.
Post Reply
You are not logged in. Please
log in
to post a reply or
register
if you don't already have an account.