WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   How to show XML frame with slash command (https://www.wowinterface.com/forums/showthread.php?t=58699)

nemvokrobot 04-16-21 07:13 AM

How to show XML frame with slash command
 
Hi :)

I have an XML frame:

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="TestFrame.lua"/>
    <Frame name="TestFrame" parent="UIParent" frameStrata="BACKGROUND" hidden="true" setAllPoints="true">
                <Layers>
                </Layers>
        <Scripts>
            <OnLoad>
                self:RegisterEvent("PLAYER_ENTERING_WORLD");
            </OnLoad>
            <OnEvent function="Testframe_OnEvent"/>
            <OnUpdate function="Testframe_OnUpdate"/>
        </Scripts>
    </Frame>
</Ui>

and I want to show this frame by typing "/showtest" to the chat. I've tried in LUA this way but didn't work:

Code:

local myframe = TestFrame;
SLASH_SHOWTEST1 = "/showtest"
SlashCmdList["SHOWTEST"] = function(message, editbox)
        myframe:Show();
end

any ideas who done something similar? or is it not possible to do it with an XML frame?

Kanegasi 04-16-21 07:57 AM

You haven't provided anything in your code about how the frame should look. No templates or textures. It exists, but it's effectively invisible and it spans the entire length and width of the screen. You can use the following command to prove it exists:

/dump TestFrame

myrroddin 04-16-21 07:59 AM

I can't speak directly to your question, but I can say that making a globally named frame "TestFrame" is a horrid idea. How do you know there aren't 1000 frames with that same name? Even your slash command has a very bad global name.

nemvokrobot 04-16-21 09:14 AM

Quote:

Originally Posted by Kanegasi (Post 338912)
You haven't provided anything in your code about how the frame should look. No templates or textures. It exists, but it's effectively invisible and it spans the entire length and width of the screen. You can use the following command to prove it exists:

/dump TestFrame

I do make a texture for it at event, but I haven't provided its code because it is not important since if I show the frame without slash command it will work, so shouldn't be related at all to visual issues.

nemvokrobot 04-16-21 09:17 AM

Quote:

Originally Posted by myrroddin (Post 338913)
I can't speak directly to your question, but I can say that making a globally named frame "TestFrame" is a horrid idea. How do you know there aren't 1000 frames with that same name? Even your slash command has a very bad global name.

This is just for testing purposes, so I don't really care about proper namings sorry.

nemvokrobot 04-16-21 09:32 AM

Anyway I did a workaround by making a local variable called ToShow and setting it true at the slash function and then at the frame's Update use Self:Show() if that variable is true. Idk why it didn't work directly lol.

Fizzlemizz 04-16-21 10:17 AM

The the name you give a frame is entered into the global table as a reference to that frame. If I remember correctly, the first frame with a given name gets the global table variable.

If you have more than one frame called TestFrame then running TestFrame:SetShown(not TesFrame:IsShown()) will toggle the first TestFrame created which may not be yours or more likely is one of the other 50 TestFrames you've got sitting in long forgotten test code.

Giving unique names to anything created as a global is good advice as you're sharing that space with all other addons including the entire Blizzard default UI.

SDPhantom 04-16-21 08:13 PM

Do you have script errors turned on? Errors earlier in your code can block the rest of it from running.
Run /console scriptErrors 1 to enable. (It's also a good idea to /reload afterward to see any errors you missed in the loading process too)



Keep your ToC load order in mind. Files are loaded one at a time in the order listed in your ToC file. If your XML loads first, it can't access the functions defined for your script handlers in your Lua file. If you load Lua first, the frame defined in the XML won't be loaded yet either.

Let's assume you have your Lua file loaded first as this is easier to work with. Here's the code you posted.
Quote:

Originally Posted by nemvokrobot (Post 338911)
Lua Code:
  1. local myframe = TestFrame;
  2. SLASH_SHOWTEST1 = "/showtest"
  3. SlashCmdList["SHOWTEST"] = function(message, editbox)
  4.     myframe:Show();
  5. end

As I mentioned earlier, when the Lua file is loaded first, TestFrame doesn't exist yet, so myframe gets assigned nil. When you run your slash command, it should throw an "attempt to index nil" error if you had scriptErrors enabled. The best way to fix this is to get rid of the myframe local.
Lua Code:
  1. SLASH_SHOWTEST1 = "/showtest"
  2. SlashCmdList["SHOWTEST"] = function(message, editbox)
  3.     TestFrame:Show();
  4. end
This causes the global TestFrame to resolve when the slash command is run and isn't affected by your ToC load order.

myrroddin 04-16-21 08:14 PM

Quote:

Originally Posted by nemvokrobot (Post 338915)
This is just for testing purposes, so I don't really care about proper namings sorry.

That is not an excuse. You should always care about proper names for global variables. To be blunt, if you do not wish to learn, then don't ask for help. And expect your code to fail when you do not do it correctly.


All times are GMT -6. The time now is 11:07 AM.

vBulletin © 2021, Jelsoft Enterprises Ltd
© 2004 - 2020 MMOUI