Thread Tools Display Modes
12-25-14, 02:26 PM   #1
MrBerlin
A Murloc Raider
Join Date: Dec 2014
Posts: 4
self is nil

Hiho, here my code for the class:
Lua Code:
  1. BattleRecord = {};
  2. BattleRecord.__index = BattleRecord;
  3.  
  4. setmetatable(BattleRecord, {
  5.   __call = function (cls, ...)
  6.     return cls.new(...)
  7.   end,
  8. });
  9.  
  10. function BattleRecord.new(tamerId, petSpeciesIds, attackIds, triesTable)
  11.   local self = setmetatable({}, BattleRecord);
  12.   self.tamerId = tamerId;
  13.   self.petSpeciesIds= {{1,2,3},{4,5,6}};
  14.   --self.petSpeciesIds = petSpeciesIds;
  15.   self.rounds={};
  16.   self.attackIds = {};
  17.   self.tries = {0,0};
  18.   if (triesTable ~= nil) then
  19.     --self.tries = triesTable;
  20.   end
  21.   print("battleRecord init SElf:"..(tostring(self) or "nil"));
  22.   return self;
  23. end
  24. function BattleRecord:GetTriesTable()
  25.     return self.tries;
  26. end
  27. function BattleRecord:GetSuccessRate()
  28.    
  29.     print("self: "..tostring(self));
  30.     if (#self.tries == 0) then
  31.         return 0;
  32.     end
  33.     if (self.tries[2]>0) then
  34.         return Math.ceil(self.tries[1]/self.tries[2]);
  35.     end
  36.     return 0;
  37. end
  38. function BattleRecord:PrintRound(round)
  39.     local str = "Round: "..round..": ";
  40.     local trainer = self.trainers[1];
  41.     local roundTable = self.rounds[round];
  42.     if (#roundTable>0) then
  43.         local pet = trainer:GetPet(self.orders[round][1]);
  44.         local orderType = self.orders[round][2];
  45.         local orderValue = self.orders[round][3];
  46.         local orderTypes = {"", " uses ", "got switched to ", " used a trap ", " skipped the turn "};
  47.         str = str..pet:GetName();
  48.         str = str..orderTypes[orderType];
  49.         if (orderType==2) then
  50.             local abilityId, abilityName = C_PetBattles.GetAbilityInfoByID(pet:GetAbilityId(orderValue));
  51.             str = str..abilityName;
  52.         else
  53.             str = str..orderValue;
  54.         end
  55.     else
  56.         str = str.."no Action";
  57.     end
  58.  
  59.     print(str);
  60. end
  61. function BattleRecord:PrintAll()
  62.     print ("--- BATTLE RECORD ---");
  63.     print ("[TamerID: "..self.tamerId.."][NumRounds: "..#self.rounds.."][Successrate: "..self.GetSuccessRate().."%]");
  64.     print ("Teams:");
  65.     for t = 1, 2 do
  66.         local teamTable ={};
  67.         local str = " Team "..t..": [";
  68.         for p = 1, #self.petSpeciesIds[t] do
  69.             local id = self.petSpeciesIds[t][p];
  70.             local info = {C_PetJournal.GetPetInfoBySpeciesID(id)};
  71.             teamTable[p] = info[1];
  72.         end
  73.         str = str..table.concat(teamTable, ", ");
  74.         print(str);
  75.     end
  76.     for i=1, self.GetNumRounds() do
  77.         self.PrintRound(i);
  78.     end
  79. end
  80. function BattleRecord:GetRound(roundIndex)
  81.     return self.rounds[roundIndex];
  82. end
  83. function BattleRecord:GetTamerId()
  84.     return self.tamerId;
  85. end
  86. function BattleRecord:GetSpeciesId(ownerIndex, petIndex)
  87.     return self.petSpeciesIds[ownerIndex][petIndex];
  88. end
  89. function BattleRecord:AddRoundTable(roundTable)
  90.     self.rounds[#self.rounds+1] = roundTable;
  91. end
  92. function BattleRecord:GetNumRounds()
  93.     return #self.rounds;
  94. end
  95. function BattleRecord:AddRound(activePet, orderType, orderValue)
  96.     if (orderType~= nil) then
  97.         self:AddRoundTable({activePet, orderType, orderValue});
  98.     else
  99.         self:AddRoundTable({});
  100.     end
  101. end

when i run it and the BattleRecord:PrintAll() is called it gives me this error:
Code:
Date: 2014-12-25 21:13:24
ID: 1
Error occured in: Global
Count: 1
Message: ..\AddOns\CounterPet\class_BattleRecord.lua line 30:
   attempt to index local 'self' (a nil value)
Debug:
   CounterPet\class_BattleRecord.lua:30: GetSuccessRate()
   CounterPet\class_BattleRecord.lua:63: PrintAll()
   CounterPet\class_Battle.lua:63: End()
   CounterPet\CounterPet.lua:31:
      CounterPet\CounterPet.lua:24
Locals:
self = nil
(*temporary) = <function> defined @Interface\FrameXML\RestrictedInfrastructure.lua:116
(*temporary) = "self: nil"
(*temporary) = <function> defined =[C]:-1
(*temporary) = true
(*temporary) = "attempt to index local 'self' (a nil value)"
Why is self nil in the BattleRecord:GetSuccessRate? *veryconfused*
  Reply With Quote
12-25-14, 02:37 PM   #2
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
It's nil because you're calling self.GetSuccessRate() instead of self:GetSuccessRate() or self.GetSuccessRate(self) on line 63.
  Reply With Quote
12-25-14, 03:44 PM   #3
MrBerlin
A Murloc Raider
Join Date: Dec 2014
Posts: 4
thx. sometimes confusing mixing several laguages in brain

qac
  Reply With Quote
12-25-14, 08:27 PM   #4
AnrDaemon
A Chromatic Dragonspawn
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 156
Originally Posted by MrBerlin View Post
thx. sometimes confusing mixing several laguages in brain

qac
I can attest to that.
The rule I've learned about Lua: when you want your first parameter to be a class reference, declare function as class:funcname().
Then you automatically get "self" reference.
  Reply With Quote
12-26-14, 11:37 AM   #5
AlleyKat
A Warpwood Thunder Caller
 
AlleyKat's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 93
lua is not OOP language, keep it in mind
  Reply With Quote
12-26-14, 09:42 PM   #6
Vlad
A Molten Giant
 
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 793
Originally Posted by AlleyKat View Post
lua is not OOP language, keep it in mind
http://en.wikipedia.org/wiki/List_of...ming_languages
__________________
Profile: Curse | Wowhead
  Reply With Quote
12-27-14, 08:32 AM   #7
AnrDaemon
A Chromatic Dragonspawn
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 156
Pediwikia is a poor argument.
  Reply With Quote
12-27-14, 06:43 PM   #8
AlleyKat
A Warpwood Thunder Caller
 
AlleyKat's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 93
It's not true OOP language, things like
Code:
function MyTable:GetSomething()
 return self.something;
end
looks stupid
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » self is nil

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