Saturday, April 21, 2012

Slash command is a myth?

Hello All!

Insert standard n00b disclaimer here.

After reading Klishu's sticky tutorial on XML, I was pumped, ready to forge into new ground and make that simple add on not so simple. That's where it all fell apart.

I cannot get the system to recognize a slash command. From all the tutorials and all the examples, one would think this is the easiest next step to do. Yet, I failed. Repeatedly.

If anyone has time to write a little "fix this code here" response, I'd be very grateful.

HelloWorld.toc


Code:
## Interface:          20300
## Title: HelloWorld
## Notes: Not so simple anymore!
## Dependencies:
HelloWorld.xml

HelloWorld.xml (almost exactly from the Simple.xml)


Code:
<Ui xmlns="http://www.blizzard.com/wow/ui/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.blizzard.com/wow/ui/ ..\FrameXML\UI.xsd">

<Script file="HellowWorld.lua" />

<Frame name="HelloWorldFrame" visible="true" parent="UIParent">
<!-- set the size of the frame -->
<Size>
<AbsDimension x="200" y="133"/>
</Size>

<!-- set where the frame will be anchored -->
<Anchors>
<Anchor point="CENTER" />
</Anchors>

<!-- define background and borders -->
<Backdrop bgFile="Interface\TutorialFrame\TutorialFrameBackground"
edgeFile="Interface\DialogFrame\UI-DialogBox-Border" tile="true">
<EdgeSize>
<AbsValue val="16"/>
</EdgeSize>
<TileSize>
<AbsValue val="32"/>
</TileSize>
<BackgroundInsets>
<AbsInset left="5" right="5" top="5" bottom="5"/>
</BackgroundInsets>
</Backdrop>

<!-- set the parameters for the text -->
<Layers>
<Layer level="ARTWORK">
<FontString name="HelloWorld_Text" inherits="ChatFontNormal" text="Type in: Hello World">
<Anchors>
<Anchor point="TOP">
<Offset>
<AbsDimension x="0" y="-33" />
</Offset>
</Anchor>
</Anchors>
</FontString>
</Layer>
</Layers>

<!-- buttons and edit box -->
<Frames>
<Button name="HelloWorld_CloseButton" inherits="UIPanelCloseButton">
<Anchors>
<Anchor point="TOPRIGHT">
<Offset>
<AbsDimension x="-3" y="-3" />
</Offset>
</Anchor>
</Anchors>

<Scripts>
<OnClick>HelloWorld_Close();</OnClick>
</Scripts>
</Button>

<EditBox name="HelloWorld_EditBox" letters="50">
<Layers>
<Layer level="BACKGROUND">
<Texture name="HelloWorld_Background">
<Color r=".2" g=".2" b=".2" a="1"/>
</Texture>
</Layer>
</Layers>

<Size>
<AbsDimension x="190" y="20"/>
</Size>

<Anchors>
<Anchor point="TOPLEFT">
<Offset>
<AbsDimension x="5" y="-66"/>
</Offset>
</Anchor>
</Anchors>

<Scripts>
<OnEnterPressed>HelloWorld_AcceptButton:Click();</OnEnterPressed>
</Scripts>

<FontString inherits="ChatFontNormal"/>
</EditBox>

<Button name="HelloWorld_AcceptButton" inherits="OptionsButtonTemplate" text="GO!">
<Anchors>
<Anchor point="BOTTOM">
<Offset>
<AbsDimension x="0" y="20"/>
</Offset>
</Anchor>
</Anchors>

<Scripts>
<OnClick>HelloWorld_ButtonClick();</OnClick>
</Scripts>
</Button>
</Frames>
</Frame>
</Ui>

HellowWorld.lua


Code:
function HelloWorld_OnLoad()
if( DEFAULT_CHAT_FRAME ) then
DEFAULT_CHAT_FRAME:AddMessage("HelloWorld Loaded");
end
local stopVar = 0;
SLASH_HelloWorld1 = "/helloworld";
SLASH_HelloWorld2 = "/hw"; -- A shortcut or alias
SlashCmdList["HelloWorld"] = function(cmd)
HelloWorld_Command(cmd);
end
end

function HelloWorld_Command(cmd)
if (cmd == "1") then
stopVar = 1;
DEFAULT_CHAT_FRAME:AddMessage("HelloWorld Message On");
HelloWorldFrame:Show();
end
if (cmd == "0") then
stopVar = 0;
DEFAULT_CHAT_FRAME:AddMessage("HelloWorld Message Off");
HelloWorldFrame:Hide();
end
end

function HelloWorld_ButtonClick()
DEFAULT_CHAT_FRAME:AddMessage(HelloWorld_EditBox:GetText());
HelloWorld_EditBox:SetText("");
end

function HelloWorld_Close()
HelloWorldFrame:Hide();
end



Once I added the _Onload and _Command functions, I started getting attempted to call global value nil errors. I see that the XML never calls the _OnLoad (If I stick

<Scripts>

<OnLoad>HelloWorld_OnLoad();</OnLoad>

</Scripts>

into any of the frames, I do get the loaded message, but still, no slash commands.)

Any suggestions would be greatly appreciated!!|||Could just be the way I do it but I don't understand this part:


Code:
SlashCmdList["HelloWorld"] = function(cmd)
HelloWorld_Command(cmd);
end

Shouldn't that just be:


Code:
SlashCmdList["HelloWorld"] = HelloWorld_Command(cmd);
|||That would just execute HelloWorld_Command and assign its return value nil to that element of SlashCmdList. That line would work without the (cmd) on the end.

I do things a little differently. I create an Initialize() function and then just have two lines like this in it:

SLASH_PAWN1 = "/pawn"

SlashCmdList["PAWN"] = PawnCommand

Then, I have a Command(cmd) function defined later on:

function PawnCommand(Command)

Then, at the end of my lua file I set up a tiny invisible frame and wait for the VARIABLES_LOADED event, and then I call PawnInitialize from there, instead of OnLoad for any particular frame or anything like that. This has worked really well for me, and makes it so that the code is the same whether I want a UI or not. Check out one of my mods (Divisor is the simplest) for the code... you'll be interested in [ModName]OnEvent and the little block at the end of the mod's main .lua file. You could either do it like I do with a hidden frame, or since you have an XML file in your mod, you could just add an OnEvent script to your main window and leave out the whole hidden frame process.|||Thanks all! I got it finally... after I noticed the typo in "HellowWorld" I appreciate the help!

No comments:

Post a Comment