Reply
Thread Tools Display Modes
Unread 03-20-13, 01:24 PM   #21
Sharparam
A Warpwood Thunder Caller
 
Sharparam's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2011
Posts: 93
Originally Posted by Nibelheim View Post
Woops. Forgot a part. First file should be:
Lua Code:
  1. local addon, ns = ...
  2. ns[1] = {}
  3. ns[2] = {}
  4.  
  5. local Funs, Settings = unpack(select(2, ...))
  6.  
  7. Settings.defaults = {
  8.    x = 0,
  9.    y = 0,
  10. }
  11.  
  12. Funs.updateSettings = function()
  13.     -- do stuff
  14. end
Why use "select(2, ...)" as argument to unpack when you can just pass in ns instead (which should be better performance-wise)?
Sharparam is offline   Reply With Quote
Unread 03-20-13, 01:28 PM   #22
fRodzet
A Flamescale Wyrmkin
Join Date: Mar 2013
Posts: 114
Okay tried WoWPedia.org and i didn't get further than like the first basics before my "AddOn" didn't work properly.

Lua Code:
  1. function MyAddon_Mimic(who, send)
  2.    local posX, posY = GetPlayerMapPosition(who)
  3.    if send==1 then
  4.       CHAT_MSG_WHISPER:AddMessage(who .. " is currently at location " .. posX .. "," .. posY)
  5.    end
  6. end
  7.  
  8.  function MyAddon_Call()
  9.    MyAddon_Mimic("player", 1)
  10.    MyAddon_Mimic("party1", 1)
  11.    MyAddon_Mimic("party2", 0)
  12. end

Now, receiving a whisper, doesn't tell me his Location, why is that?
fRodzet is offline   Reply With Quote
Unread 03-20-13, 01:30 PM   #23
Sharparam
A Warpwood Thunder Caller
 
Sharparam's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2011
Posts: 93
Unless you are defining CHAT_MSG_WHISPER somewhere, I'm not sure if the WoW UI defines it.


You are probably looking for "DEFAULT_CHAT_FRAME:AddMessage".


Also, the code you pasted doesn't show you creating a frame and registering for the whisper event anywhere, if you haven't done so somewhere else, you need to do that too before you can receive and respond to events :)

Last edited by Sharparam : 03-20-13 at 01:32 PM.
Sharparam is offline   Reply With Quote
Unread 03-20-13, 02:51 PM   #24
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 9,624
CHAT_MSG_WHISPER is an event, not a frame/object. See what F16Gaming said. ^^
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

Seerah is online now   Reply With Quote
Unread 03-20-13, 03:13 PM   #25
Phanx
A Pyroguard Emberseer
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 4,391
Edit: I overlooked your correction, but the rest of my post is still relevant so I'm leaving it as-is.

Originally Posted by Nibelheim View Post
Top of first .Lua file
Lua Code:
  1. local addon, ns = ...
  2. local Funs, Settings = unpack(select(2, ...))
I'm not sure what that's supposed to do... if that's the first file to load, the second argument passed to your file is an empty table, so unpacking it won't give you any values. Maybe you meant something like this?

First file:
Code:
local ADDON_NAME, ns = ...
local Functions, Settings = {}, {}
ns[1], ns[2] = Functions, Settings
Other files:
Code:
local ADDON_NAME, ns = ...
local Functions, Settings = ns[1], ns[2]

Functions.DoSomething = function()
    -- do something
end
However, that's still a strange way to do it. There's no reason to use cryptic indices in your private table. Just use descriptive keys:

First file:
Code:
local ADDON_NAME, ns = ...
local Functions, Settings = {}, {}
ns.Functions, ns.Settings = Functions, Settings
Other files:
Code:
local ADDON_NAME, ns = ...
local Functions, Settings = ns.Functions, ns.Settings

Functions.DoSomething = function()
    -- do something
end
In the same vein, I'm not sure why you'd store functions in their own sub-table. Why not just store them directly in the top-level table?
__________________
Author/maintainer of Grid, PhanxChat, ShieldsUp, and many more.
Troubleshoot an addonTurn any code into an addonMore addon resources
Need help with your code? Post all of your actual code! Attach or paste your files.
Please don’t PM me about addon bugs or code questions. Post a comment or forum thread instead!

Last edited by Phanx : 03-20-13 at 03:30 PM.
Phanx is offline   Reply With Quote
Unread 03-20-13, 03:28 PM   #26
Phanx
A Pyroguard Emberseer
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 4,391
Originally Posted by fRodzet View Post
Okay tried WoWPedia.org and i didn't get further than like the first basics before my "AddOn" didn't work properly.
If that's your complete code, then the problem is that you don't have a frame listening for and responding to events, and nothing in your code actually calls your functions.

If that's not your complete code, where is the rest of it?

It's hard to tell what you're trying to do, but it looks like you're trying to print the map position of any group member who whispers you, in which case there are two ways to do that.

#1 - Register for the "CHAT_MSG_WHISPER" event to find out when you get a whisper:

Code:
local f = CreateFrame("Frame", "MyAddon")
f:RegisterEvent("CHAT_MSG_WHISPER")
f:SetScript("OnEvent", function(self, event, message, sender)
    local x, y = GetPlayerMapPosition(sender)
    if x and y then
        DEFAULT_CHAT_FRAME:AddMessage(sender .. " is currently at location " .. x .. ", " .. y)
    end
end)
#2 - Use the chat filtering system to find out when you get a whisper:

Code:
ChatFrame_AddMessageEventFilter("CHAT_MSG_WHISPER", function(self, event, message, sender)
    local x, y = GetPlayerMapPosition(sender)
    if x and y then
        DEFAULT_CHAT_FRAME:AddMessage(sender .. " is currently at location " .. x .. ", " .. y)
    end
end)
The second method is simpler, but far more limited, since you can only use it to detect chat messages, not other events like logging in (if you want to save and restore settings between login sessions), changing zones, etc.

Also, I'm not sure GetPlayerMapPosition accepts a name. It might need a real UnitID (like "player" or "raid13") in which case you would need to loop over all the players in your group to find the UnitID for the player who sent you the message. You can test easily whether you need to do this by typing this in-game:
/dump GetPlayerMapPosition("Yourname")
...where "Yourname" is replaced by your character's name. If you get an error message or an empty result, then you'll need to convert the sender's name to a UnitID.
__________________
Author/maintainer of Grid, PhanxChat, ShieldsUp, and many more.
Troubleshoot an addonTurn any code into an addonMore addon resources
Need help with your code? Post all of your actual code! Attach or paste your files.
Please don’t PM me about addon bugs or code questions. Post a comment or forum thread instead!
Phanx is offline   Reply With Quote
Unread 03-20-13, 03:42 PM   #27
fRodzet
A Flamescale Wyrmkin
Join Date: Mar 2013
Posts: 114
Originally Posted by Phanx View Post
If that's your complete code, then the problem is that you don't have a frame listening for and responding to events, and nothing in your code actually calls your functions.

If that's not your complete code, where is the rest of it?

It's hard to tell what you're trying to do, but it looks like you're trying to print the map position of any group member who whispers you, in which case there are two ways to do that.

#1 - Register for the "CHAT_MSG_WHISPER" event to find out when you get a whisper:

Code:
local f = CreateFrame("Frame", "MyAddon")
f:RegisterEvent("CHAT_MSG_WHISPER")
f:SetScript("OnEvent", function(self, event, message, sender)
    local x, y = GetPlayerMapPosition(sender)
    if x and y then
        DEFAULT_CHAT_FRAME:AddMessage(sender .. " is currently at location " .. x .. ", " .. y)
    end
end)
#2 - Use the chat filtering system to find out when you get a whisper:

Code:
ChatFrame_AddMessageEventFilter("CHAT_MSG_WHISPER", function(self, event, message, sender)
    local x, y = GetPlayerMapPosition(sender)
    if x and y then
        DEFAULT_CHAT_FRAME:AddMessage(sender .. " is currently at location " .. x .. ", " .. y)
    end
end)
The second method is simpler, but far more limited, since you can only use it to detect chat messages, not other events like logging in (if you want to save and restore settings between login sessions), changing zones, etc.

Also, I'm not sure GetPlayerMapPosition accepts a name. It might need a real UnitID (like "player" or "raid13") in which case you would need to loop over all the players in your group to find the UnitID for the player who sent you the message. You can test easily whether you need to do this by typing this in-game:
/dump GetPlayerMapPosition("Yourname")
...where "Yourname" is replaced by your character's name. If you get an error message or an empty result, then you'll need to convert the sender's name to a UnitID.
Thank you very much, worked perfect You can use a name to get the position - no need to use ID

Also now i tried to add Tooltips @mouseover from the ChatFrame when people linked an item, it works well but is my code considered readable?

Lua Code:
  1. local function ChatlinkTooltips_ShowTip(self, linkData)
  2.     local LinkType = string.split(":", linkData)
  3.     if LinkType == "item"
  4.     or LinkType == "spell"
  5.     or LinkType == "achievement"
  6.     or LinkType == "enchant"
  7.     or LinkType == "quest"
  8.     or LinkType == "talent"
  9.     or LinkType == "unit"
  10.     or LinkType == "glyph" then
  11.         GameTooltip:SetOwner(self, "ANCHOR_CURSOR")
  12.         GameTooltip:SetHyperlink(linkData)
  13.         GameTooltip:Show()
  14.     end
  15. end
  16.  
  17. local function ChatlinkTooltips_HideTip()
  18.     GameTooltip:Hide()
  19. end
  20.  
  21. local function ChatlinkTooltips_SetOrHookHandler(frame, event, func)
  22.     if frame:GetScript(event) then -- checks if it already has a script handler...
  23.         frame:HookScript(event, func) -- ... and hooks it.
  24.     else
  25.         frame:SetScript(event, func) -- sets our function as event handler otherwise
  26.     end
  27. end
  28.  
  29. for i = 1, NUM_CHAT_WINDOWS do
  30.     local frame = _G["ChatFrame" .. i] -- Copy a reference
  31.     if frame then -- Makes sure the frame exists
  32.         ChatlinkTooltips_SetOrHookHandler(frame, "OnHyperLinkEnter", ChatlinkTooltips_ShowTip)
  33.         ChatlinkTooltips_SetOrHookHandler(frame, "OnHyperLinkLeave", ChatlinkTooltips_HideTip)
  34.     end
  35. end

I understand it pretty much myself - but would there be an even better way to clarify this code?
fRodzet is offline   Reply With Quote
Unread 03-20-13, 04:04 PM   #28
Sharparam
A Warpwood Thunder Caller
 
Sharparam's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2011
Posts: 93
Not sure about the later parts, but the first part can be made more readable/easier to maintain:
lua Code:
  1. local supportedTypes = {
  2.     item = true,
  3.     spell = true,
  4.     achievement = true,
  5.     enchant = true,
  6.     quest = true,
  7.     talent = true,
  8.     unit = true,
  9.     glyph = true
  10. }
  11.  
  12. local function ChatlinkTooltips_ShowTip(self, linkData)
  13.     local LinkType = string.split(":", linkData)
  14.     if supportedTypes[LinkType] then
  15.         GameTooltip:SetOwner(self, "ANCHOR_CURSOR")
  16.         GameTooltip:SetHyperlink(linkData)
  17.         GameTooltip:Show()
  18.     end
  19. end
Sharparam is offline   Reply With Quote
Unread 03-20-13, 05:39 PM   #29
Phanx
A Pyroguard Emberseer
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 4,391
The link text includes the opening "|H" so you'll want to change

Code:
    local LinkType = string.split(":", linkData)
to:

Code:
    local LinkType = strmatch(linkData, "|H(.-):")
to catch only what's between the opening "|H" and the first colon.

Also, you don't need your whole SetOrHookScript function... just use HookScript directly. If the frame doesn't already have a script of that type set, HookScript works exactly like SetScript.
__________________
Author/maintainer of Grid, PhanxChat, ShieldsUp, and many more.
Troubleshoot an addonTurn any code into an addonMore addon resources
Need help with your code? Post all of your actual code! Attach or paste your files.
Please don’t PM me about addon bugs or code questions. Post a comment or forum thread instead!
Phanx is offline   Reply With Quote
Unread 03-21-13, 02:39 AM   #30
fRodzet
A Flamescale Wyrmkin
Join Date: Mar 2013
Posts: 114
Lua Code:
  1. local function ChatlinkTooltips_SetOrHookHandler(frame, event, func)
  2.     frame:HookScript(event, func) -- Sets & Hooks the script
  3. end

Worked like a charm, ty However:

Lua Code:
  1. local linkType = strmatch(linkData, "|H(.-):")

doesn't seem to work.. and i don't quiet get, why it "should" work either?

string.split(":", linkData) makes also way more sense in my head :P Here is my code so far:

Lua Code:
  1. local supportedType = {
  2. spell = true,
  3. item = true,
  4. quest = true,
  5. achievement = true,
  6. talent = true,
  7. glyph = true,
  8. unit = true,
  9. enchant = true
  10. }
  11.  
  12. local function ChatlinkTooltips_ShowTip(self, linkData)
  13.     local linkType = string.split(":", linkData)
  14.     if supportedType[linkType] then
  15.         GameTooltip:SetOwner(self, "ANCHOR_CURSOR")
  16.         GameTooltip:SetHyperlink(linkData)
  17.         GameTooltip:Show()
  18.     end
  19. end
  20.  
  21. local function ChatlinkTooltips_HideTip()
  22.     GameTooltip:Hide()
  23. end
  24.  
  25. local function ChatlinkTooltips_HookHandler(frame, event, func)
  26.     frame:HookScript(event, func) -- Sets & Hooks the script
  27. end
  28.  
  29.     for i = 1, NUM_CHAT_WINDOWS do
  30.     local frame = _G["ChatFrame" .. i] -- Copy a reference
  31.         ChatlinkTooltips_HookHandler(frame, "OnHyperLinkEnter", ChatlinkTooltips_ShowTip)
  32.         ChatlinkTooltips_HookHandler(frame, "OnHyperLinkLeave", ChatlinkTooltips_HideTip)
  33. end

Last edited by fRodzet : 03-21-13 at 02:56 AM.
fRodzet is offline   Reply With Quote
Unread 03-21-13, 03:31 AM   #31
Phanx
A Pyroguard Emberseer
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 4,391
Originally Posted by fRodzet View Post
Lua Code:
  1. local linkType = strmatch(linkData, "|H(.-):")

doesn't seem to work.. and i don't quiet get, why it "should" work either?

string.split(":", linkData) makes also way more sense in my head :P
If your string looks like this:
"|Hitem:12345:0:1:2:3|h[Random Item of the X]|h"

...then string.split on colons returns:
  1. "|Hitem"
  2. "12345"
  3. "0"
  4. "1"
  5. "2"
  6. "3|h[Random Item of the X]|h"

...whereas string.match to capture what's between the first "|H" and the first following ":" returns only "item":
|Hitem:12345:0:1:2:3|h[Random Item of the X]|h
__________________
Author/maintainer of Grid, PhanxChat, ShieldsUp, and many more.
Troubleshoot an addonTurn any code into an addonMore addon resources
Need help with your code? Post all of your actual code! Attach or paste your files.
Please don’t PM me about addon bugs or code questions. Post a comment or forum thread instead!
Phanx is offline   Reply With Quote
Unread 03-21-13, 04:52 AM   #32
fRodzet
A Flamescale Wyrmkin
Join Date: Mar 2013
Posts: 114
Originally Posted by Phanx View Post
If your string looks like this:
"|Hitem:12345:0:1:2:3|h[Random Item of the X]|h"

...then string.split on colons returns:
  1. "|Hitem"
  2. "12345"
  3. "0"
  4. "1"
  5. "2"
  6. "3|h[Random Item of the X]|h"

...whereas string.match to capture what's between the first "|H" and the first following ":" returns only "item":
|Hitem:12345:0:1:2:3|h[Random Item of the X]|h
okay i get the idea now.. However if i type: local linkType = strmatch(linkData, "|H(.-):") it doesn't show any tooltips when mouse hovering also tried string.match(x, x).. maybe some of my other code needs to be changed also then - but looked it all trough, dno what should be changed then.

Here is the code, but it won't work properly:

Lua Code:
  1. local supportedType = {
  2.       spell = true,
  3.       item = true,
  4.       quest = true,
  5.       achievement = true,
  6.       talent = true,
  7.       glyph = true,
  8.       unit = true,
  9.       enchant = true
  10.     }
  11.  
  12. local function ChatlinkTooltips_ShowTip(self, linkData)
  13.     local linkType = strmatch(linkData, "|H(.-):")
  14.     if supportedType[linkType] then
  15.         GameTooltip:SetOwner(self, "ANCHOR_CURSOR")
  16.         GameTooltip:SetHyperlink(linkData)
  17.         GameTooltip:Show()
  18.     end
  19. end
  20.  
  21. local function ChatlinkTooltips_HideTip()
  22.     GameTooltip:Hide()
  23. end
  24.  
  25. local function ChatlinkTooltips_HookHandler(frame, event, func)
  26.     frame:HookScript(event, func) -- Sets & Hooks the script
  27. end
  28.  
  29.     for i = 1, NUM_CHAT_WINDOWS do
  30.     local frame = _G["ChatFrame" .. i] -- Copy a reference
  31.         ChatlinkTooltips_HookHandler(frame, "OnHyperLinkEnter", ChatlinkTooltips_ShowTip)
  32.         ChatlinkTooltips_HookHandler(frame, "OnHyperLinkLeave", ChatlinkTooltips_HideTip)
  33. end
fRodzet is offline   Reply With Quote
Unread 03-21-13, 10:07 AM   #33
Sharparam
A Warpwood Thunder Caller
 
Sharparam's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2011
Posts: 93
Try using "|H(.+):" ('+' instead of '-'). It should work

Edit: They both give the same result apparently, I thought it was a typo at first :P

Edit2: So apparently a dash ('-') is equivalent to an asterisk ('*') in Lua patterns, not sure why you'd want to match 0 or more though. To me it makes more sense to match 1 or more (plus sign: '+'), since I doubt an item with no name exists in the game?

Last edited by Sharparam : 03-21-13 at 10:12 AM.
Sharparam is offline   Reply With Quote
Unread 03-21-13, 10:12 AM   #34
fRodzet
A Flamescale Wyrmkin
Join Date: Mar 2013
Posts: 114
Originally Posted by F16Gaming View Post
Try using "|H(.+):" ('+' instead of '-'). It should work

Edit: They both give the same result apparently, I thought it was a typo at first :P
Nope, won't work. Weird :P

Anyways, WoWPedia.org confuses the **** out of me :P Ain't there some step by step guide or something somewhere, where you learn to create a few AddOn examples?

Last edited by fRodzet : 03-21-13 at 10:14 AM.
fRodzet is offline   Reply With Quote
Unread 03-21-13, 10:14 AM   #35
Sharparam
A Warpwood Thunder Caller
 
Sharparam's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2011
Posts: 93
What if you add some print statements to check the value of the strings? Like this:

lua Code:
  1. local function ChatlinkTooltips_ShowTip(self, linkData)
  2.     print("linkData == " .. linkData)
  3.     local linkType = strmatch(linkData, "|H(.-):")
  4.     print("linkType == " .. linkType)
  5.     if supportedType[linkType] then

What does it show from those print calls?
Sharparam is offline   Reply With Quote
Unread 03-21-13, 10:33 AM   #36
fRodzet
A Flamescale Wyrmkin
Join Date: Mar 2013
Posts: 114
Originally Posted by F16Gaming View Post
What if you add some print statements to check the value of the strings? Like this:

lua Code:
  1. local function ChatlinkTooltips_ShowTip(self, linkData)
  2.     print("linkData == " .. linkData)
  3.     local linkType = strmatch(linkData, "|H(.-):")
  4.     print("linkType == " .. linkType)
  5.     if supportedType[linkType] then

What does it show from those print calls?
linkData == item:77272:0:0:0:0:0:0:0:11:0:0
linkType == item

<-- this is what it prints to the chatframe output.
fRodzet is offline   Reply With Quote
Unread 03-21-13, 10:35 AM   #37
Sharparam
A Warpwood Thunder Caller
 
Sharparam's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2011
Posts: 93
Seems the linkType does contain the correct value then, maybe the error lies somewhere else.

Also, there was no |H at the start of the link string? Then how could the string match work...
Sharparam is offline   Reply With Quote
Unread 03-21-13, 10:41 AM   #38
fRodzet
A Flamescale Wyrmkin
Join Date: Mar 2013
Posts: 114
Originally Posted by F16Gaming View Post
Seems the linkType does contain the correct value then, maybe the error lies somewhere else.

Also, there was no |H at the start of the link string? Then how could the string match work...
i have no idea, however:

local function ChatlinkTooltips_ShowTip(self, linkData)
local linkType = strmatch(linkData, "(.-):")
if supportedType[linkType] then
GameTooltip:SetOwner(self, "ANCHOR_CURSOR")
GameTooltip:SetHyperlink(linkData)
GameTooltip:Show()
end
end

worked just well - without the |H
fRodzet is offline   Reply With Quote
Unread 03-21-13, 10:43 AM   #39
Sharparam
A Warpwood Thunder Caller
 
Sharparam's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2011
Posts: 93
That is extremely odd, using the data you provided in the previous post:

Code:
> print(("item:77272:0:0:0:0:0:0:0:11:0:0"):match("|H(.+):"))
nil
> print(("item:77272:0:0:0:0:0:0:0:11:0:0"):match("|H(.-):"))
nil
Sharparam is offline   Reply With Quote
Unread 03-21-13, 10:49 AM   #40
fRodzet
A Flamescale Wyrmkin
Join Date: Mar 2013
Posts: 114
Originally Posted by F16Gaming View Post
That is extremely odd, using the data you provided in the previous post:

Code:
> print(("item:77272:0:0:0:0:0:0:0:11:0:0"):match("|H(.+):"))
nil
> print(("item:77272:0:0:0:0:0:0:0:11:0:0"):match("|H(.-):"))
nil
Yeah, you tell me - i'm the newb here
fRodzet is offline   Reply With Quote
Reply

Go BackWoWInterface » Developer Discussions » Lua/XML Help » H: Why XML and why LUA?

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off