WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Macro Help (https://www.wowinterface.com/forums/forumdisplay.php?f=140)
-   -   Using addon in a macro (https://www.wowinterface.com/forums/showthread.php?t=45145)

gmarco 11-14-12 01:45 AM

Using addon in a macro
 
Hi all,
I have written a tiny addon that can summon different mounts randomly on categories depending on a custom parameter it will be passed.

The general syntax is

/mrc category1
/mrc category2
etc etc

Now I like to use this addon in a macro so something like:

/mrc [btn:1] category1
/mrc [btn:2] category2

but I suppose I have to use something more than this and probably to modify the addon to add some code to deal with it.

P.s.
The addon discussion and the addon is here:
http://www.wowinterface.com/forums/s...ad.php?t=45113

Thanks for attention.

semlar 11-14-12 02:00 AM

Try /run SlashCmdList["MOUNTRNDCAT"]("category1")

SDPhantom 11-14-12 04:36 AM

If you're saying you want to implement macro option parsing for your addon, you can use SecureCmdOptionParse() for this.
Code:

SlashCmdList["MOUNTRNDCAT"] = function(args)
        local category=SecureCmdOptionParse(args);
        if not category then return; end
        category=category:lower();

        if category=="" then
                print("MountRandomCategory v.0.2");
                print("Usage: /mrc category");
        elseif mounts[category] then
                if IsMounted() then
                        Dismount();
                end

                local number=random(1,#mounts[category]);
                local picked=mounts[category][number]:lower();
                print("casting ...",picked);

                for index=1,GetNumCompanions("MOUNT") do
                        local _,name=GetCompanionInfo("MOUNT",index);
                        if name:lower()==picked then
                                CallCompanion("MOUNT",index);
                        end
                end
        else
                print("MountRandomCategory v.0.2");
                print("Unable to find category",category);
        end
end

I've corrected a few issues, mainly values leaking out into the global namespace that didn't need to be out there. This is controlled by using the local keyword when defining a variable for the first time. I also had the random mount name cached so the CPU wouldn't be used up so much with repetitive indexing of the mounts table.

On a side note, the usage of the underscore as a variable has no special meaning. It's just another valid variable name and is often used as such to shove unwanted values into to fetch values further down the list.

gmarco 11-14-12 05:35 AM

Hi,

Thanks really very much for you replies and fixes to my bad code:-)

I'll apply them and try as soon as I return home from work in the afternoon.

Thanks again.

gmarco 11-14-12 04:46 PM

Hi,

It seems to works (almost) everything.

If I create a simple macro like this:

/mrc [btn:1] ground
/mrc [btn:2] flying
... etc etc ...

It works like expected ... button1 cast a ground mount, button2 cast a flying mount but I got an error in bugrabber.

Code:

MountRndCat-0.4\core.lua:11: bad argument #1 to "lower" (string expected, got nil)
<in C code>
MountRndCat-0.4\core.lua:11: in function "?"
FrameXML\ChatFrame.lua:4358: in function "ChatEdit_ParseText"
FrameXML\ChatFrame.lua:4052: in function "ChatEdit_SendText"
FrameXML\ChatFrame.lua:2727: in function <FrameXML\ChatFrame.lua:2720>
<in C code>
FrameXML\SecureTemplates.lua:275: in function "handler"
FrameXML\SecureTemplates.lua:560: in function <FrameXML\SecureTemplates.lua:508>
<in C code>
FrameXML\SecureHandlers.lua:264: in function <FrameXML\SecureHandlers.lua:261>
<in C code>
FrameXML\SecureHandlers.lua:294: in function <FrameXML\SecureHandlers.lua:277>
(tail call): ?

Locals:
args = "[btn:2] flying"
category = nil

The code is this one:

Code:

SLASH_MOUNTRNDCAT1 = "/mntrndcat";
SLASH_MOUNTRNDCAT2 = "/mrc";
SlashCmdList["MOUNTRNDCAT"] = function(args)
       
        local category=SecureCmdOptionParse(args);
        category = string.lower(category)
       
        if (category == "" ) then       
               
                print ("MountRandomCategory (mrc for friends)");
                print ("Usage: /mrc your_category\n");       
                       
        else       
               
                if(IsMounted()) then
                        Dismount();
                end       
               
                local number=random(1,#mounts[category]);
                local picked=mounts[category][number];
       
                print ("mrc is summoning for you: " .. "[" .. category .. "] " .. picked);
               
                -- Protected code
                -- CastSpellByName(mounts[category][number])
               
                -- Non protected code
                for index=1,GetNumCompanions("MOUNT") do
                        local _,name=GetCompanionInfo("MOUNT",index);
                        if name==picked then
                                CallCompanion("MOUNT",index);
                        end
                end
                -- End non protected code
               
        end
       
end

Thanks for your attention.

SDPhantom 11-14-12 05:20 PM

I fixed the code I posted earlier. It appears when none of the conditions match for SecureCmdOptionParse(), it returns nil. At this point, the command should not be run. I added the check for this and have it exit immediately. I also fixed a potential error if the user were to call for a category that doesn't exist. The code is also more robust in the way that both the names in the table and the category name given through the arguments are case-insensitive.

gmarco 11-14-12 11:56 PM

Thanks really much for the patches and fixes SDPhantom.

I have corrected and modified the core.lua and everything seems to work great ...

Only a little thing seems to not work as expected and I report (and continue to stress you :-) because I really don't understand why it is happening.

The addon works great if you create a macro:

Code:

/mrc [btn:1] ground
/mrc [btn:2] flying
/mrc [btn:3] repair



But if add something like:

Code:

/mrc [mod:shift, btn:1] funny
it writes two lines from the print:

Quote:

mrc is summoning... fiera della notte striata
mrc is summoning... tartaruga di mare
but casts only the mount from the first group (displaying an ui error "that I am doing another action").

Probably it is due the fact it matches both the btn:1 condition and the shift+btn:1 condition retrieved by SecureCmdOptionParse.

Now I experimenting a little bit and googling around to understand why this condition is not treated as one condition and not as matched as two different conditions.

Thanks again really for your kind replies.

Phanx 11-15-12 02:06 AM

If this is your macro:
Code:

/mrc [btn:1] ground
/mrc [btn:2] flying
/mrc [btn:3] repair
/mrc [mod:shift, btn:1] funny

Then the two lines highlighted in yellow will both run when you left-click. This is expected. Each slash command is separate, and calls its handler function separately, so the condition provided with each is evaluated independently of any other slash commands the macro may be calling before or after the current one.

If you want everything to be evaluated at the same time, you need to change it to only one slash command, so that all of the arguments are passed to the handler function at the same time:
Code:

/mrc [btn:2] flying; [btn:3] repair; [mod:shift] funny; ground
This is just like writing a "real" macro. If you wrote this "real" macro:
Code:

/cancelaura [btn:1] Lightning Shield
/cast [btn:1, mod:shift] Lightning Shield

... and left-clicked it, both lines would run, so you would cancel Lightning Shield and then immediately recast it.

gmarco 11-15-12 08:30 AM

Hi,

thanks for your reply.

Even if I play wow for long time now I am really newbie in the macro and addon / lua field.
Your fixes of my wrong way of write the macro works like a charm.

Thanks again.


All times are GMT -6. The time now is 01:44 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI