Thread Tools Display Modes
04-19-15, 12:59 AM   #1
Etul
A Murloc Raider
Join Date: Apr 2015
Posts: 4
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

Last edited by Etul : 04-19-15 at 06:38 AM. Reason: Problem solved
  Reply With Quote
04-19-15, 01:57 AM   #2
odjur84
A Fallenroot Satyr
 
odjur84's Avatar
Join Date: Jan 2015
Posts: 24
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

Last edited by odjur84 : 04-19-15 at 02:01 AM.
  Reply With Quote
04-19-15, 02:53 AM   #3
sticklord
A Wyrmkin Dreamwalker
AddOn Author - Click to view addons
Join Date: Aug 2014
Posts: 57
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).
  Reply With Quote
04-19-15, 06:36 AM   #4
Etul
A Murloc Raider
Join Date: Apr 2015
Posts: 4
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)
  Reply With Quote
04-19-15, 07:35 AM   #5
odjur84
A Fallenroot Satyr
 
odjur84's Avatar
Join Date: Jan 2015
Posts: 24
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
  Reply With Quote
04-19-15, 09:36 AM   #6
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
Originally Posted by Etul View Post
Lua Code:
  1. -- Table for saved variables
  2. NpcData = {}
That is a horrible global name for a variable. Add the word local in front.
  Reply With Quote
04-19-15, 09:45 AM   #7
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
Originally Posted by myrroddin View Post
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
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
04-19-15, 10:14 AM   #8
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
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)
__________________
Grab your sword and fight the Horde!
  Reply With Quote
04-19-15, 02:45 PM   #9
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
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.
__________________
"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


Last edited by Seerah : 04-19-15 at 02:47 PM.
  Reply With Quote
04-19-15, 02:47 PM   #10
10leej
A Molten Giant
 
10leej's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2011
Posts: 583
Originally Posted by Seerah View Post
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?
__________________
Tweets YouTube Website
  Reply With Quote
04-19-15, 02:53 PM   #11
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
/shrug I dunno. I Googled the function name and the only thing I could find was for NWN.
__________________
"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

  Reply With Quote
04-20-15, 01:09 AM   #12
Etul
A Murloc Raider
Join Date: Apr 2015
Posts: 4
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.

Originally Posted by odjur84 View Post
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!

Originally Posted by myrroddin View Post
That is a horrible global name for a variable. Add the word local in front.
Originally Posted by Rilgamon View Post
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.
  Reply With Quote
04-20-15, 01:15 AM   #13
Etul
A Murloc Raider
Join Date: Apr 2015
Posts: 4
Originally Posted by Lombra View Post
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.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Seeking help with first addon

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