LUA CreateFrame, then RemoveFrame??
Hello all!
I'm about 3 hours in to my first attempt at creating (err, finding one and building on to it) an addon. I found an addon that I thought was a good start, but lacked a lot of what I desired, so why not learn LUA and write my own eh? What I've had a tough time doing is creating a toggle effect when using slash commands + Msg. As you can see in the code, if Msg == "on" then I CreateFrame. If Msg == "off", essentially I would like to just undo the CreateFrame that I just created. I can't figure out how to do this without just applying a ReloadUI (as you see commented out), which, while effective, rather annoying. Any help would be much appreciated! Lua Code:
|
There is no way to remove a frame from memory once created. What you can do is create the frame when you load the addon and hide it immediately. When you use the slash command, you can toggle the visibility of the frame in order to obtain the effect you want.
Globals are also not saved between sessions. You can however register a specific global to be saved for your addon if defined in its TOC file. For example, the following tag defines RYINNCURSOR_Enabled to be saved between sessions. You can retrieve the data in these variables after ADDON_LOADED fires for your addon. Code:
## SavedVariables: RYINNCURSOR_Enabled |
There is no RemoveFrame function, nor any other way to destroy a frame once it's been created. Instead, you should create the frame(s) only once, and then just :Hide() them when not in use. You can :Show() them again later. If you need to change what's displayed on the frame, just change things as necessary, using methods like :SetText() on font strings, :SetTexture() and :SetVertexColor() on textures, :SetSize() on frames, etc.
Also, "Enabled" is an absolutely unacceptable name for a global variable. There is an extremely high chance of some other addon (or even the default UI) leaking a variable with that name into the global namespace, overwriting your variable or vice versa. Global variables and object names should always follow two guidelines: 1. The name is unique, and unlikely to collide with the name used by any other addon or the default UI. A simple way to ensure uniqueness is to use your addon name as a prefix, eg. "MyAddon_Enabled". 2. The name clearly identifies the addon that owns it. This way, if it shows up in an error message, the user knows who to tell about it. Using your addon name as a prefix also solves this problem. |
An easy way to do what they sayed would be to add a name to you frame and to reference it later on. Like
Lua Code:
|
I mentioned that I'm a complete beginner here - and was wondering if someone can help me with the code, perhaps help me re-write it properly for what I'm trying to achieve. Would someone be willing to help me with that?
While I completely understand what you're saying about first Creating the frame, and hiding it immediately, then show or hide works as the toggle, I'm having a difficulty putting it all together. I really appreciate any help! |
I've cleaned up the code a little and added a few comments (all prefixed with my name) explaining the changes and questioning why certain things are the way they are.
Lua Code:
It's not perfect and I can't guarantee it will work, but it probably will. If you're going to be saving whether or not the AddOn is enabled, you should use the ADDON_LOADED event to set the default enabled state and then show/hide your frame based on the enabled state. |
Choonster, thank you! This is great!
And I also very much appreciate the comments, which help me learn. These aren't going to be used elsewhere so how you have it is fine! Quote:
|
Use this instead of the existing Ryinn_Command function:
Lua Code:
After looking at the code more closely, I realised that the x, y and speed variables do need to be declared outside of the OnUpdate function as the values from the previous call are used to calculate the new value of speed each time. |
Quote:
[e] As I read Choonsters reply I'm actually unsure if the value will be Nil or an empty string. :) *shrug* Lua Code:
|
Quote:
https://github.com/tekkub/wow-ui-sou...rame.lua#L4315 Notice that the msg variable passed as the first argument is always assigned to a string value (either a literal or the return value of strsub/strtrim). |
I tested this in game and so far so good!
I did make one tweak though for functionality. When initially logging in, it would hide the frame, as expected. On first MSG command, it would skip to disable On second MSG command, it would enable... so I changed the var to false, and now it works perfectly! Thanks for everyone's help. Does anyone recommend a good place to learn LUA, like tutorials? Lua Code:
|
Quote:
Keep in mind that WoW uses Lua 5.1, the most recent release version of Lua is 5.2. The two are mostly similar, but there are some major differences like the concept of function environments (getfenv/setfenv in 5.1 vs _ENV in 5.2). Function environments are a fairly advanced feature that you're not likely to use very often, though. The official reference manual is quite useful, but it's not a tutorial. As for WoW-specific stuff, there's a few tutorials here. There's also this book, but I haven't bought it myself so I can't say how useful it is. There's also a bunch of handy links in this sticky. When I was learning Lua, I mostly just learned the various features of the language as I needed them by a combination of trial and error, reading existing code and reading the documentation (the official reference manual or the WoW API Reference as appropriate). |
Thanks Choon! It'll definitely take some getting used to. First time I've ever really looked at LUA, seems pretty basic for the most part coming off of C++ knowledge, but still rusty.
I'll take a look at the links and play around. Again, I really appreciate your help! Cheers~ |
One last thing: The language is actually called Lua, not LUA. It's the Portuguese word for "moon", not an acronym.
|
All times are GMT -6. The time now is 03:36 AM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI