(using version 2.2.4)
Reason:
There are items that increase a champion's maximum energy, like the gear_necklace in
/assets/scripts/items/accessories.lua
But when you un-equip such an item, the surplus energy is lost.
My first attempt looked like this:
(it uses a simplified version of JKos's code from the thread Storing data to entities and other scripting tricks)
Code: Select all
defineObject{
name = "necklace_of_energy_1",
baseObject = "base_item",
components = {
{
class = "Model",
model = "assets/models/items/gear_necklace.fbx",
},
{
class = "Item",
uiName = "Necklace of Energy 1",
gfxIndex = 219,
weight = 1.0,
traits = { "necklace" },
description = "A necklace that stores energy.",
onEquipItem = function(self, champion, slot)
if (slot == ItemSlot.Necklace) then
champion:regainEnergy(self.go.data:get("stored_energy"))
self.go.data:set("stored_energy", 0)
end
end,
onUnequipItem = function(self, champion, slot)
if (slot == ItemSlot.Necklace) then
-- store the surplus energy in the item
if (champion:getEnergy() > (champion:getMaxEnergy() - 15)) then
self.go.data:set("stored_energy", champion:getEnergy() - (champion:getMaxEnergy() - 15))
end
end
end,
},
{
class = "EquipmentItem",
energy = 15,
},
{
class = "Script",
name = "data",
source = [[
data = {["stored_energy"] = 15}
get = function(self, name)
return self.data[name]
end
set = function(self, name, value)
self.data[name] = value
end
]]
}
},
}(is this a bug or intended behavior?)
My second attempt was to call 'champion:regainEnergy(self.go.data:get("stored_energy"))' when the 15 energy modifier has been applied:
Code: Select all
defineObject{
name = "necklace_of_energy_2",
baseObject = "base_item",
components = {
{
class = "Model",
model = "assets/models/items/gear_necklace.fbx",
},
{
class = "Item",
uiName = "Necklace of Energy 2",
gfxIndex = 219,
weight = 1.0,
traits = { "necklace" },
description = "A necklace that stores energy.",
onEquipItem = function(self, champion, slot)
if (slot == ItemSlot.Necklace) then
if (self.go.data:get("stored_energy") > 0) then
self.go.data:set("new_max_energy", champion:getMaxEnergy() + 15)
end
end
end,
onUnequipItem = function(self, champion, slot)
if (slot == ItemSlot.Necklace) then
-- store the surplus energy in the item
if (champion:getEnergy() > (champion:getMaxEnergy() - 15)) then
self.go.data:set("stored_energy", champion:getEnergy() - (champion:getMaxEnergy() - 15))
end
end
end,
},
{
class = "EquipmentItem",
energy = 15,
onRecomputeStats = function(self, champion)
if (champion:getMaxEnergy() == self.go.data:get("new_max_energy")) then
self.go.data:set("new_max_energy", -1)
champion:regainEnergy(self.go.data:get("stored_energy"))
self.go.data:set("stored_energy", 0)
end
end
},
{
class = "Script",
name = "data",
source = [[
data = {["stored_energy"] = 15, ["new_max_energy"] = -1}
get = function(self, name)
return self.data[name]
end
set = function(self, name, value)
self.data[name] = value
end
]]
}
},
}(again, is this a bug or intended behavior?)