WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Seeking help with first addon (https://www.wowinterface.com/forums/showthread.php?t=52208)

Etul 04-19-15 12:59 AM

Seeking help with first addon
 
Edit: Problem solved (sort of), might keep this thread handy in case any other issues pop up later. Thanks guys!

Hi! This is my first time trying to make an addon with Lua- the idea I am working towards is something that will keep track of the NPCs that are available in all of your alts' garrisons.

To start with I tried to check the character's level when they log in and only check other information such as their name/class/realm if they are level 100. Mostly just as an experiment I wanted to try and print this to the user's chat frame after collecting this information, however despite fiddling with my code for a few hours I can't seem to manage to get it to work.

My code is below- any advice would be greatly appreciated. Thanks!

---

GarrisonNPC.toc

Code:

## Interface: 60100
## Title: Garrison NPC
## Author: Etuliela
## Version: v1.0.0
## Notes: Tracks which NPCs are available in each of your characters' garrisons.
## SavedVariables: NpcData

garrisonnpc.lua

garrisonnpc.lua

Lua Code:
  1. -- Table for saved variables
  2. NpcData = {}
  3.  
  4. local EventFrame = CreateFrame("Frame")
  5.  
  6. -- Check player's level on login - if player is 100 it will record additional character information, otherwise it will not
  7. EventFrame:RegisterEvent("PLAYER_LOGIN")
  8. EventFrame.OnEvent = function(self,event,playerName,characterLevel)
  9.     playerName = UnitName("Player")
  10.     characterLevel = GetCharacterLevel(playerName)
  11.     if characterLevel == 100 then
  12.         NpcData.characterName = UnitName("Player")
  13.         NpcData.characterRealm = GetRealmName()
  14.         NpcData.characterAccount = THIS_ACCOUNT
  15.         ChatFrame1:AddMessage(NpcData.characterName, NpcData.characterRealm, NpcData.characterAccount)
  16.     end
  17.  
  18. end

odjur84 04-19-15 01:57 AM

Hi Etul.

Further information about the issue you encounter (e.g. which error message you get or, more general, what makes you think that the addon doesn't work) would be nice.

Since I'm just a beginner with lua and addon-programming, it's highly likely that I've overlooked something, but after reading your code several times (and under the assumption that this is indeed all of your code) my only guess is the following:

Code:

ChatFrame1:AddMessage(NpcData.characterName, NpcData.characterRealm, NpcData.characterAccount)
Since I do not have any experience with the API AddMessage I can only consult the API docs (see here or here) and they seem to indicate that you may have used the wrong syntax by simply string together the messages that shall be shown in the ChatFrame. Maybe you want to try something like:

Code:

ChatFrame1:AddMessage(NpcData.characterName)
ChatFrame1:AddMessage(NpcData.characterRealm)
ChatFrame1:AddMessage(NpcData.characterAccount)

or

Code:

ChatFrame1:AddMessage(NpcData.characterName .. " " .. NpcData.characterRealm .. " " .. NpcData.charakterAccount)
If this doesn't do the trick, please let us know what went wrong with your testing.

Cheers,

Odjur

sticklord 04-19-15 02:53 AM

Your problem is that you have registered an event and all, but there is nothing happening when it triggers. What you need is EventFrame:SetScript("OnEvent", your function).

Etul 04-19-15 06:36 AM

Thanks for the help guys, it's really appreciated. :)

I added the SetScript part and finally managed to get it to print to chat with the following:

Lua Code:
  1. DEFAULT_CHAT_FRAME:AddMessage(NpcData.characterName)
  2.         DEFAULT_CHAT_FRAME:AddMessage(NpcData.characterRealm)

However it did print out on separate lines like this:

[22:32:49] Sillycoder
[22:32:49] Barthilas

Next step is to work out how to get it to print to the same line, but I'm just happy that I finally managed to get my addon to actually DO something tangible. >.< Seems like this will be a very steep learning curve, but it will be a very interesting journey! (and a long one, too)

odjur84 04-19-15 07:35 AM

Hi!

I already gave you a hint how to print in one line. :-)

Code:

DEFAULT_CHAT_FRAME:AddMessage("Charactername: " .. NpcData.characterName .. " " .. "Characterrealm: " .. NpcData.characterRealm)
This concatenates the two strings, separated by a blank and some additional information.

Cheers,

Odjur

myrroddin 04-19-15 09:36 AM

Quote:

Originally Posted by Etul (Post 308267)
Lua Code:
  1. -- Table for saved variables
  2. NpcData = {}

That is a horrible global name for a variable. Add the word local in front.

Rilgamon 04-19-15 09:45 AM

Quote:

Originally Posted by myrroddin (Post 308280)
That is a horrible global name for a variable. Add the word local in front.

Not only that ... its misleading, too. NPC means Non-Player-Character ... storing Player-Data in this var is confusing ;)

Lombra 04-19-15 10:14 AM

print is handy to use instead of DEFAULT_CHAT_FRAME:AddMessage. Not only because it's quicker (to type), but it also takes and prints multiple arguments.
Code:

print(NpcData.characterName, NpcData.characterRealm)

Seerah 04-19-15 02:45 PM

I don't understand... How is your code working? And how did no one point this out?

There is no such function in WoW called GetCharacterLevel. It's just UnitLevel.

/edit: and "Player" =/= "player"

/edit2: both of these errors would have been pointed out to you if you have Lua errors enabled in your Interface Options.

10leej 04-19-15 02:47 PM

Quote:

Originally Posted by Seerah (Post 308286)
I don't understand... How is your code working? And how did no one point this out?

There is no such function in WoW called GetCharacterLevel. It's just UnitLevel.

/edit: and "Player" =/= "player"

maybe there's a library adding this function?

Seerah 04-19-15 02:53 PM

/shrug I dunno. I Googled the function name and the only thing I could find was for NWN.

Etul 04-20-15 01:09 AM

Oh wow... I wasn't expecting this thread to explode overnight, though I do appreciate all of you adding your input. I almost don't know where to start.

Quote:

Originally Posted by odjur84 (Post 308277)
Hi!

I already gave you a hint how to print in one line. :-)

Code:

DEFAULT_CHAT_FRAME:AddMessage("Charactername: " .. NpcData.characterName .. " " .. "Characterrealm: " .. NpcData.characterRealm)
This concatenates the two strings, separated by a blank and some additional information.

Cheers,

Odjur

Whoops, my bad... so much information is flying everywhere I totally missed it. >.< Thanks!

Quote:

Originally Posted by myrroddin (Post 308280)
That is a horrible global name for a variable. Add the word local in front.

Quote:

Originally Posted by Rilgamon (Post 308284)
Not only that ... its misleading, too. NPC means Non-Player-Character ... storing Player-Data in this var is confusing ;)

My intention was for the "NPCData" variable to store data from multiple toons as I specified it in the "SavedVariable" section of my .toc file. If I declare it as a local variable will it still work across multiple toons?

I realised that I probably need to reverse the process and scan for the appropriate NPCs before recording any data so I will probably end up making arrays within something like NPCData[harrison] to store the player info. I don't know if it will make things less confusing then. ;)

Etul 04-20-15 01:15 AM

Quote:

Originally Posted by Lombra (Post 308285)
print is handy to use instead of DEFAULT_CHAT_FRAME:AddMessage. Not only because it's quicker (to type), but it also takes and prints multiple arguments.
Code:

print(NpcData.characterName, NpcData.characterRealm)

I shall try that out as well. Thanks! :)

@Seerah/10leej - I honestly can't remember where exactly I found the GetCharacterLevel() thing- iirc I couldn't find anything on wowiki/wowprogramming so I tried looking to see what people did in actual addons to find out a character's level and ended up with that. I think my mistake was using Ctrl+F to search for "character" which is why I would have missed it on wowprogramming. But I did manage to find the UnitLevel("player") thing somewhere else so I finally got my code working using that. :)

Also, I tried running /dump UnitLevel("player") and /dump UnitName("player") with both capitalized and non-capiltalized versions of "player" and it returned the same result.


All times are GMT -6. The time now is 12:06 PM.

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