Put a script entity called "script_entity_1" in your mod, and put this code in it:
Code: Select all
function giveMouseItem(item_id, altar_id)
local item_local = findEntity(item_id)
local altar_local = findEntity(altar_id)
if ((item_local ~= nil) and (altar_local ~= nil)) then
setMouseItem(item_local.item)
altar_local:destroy()
end
end
This bag only accepts keys:
Code: Select all
defineObject{
name = "key_bag",
baseObject = "base_item",
components = {
{
class = "Model",
model = "assets/models/items/sack_empty.fbx",
},
{
class = "Item",
uiName = "Key Bag",
gfxIndex = 82,
gfxIndexContainer = 482,
weight = 0.4,
fitContainer = false,
},
{
class = "ContainerItem",
containerType = "sack",
openSound = "container_sack_open",
closeSound = "container_sack_close",
onInit = function(self)
if self:getItemCount() > 0 then
self.go.model:setModel("assets/models/items/sack_full.fbx")
self.go.item:setGfxIndex(81)
self.go.item:updateBoundingBox()
end
end,
onInsertItem = function(self, item, slot)
if (item:hasTrait("key")) then
self.go.data:set("item_id", nil)
self.go.data:set("altar_id", nil)
-- convert to full sack
if self.go.item:getGfxIndex() == 82 then
self.go.model:setModel("assets/models/items/sack_full.fbx")
self.go.item:setGfxIndex(81)
self.go.item:updateBoundingBox()
end
else
hudPrint("This bag only accepts keys!")
self.go.data:set("item_id", item.go.id)
local altar_local = spawn("altar", party.level, party.x, party.y, party.facing, party.elevation)
self.go.data:set("altar_id", altar_local.id)
altar_local.surface:addItem(item)
self:removeItemFromSlot(slot)
if (getMouseItem() ~= nil) then
if (getMouseItem().go.id ~= item.go.id) then
self:insertItem(slot, getMouseItem())
end
end
end
end,
onRemoveItem = function(self, item)
if ((self.go.data:get("item_id") ~= nil) and (self.go.data:get("altar_id") ~= nil)) then
delayedCall("script_entity_1", 0, "giveMouseItem", self.go.data:get("item_id"), self.go.data:get("altar_id"))
self.go.data:set("item_id", nil)
self.go.data:set("altar_id", nil)
end
-- convert to empty sack when last item is removed
if self:getItemCount() == 0 and self.go.item:getGfxIndex() == 81 then
self.go.model:setModel("assets/models/items/sack_empty.fbx")
self.go.item:setGfxIndex(82)
self.go.item:updateBoundingBox()
end
end,
},
{
class = "Script",
name = "data",
source = [[
data = { ["item_id"] = nil, ["altar_id"] = nil }
get = function(self, name)
return self.data[name]
end
set = function(self, name, value)
self.data[name] = value
end
]],
}
},
}
Based on
viewtopic.php?f=22&t=8464&p=93490#p93489