I modified it a bit and made use of delayedCall(). So now instead of dropping the lock picks on the ground, delayedCall will give them back to you in 0.2 seconds.
I also added a line to get your key back if you want to have it given back.
It works perfect. I am marking this solved.
One last comment is how the normal chest comes with a component called "lock". This is named very poorly. This is not a LockComponet. It's the lock model, "treasure_chest_lock.model". We should have never been touching this to solve the problem. Oh well, we know now.
Thanks everyone for your help. Final (UPDATED 12/21) code:
In objects.lua:
Code: Select all
defineObject{
name = "chest_keylocked",
baseObject = "chest",
components = {
{
class = "Lock",
name = "keyLock"
},
{
class = "Clickable",
offset = vec(0,0.5,0),
size = vec(1.5, 1.0, 1.0),
maxDistance = 1,
onClick = function(self)
if not self.go.chest:getLocked() then
if self.go:getComponent("keyLock") then
self.go:removeComponent("keyLock")
end
return true
else
local used_item = getMouseItem()
if not self.go.chest:getLocked() then
return false
end
if used_item == nil then
return false
end
local stackSize = used_item:getStackSize()
if used_item.go.name == 'lock_pick' then
setMouseItem(nil)
playSound("lock_incorrect")
hudPrint("This chest requires a key and cannot be picked.")
delayedCall("chest_scr", 0.2, "delayedGiveLockPick", used_item.go.name, stackSize)
return false
elseif not used_item.go.item:hasTrait("key") then
hudPrint("You must open the chest with a key.")
return false
elseif self.go.keyLock:getOpenedBy() == used_item.go.name then
self.go.chest:setLocked(false)
setMouseItem(nil)
self.go.lock:disable()
playSound("key_lock")
self.go:removeComponent("keyLock")
-- If you don't want the key back, remove the line below
delayedCall("chest_scr", 0.2, "delayedGiveLockPick", used_item.go.name)
return true
else
hudPrint("Not the right key.")
playSound("key_lock_faint")
return false
end
end
end
},
},
}
Code: Select all
function delayedGiveLockPick(itemName, stackSize)
local lockpick = spawn(itemName,party.level,party.x,party.y,party.elevation)
if stackSize then
lockpick.item:setStackSize(stackSize)
end
setMouseItem(lockpick.item)
end