Page 1 of 3
[Solved!] Want to make a chest openable with a key
Posted: Mon Nov 17, 2014 4:12 am
by MrChoke
I would like to make a
chest that has to be opened with a key instead of
lock picks. I am quickly running into problems.
I see ChestComponet already has a
lock component on it but it gives no options in the editor to customize it. So I thought to define a LockComponent of my own on it. That actually worked to a point in that it recognizes only using the correct key unlocks. But when I try to unlock it, it blows up.
Looks like I need to define animations here but I really don't know how or what to define. Being able to re-use the
chest opening animation for a
lock pick is perfectly fine by me if I knew how to use it. Does anyone know much about how to do this? Or am I out of luck until the asset definition comes out.
Thanks
Re: Want to make a chest openable with a key
Posted: Mon Nov 17, 2014 11:20 am
by Prozail
You add something like
Code: Select all
defineObject {
name = "chest_padlocked",
baseObject = "chest",
components = {
{
class = "Lock",
name = "keylock",
openedBy = "iron_key",
onActivate = function(self)
self.go.chest:setLocked(false)
self.go.animation:stop()
end,
},
{
class = "Animation",
animations = {
lock = "assets/animations/env/treasure_chest_open.fbx",
open = "assets/animations/env/treasure_chest_open.fbx",
close = "assets/animations/env/treasure_chest_close.fbx",
}
},
}
}
Edit: There seems to be more quirks to work out... This was alot trickier than i thought... ill have a look later..
Edit2: The above sortof works.. trouble is you cant interact with anything in the
chest, even if you unlock it with a regular lockpick... Hell even if you leave it totally unlocked. The surface just doesn't react. (There is some magic ingame code to disable and enable a Surface inside the
chest). Even tried manually manipulating it through the console.
Re: Want to make a chest openable with a key
Posted: Mon Nov 17, 2014 1:20 pm
by Scotty
This is how I did it for my dungeon, though I'm pretty sure it would also be opened with a lockpick so you might have to figure out a way to disable that.
A connector that runs a function onClick, in this case the function unlockChest() in my library script
Code: Select all
spawn("chest",level_index, herei, herej, 0, 0, chest_ID).clickable:addConnector("onClick", "library", "unlockChest")
and the function:
Code: Select all
function unlockChest(entity)
if getMouseItem() ~= nil then
if entity.go.chest:getLocked() and getMouseItem().go.name == "iron_key" then
setMouseItem()
entity.go.chest:setLocked(false)
end
end
end
That'll unlock the
chest and delete the iron key in hand.
Re: Want to make a chest openable with a key
Posted: Mon Nov 17, 2014 2:02 pm
by JKos
Prozail, your code almost worked, just rename the keylock to
lock.
Code: Select all
defineObject {
name = "chest_padlocked",
baseObject = "chest",
components = {
{
class = "Lock",
name = "lock",
openedBy = "iron_key",
onActivate = function(self)
self.go.chest:setLocked(false)
self.go.animation:stop()
end,
},
{
class = "Animation",
animations = {
lock = "assets/animations/env/treasure_chest_open.fbx",
open = "assets/animations/env/treasure_chest_open.fbx",
close = "assets/animations/env/treasure_chest_close.fbx",
}
},
}
}
It had 2
lock (
lock and keylock) components so that's why it didn't work.
Re: Want to make a chest openable with a key
Posted: Mon Nov 17, 2014 2:21 pm
by MrChoke
Thanks for your help. I thought it would be easy to do. I see I am not the only one who found this not so easy.
@JKos, your solution looks almost exactly like mine and it didn't work right... let me try yours out though. Maybe I am missing something.
@Scotty, I couldn't get Clickable to override for a
chest. Even when I used the exact same offset and size as the original one. I tried override in defineObject though, not with addConnector(). That must be why.
Also at @Scotty. I played your dungeon a bit. Nice! You know we are going to be competing right?
Re: Want to make a chest openable with a key
Posted: Mon Nov 17, 2014 2:23 pm
by Scotty
Also at @Scotty. I played your dungeon a bit. Nice! You know we are going to be competing right?
Game on!
Re: Want to make a chest openable with a key
Posted: Mon Nov 17, 2014 2:34 pm
by JKos
MrChoke wrote:
@JKos, your solution looks almost exactly like mine and it didn't work right... let me try yours out though. Maybe I am missing something.
It was not my solution but Prozail's, so credits to him
I just happened to notice that he didn't overwrite the default
lock-component. And I tested it and it seemed to work pretty well, but it can still be opened with
lock-picks too, so some fine tuning may be needed.
Re: Want to make a chest openable with a key
Posted: Mon Nov 17, 2014 2:48 pm
by Prozail
I was actually consciously avoiding to remove the
lock-Component as it is a Model (the
lock on the
chest) that gets disabled when you unlock it.
But your solution works! And why? Because the normal chesthandler CALLS DISABLE ON THE
LOCK when it gets unlocked...
Re: Want to make a chest openable with a key
Posted: Mon Nov 17, 2014 2:53 pm
by MrChoke
JKos wrote:MrChoke wrote:
@JKos, your solution looks almost exactly like mine and it didn't work right... let me try yours out though. Maybe I am missing something.
It was not my solution but Prozail's, so credits to him
I just happened to notice that he didn't overwrite the default
lock-component. And I tested it and it seemed to work pretty well, but it can still be opened with
lock-picks too, so some fine tuning may be needed.
Yeah, I had the exact same thing you had, except I typed "onActivated" and not "onActivate" for the
lock hook. Sigh.
A minor thing with the solution is the
chest doesn't unlock first, it goes straight to open. With
lock picks, first you unlock it, then you click to open it. This is minor though. Who wouldn't want to look into the
chest right away anyway!
The
lock picks still work though. Hmmmm... I guess I could always just remove them from my dungeon. I may not want them anyway. Still wish we can get this thing to work 100%
Re: Want to make a chest openable with a key
Posted: Mon Nov 17, 2014 3:11 pm
by Prozail
Ok.. this is getting wierd
The code below will work fine, as long as you DONT unlock it with a lockpick.. then it bug's out (as the
lock-Component doesn't get removed, and then you cant click stuff in the
chest)... If you use a key its fine, but the native click-event still fires, and types "Locked." on the hud.
It's basically the same as my first one (where i dont override the "
lock") but here i remove the LockComponent after it has triggered.
Problem is the click-event can't be replaced, because it does things that are not available through the modding-script-Engine at this time. (onBeginMouseLook etc)
Code: Select all
defineObject {
name = "chest_padlocked",
baseObject = "chest",
components = {
{
class = "Lock",
name = "keylock",
openedBy = "iron_key",
onActivate = function(self)
self.go.chest:setLocked(false)
self.go.animation:stop()
self.go:removeComponent("keylock")
end,
},
{
class = "Animation",
animations = {
lock = "assets/animations/env/treasure_chest_open.fbx",
open = "assets/animations/env/treasure_chest_open.fbx",
close = "assets/animations/env/treasure_chest_close.fbx",
}
},
}
}
tbh... if i was to implement it, i would prob. use scotty's version of just putting a custom click-event after the original one.