[Lua:Help:Solved] onChangeInventory
Posted: Mon Oct 15, 2012 6:58 am
I can't find the error. I have something like a "Nanny-Wave" wich i shoot at my Champions every 0.01 second. They look into their inventory and give them to bonus they should have gotten and taking away Items which might be in the way and so on. And now I like to teach my "Nanny-Wave" to look for a special thing to happen. ... an onInventoryChange ... event.
I have a Table named myParty and in it there are the inventories of my champions. My timer calls the myParty.inventoryChanged() before the myParty.refresh(). Both are called correctly, as far as I can see.
In the myParty.inventoryChanged() function the Champions are correctly picked(I have only one Champion enabled). But where I compare the Inventories of myParty and party, it seems like it ... well it finds the items and it can tell me the name of it, but for some reason the boolean variable changedInventory seems to stay false.
I am looking at a wall, not only in the game. Has anyone a suggestion?
Here is the code:
Here the working solution and additional functions:
I have a Table named myParty and in it there are the inventories of my champions. My timer calls the myParty.inventoryChanged() before the myParty.refresh(). Both are called correctly, as far as I can see.
In the myParty.inventoryChanged() function the Champions are correctly picked(I have only one Champion enabled). But where I compare the Inventories of myParty and party, it seems like it ... well it finds the items and it can tell me the name of it, but for some reason the boolean variable changedInventory seems to stay false.
I am looking at a wall, not only in the game. Has anyone a suggestion?
Here is the code:
SpoilerShow
Code: Select all
changedInventory = false
enabledChampions = 0
-- inventory recognition table for onChangeInventory
myParty = {
champion ={
{
name = party:getChampion(1):getName(),
isEnabled = party:getChampion(1):getEnabled(),
inventory = {}
},
{
name = party:getChampion(2):getName(),
isEnabled = party:getChampion(2):getEnabled(),
inventory = {}
},
{
name = party:getChampion(3):getName(),
isEnabled = party:getChampion(3):getEnabled(),
inventory = {}
},
{
name = party:getChampion(4 ):getName(),
isEnabled = party:getChampion(4):getEnabled(),
inventory = {}
}
}
}
function myParty.refresh()
for i=1,4 do
if(party:getChampion(i):getEnabled()) then
local champ = party:getChampion(i)
for x=1,31 do
myParty.champion[i].inventory[x] = champ:getItem(x)
end
end
end
end
-- set changedInventory true if Inventory has Changed
function myParty.inventoryChanged()
for i=1,4 do
-- pick only enabled Champions
if(party:getChampion(i):getEnabled()) then
--shortcuts for the champions for mystate and gamestate
local champ = party:getChampion(i)
local champstate = myParty.champion[i]
-- compare inventories of myParty and party
for x=1,31 do
if(champstate.inventory[x] and champ:getItem(x)) then
if(champstate.inventory[x].name ~= champ:getItem(x).name) then
-- x1 x2
changedInventory = true
else
-- x1 x1
changedInventory = false
end
else
if(champstate.inventory[x] or champ:getItem(x)) then
--- x nil
changedInventory = true
else
-- nil nil
changedInventory = false
end
end
-- debug output
if champstate.inventory[x] then
print(champstate.inventory[x].name)
end
end
end
end
-- debug output
if(changedInventory) then
hudPrint("Inventory Change Event!")
else
--hudPrint("No Custom Event!")
end
end
function myParty.getInventoryItem(item)
for i=1,4 do
if(party:getChampion(i):getEnabled()) then
local champ = party:getChampion(i)
for x=1,31 do
if(champ:getItem(x)) then
if(champ:getItem(x).name == item) then
return true
end
end
end
end
end
return false
end
SpoilerShow
Code: Select all
changedInventory = false
-- inventory recognition table for onChangeInventory
myParty = {
champion ={
{
name = party:getChampion(1):getName(),
isEnabled = party:getChampion(1):getEnabled(),
inventory = {}
},
{
name = party:getChampion(2):getName(),
isEnabled = party:getChampion(2):getEnabled(),
inventory = {}
},
{
name = party:getChampion(3):getName(),
isEnabled = party:getChampion(3):getEnabled(),
inventory = {}
},
{
name = party:getChampion(4 ):getName(),
isEnabled = party:getChampion(4):getEnabled(),
inventory = {}
}
}
}
function myParty.refresh()
for i=1,4 do
if(party:getChampion(i):getEnabled()) then
local champ = party:getChampion(i)
for x=1,31 do
myParty.champion[i].inventory[x] = champ:getItem(x)
end
end
end
end
-- set changedInventory true if Inventory has Changed
function myParty.inventoryChanged()
for i=1,4 do
if(changedInventory) then break end
-- pick only enabled Champions
if(party:getChampion(i):getEnabled()) then
--shortcuts for the champions for mystate and gamestate
local champ = party:getChampion(i)
local champstate = myParty.champion[i]
-- compare inventories of myParty and party
for x=1,31 do
if(champstate.inventory[x] and champ:getItem(x)) then
if(champstate.inventory[x].name ~= champ:getItem(x).name) then
-- x1 x2
changedInventory = true
break
else
-- x1 x1
changedInventory = false
end
else
if(champstate.inventory[x] or champ:getItem(x)) then
--- x nil
changedInventory = true
break
else
-- nil nil
changedInventory = false
end
end
-- debug output
if champstate.inventory[x] then
print(champstate.inventory[x].name)
end
end
end
end
if(changedInventory) then
----------------------------------------------
-- onInventoryChange actions here... ---------
----------------------------------------------
hudPrint("Inventory Change Event!")
myParty.onInventoryChanged()
changedInventory = false
else
--hudPrint("No Custom Event!")
end
end
function myParty.onInventoryChanged()
end
function myParty.getInventoryItem(item)
for i=1,4 do
if(party:getChampion(i):getEnabled()) then
local champ = party:getChampion(i)
for x=1,31 do
if(champ:getItem(x)) then
if(champ:getItem(x).name == item) then
return true
end
end
end
end
end
return false
end