Hiho, here my code for the class:
Lua Code:
BattleRecord = {};
BattleRecord.__index = BattleRecord;
setmetatable(BattleRecord, {
__call = function (cls, ...)
return cls.new(...)
end,
});
function BattleRecord.new(tamerId, petSpeciesIds, attackIds, triesTable)
local self = setmetatable({}, BattleRecord);
self.tamerId = tamerId;
self.petSpeciesIds= {{1,2,3},{4,5,6}};
--self.petSpeciesIds = petSpeciesIds;
self.rounds={};
self.attackIds = {};
self.tries = {0,0};
if (triesTable ~= nil) then
--self.tries = triesTable;
end
print("battleRecord init SElf:"..(tostring(self) or "nil"));
return self;
end
function BattleRecord:GetTriesTable()
return self.tries;
end
function BattleRecord:GetSuccessRate()
print("self: "..tostring(self));
if (#self.tries == 0) then
return 0;
end
if (self.tries[2]>0) then
return Math.ceil(self.tries[1]/self.tries[2]);
end
return 0;
end
function BattleRecord:PrintRound(round)
local str = "Round: "..round..": ";
local trainer = self.trainers[1];
local roundTable = self.rounds[round];
if (#roundTable>0) then
local pet = trainer:GetPet(self.orders[round][1]);
local orderType = self.orders[round][2];
local orderValue = self.orders[round][3];
local orderTypes = {"", " uses ", "got switched to ", " used a trap ", " skipped the turn "};
str = str..pet:GetName();
str = str..orderTypes[orderType];
if (orderType==2) then
local abilityId, abilityName = C_PetBattles.GetAbilityInfoByID(pet:GetAbilityId(orderValue));
str = str..abilityName;
else
str = str..orderValue;
end
else
str = str.."no Action";
end
print(str);
end
function BattleRecord:PrintAll()
print ("--- BATTLE RECORD ---");
print ("[TamerID: "..self.tamerId.."][NumRounds: "..#self.rounds.."][Successrate: "..self.GetSuccessRate().."%]");
print ("Teams:");
for t = 1, 2 do
local teamTable ={};
local str = " Team "..t..": [";
for p = 1, #self.petSpeciesIds[t] do
local id = self.petSpeciesIds[t][p];
local info = {C_PetJournal.GetPetInfoBySpeciesID(id)};
teamTable[p] = info[1];
end
str = str..table.concat(teamTable, ", ");
print(str);
end
for i=1, self.GetNumRounds() do
self.PrintRound(i);
end
end
function BattleRecord:GetRound(roundIndex)
return self.rounds[roundIndex];
end
function BattleRecord:GetTamerId()
return self.tamerId;
end
function BattleRecord:GetSpeciesId(ownerIndex, petIndex)
return self.petSpeciesIds[ownerIndex][petIndex];
end
function BattleRecord:AddRoundTable(roundTable)
self.rounds[#self.rounds+1] = roundTable;
end
function BattleRecord:GetNumRounds()
return #self.rounds;
end
function BattleRecord:AddRound(activePet, orderType, orderValue)
if (orderType~= nil) then
self:AddRoundTable({activePet, orderType, orderValue});
else
self:AddRoundTable({});
end
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*