Thread Tools Display Modes
07-25-08, 10:36 AM   #1
jackfrosty
A Kobold Labourer
AddOn Author - Click to view addons
Join Date: Jun 2008
Posts: 1
need help with UnitName("pet") (bad first time author)

i am trying to make a probably simple addon but i suck at this i thought i almost had it done but i am getting a bug at line 17:attempt to concatenate local 'petName' (a nil value)
Code:
local HEMOTE_OUTPUT_INFO = "Type /HunterEmoteOff to turn the Emotes off or /HunterEmoteOn to turn them on";
local HEMOTE_OUTPUT_OFF = "Hunter Emote is now Off";
local HEMOTE_OUTPUT_ON = "Hunter Emote is now On";
local petName = UnitName("pet");

HEon = "1"

HEmoteDelay = {
'1';
'2';
'3';
'4';
'5';
};

HEmoteMsg = {
'Charge! ' .. petName ;
'Rip it to shreds! ' ..petName;
'Ravage it! ' ..petName;
petName..' Help me!';
'Go for the Throat! ' ..petName;
petName..' Attack!';
'Kill em! ' ..petName;
'Get em! ' ..petName;
'Sick em! ' ..petName;
'Go for the Ankles! ' ..petName;
'Get off your lazy butt ' ..petName;
};

function HEmote_OnLoad (self)
 this:RegisterEvent("PET_ATTACK_START");
  
 SlashCmdList["HEMOTE_INFO"] = HEmoteinfo;
 SLASH_HEMOTE_INFO1 = "/hunteremote";
 SLASH_HEMOTE_INFO2 = "/hemote";
 
 SlashCmdList["HEMOTE_OFF"] = HEmoteoff;
 SLASH_HEMOTE_OFF1 = "/hunteremoteoff";
 SLASH_HEMOTE_OFF2 = "/hemoteoff";

 SlashCmdList["HEMOTE_ON"] = HEmoteon;
 SLASH_HEMOTE_ON1 = "/hunteremoteon";
 SLASH_HEMOTE_ON2= "/hemoteon";
end

function HEmoteOutput(msg)
 if(DEFAULT_CHAT_FRAME) then
  DEFAULT_CHAT_FRAME:AddMessage(msg);
 end
end

function HEmoteinfo(msg)
 local echo = HEMOTE_OUTPUT_INFO .. msg;
 HEmoteOutput(echo);
end

function HEmoteoff(msg)
 HEon = "0"
 local echo = HEMOTE_OUTPUT_OFF .. msg;
 HEmoteOutput(echo);
end

function HEmoteon(msg)
 HEon = "1"
 local echo = HEMOTE_OUTPUT_ON .. msg;
 HEmoteOutput(echo);
end

function HEmote_OnEvent(self, event, ...)
 if HEon == "1" then
  local Delaynum = table.getn(HEmoteDelay);
  local delaykey = math.random(Delaynum);
   if (delaykey > 3) then
    local RanEmotes = table.getn(HEmoteMsg);
    local randomKey = math.random(RanEmotes);
     SendChatMessage(HEmoteMsg[randomKey], "SAY");
   end
  end
 end
if i reload my ui it will work the way i want it too i looked up the problem and i guess it is happening because it takes a sec for the pet to load into the world and meanwhile having a nil value in my HEmotemsg table is messing it up so i was wondering if there was a way i could put the pets name into the table still and have it work or maybe some other way or something this is my first coding experience so most of the stuff i have up there was from just reading and is probably pretty messy lol

Last edited by jackfrosty : 08-09-12 at 11:02 PM.
  Reply With Quote
07-25-08, 10:53 AM   #2
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
This prevents petName from being nil:
Code:
local petName = UnitName("pet") or ""
You could save petName. Declare petName as global
Code:
petName = UnitName("pet") or ""
and add
Code:
## SavedVariablesPerCharacter: petName
to your toc.
  Reply With Quote
07-25-08, 11:16 AM   #3
Akryn
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 479
Originally Posted by Duugu View Post
This prevents petName from being nil:
Code:
local petName = UnitName("pet") or ""
You could save petName. Declare petName as global
Code:
petName = UnitName("pet") or ""
and add
Code:
## SavedVariablesPerCharacter: petName
to your toc.
this is good advice, except that "petName" is not a good global variable name, since it's very generic and could end up being used by another addon. if nothing else, i would suggest using something like myAddonsNameHere_petName

or if you have more than one that you set up a namespace:
Code:
myAddon_savedVariables = {
     petName = "",
     somethingElse = value,
     etc
}
  Reply With Quote
07-25-08, 12:07 PM   #4
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
You tried getting the petname after the event "ADDON_LOADED"?
  Reply With Quote
07-29-08, 04:53 AM   #5
Mahiro
A Murloc Raider
 
Mahiro's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2006
Posts: 9
you could try
Code:
local petName; if UnitExists("pet") then petName = UnitName("pet"); end

Last edited by Mahiro : 07-29-08 at 04:55 AM.
  Reply With Quote
07-29-08, 09:08 AM   #6
Taffu
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Jan 2006
Posts: 149
Originally Posted by Mahiro View Post
you could try
Code:
local petName; if UnitExists("pet") then petName = UnitName("pet"); end
If it's called when you first login and your pet isn't called (dismiss/call while in-game) the local will have to be reup'd via an event or update call of some sort doing it this way.

Because pets go in and out as far as existence is concerned...you may want to try attaching the variable update to an event. Try UNIT_PET or PET_BAR_UPDATE (or both) for variable updates. Something like:

Code:
local petName

function MyAddOn:UNIT_PET()
  if ( UnitExists("pet") ) then
    petName = UnitName("pet")
  else
    petName = ""
  end  
end
There's a few different ways you could do it. This is just one. This requires seperate OnEvent handlers for each event, so might have to make some adjustments there.
  Reply With Quote
08-01-08, 08:44 PM   #7
LBXZero
A Theradrim Guardian
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 61
Taffu is correct. With the original code in the first post, if the player logs in with out a pet or the mod loads too early, petName will be nil. It is because it only sets petName when the script is read the first time.

The best solution is to register for event UNIT_PET because it fires when something happens to your pet. It should always occur during loading if the pet is out when logging in.

Code:
function HEmote_OnLoad (self)
 this:RegisterEvent("PET_ATTACK_START");
 self:RegisterEvent("UNIT_PET");
...

function HEmote_OnEvent(self, event, ...)
if (event == "UNIT_PET") then
  petName = UnitName("pet");
 elseif (petName and HEon == "1") then
 ...
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » need help with UnitName("pet") (bad first time author)


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