How do you assign a key to a chest?

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Post Reply
User avatar
The_Morlock
Posts: 9
Joined: Sun Jun 02, 2024 6:15 am

How do you assign a key to a chest?

Post by The_Morlock »

Hey everyone,

New here and new to LoG and the Dungeon Editor. I've kinda gone headfirst into the editor as I liked the capabilities and how intuitive it is to create maps.

Having messed with the LoG 1 editor, I thought I'd buy LoG 2 and get the updated editor and assets. Done and so far, it is pretty neat.

Some things are a little different from the LoG 1 editor, a little less intuitive, IMHO. One thing I am having trouble with is how do you assign a key, or at least a key type, to a chest? I made a connector from the brass key to the chest and assigned Chest Locked = true. With this, it does not open though.

Sorry for the relatively easy question, but I can't seem to figure this out.

Thanks all and great job Almost Human!
User avatar
THOM
Posts: 1267
Joined: Wed Nov 20, 2013 11:35 pm
Location: Germany - Cologne
Contact:

Re: How do you assign a key to a chest?

Post by THOM »

Right from the basic game design you can't.

Chests get opened by lockpicks and nothing else. It's also not a certain lockpick - everyone works on every chest.

You can change the look of the lockpicks by changing the icon (and also the name if you want) - but that's all.

I'm pretty sure you can make some tweaks to get chests opended by other keys, but this must be a bit tricky and I can't help with that.
THOM formaly known as tschrage
_______________________________________________
My MOD (LoG1): Castle Ringfort Thread
My MOD (LoG2): Journey To Justice Thread | Download
User avatar
ratman
Posts: 159
Joined: Fri Jan 10, 2020 1:13 am

Re: How do you assign a key to a chest?

Post by ratman »

This solution requires a bit of scripting, but works well enough:

Place a chest on the map, and disable the 'clickable' component. Enable the 'lock' component (do not enable the 'locked' checkbox inside the 'chest' component.)
place a lock in front of the chest and disable the 'model' component. Assign whichever key you like.
Place a script entity, and paste the following code into it, replacing any object names with your own:

Code: Select all

lock_1:setWorldPositionY(-0.75)   --lowers the lock so it is level with the chest's lock

function unlockChest()
chest_1.lock:disable()            --disables the lock model on the chest
chest_1.clickable:enable()        --allows the player to open the chest by clicking
lock_1.clickable:disable()        --prevents the lock from getting in the way when players are opening the chest
end
Assign a connecter from the lock to the script, and you have a working chest lock. :)
User avatar
The_Morlock
Posts: 9
Joined: Sun Jun 02, 2024 6:15 am

Re: How do you assign a key to a chest?

Post by The_Morlock »

ratman wrote: Sun Jun 02, 2024 5:08 pm This solution requires a bit of scripting, but works well enough:

Place a chest on the map, and disable the 'clickable' component. Enable the 'lock' component (do not enable the 'locked' checkbox inside the 'chest' component.)
place a lock in front of the chest and disable the 'model' component. Assign whichever key you like.
Place a script entity, and paste the following code into it, replacing any object names with your own:

Code: Select all

lock_1:setWorldPositionY(-0.75)   --lowers the lock so it is level with the chest's lock

function unlockChest()
chest_1.lock:disable()            --disables the lock model on the chest
chest_1.clickable:enable()        --allows the player to open the chest by clicking
lock_1.clickable:disable()        --prevents the lock from getting in the way when players are opening the chest
end
Assign a connecter from the lock to the script, and you have a working chest lock. :)
Thank yoy very much, this worked quite well with some tinkering on my part.
Chest Name: brasschest1 (no connector)
Lock Name: brasschest_lock1 (connects to brasschest_scriptentity1)
Key Name: brasschest_key1 (no connector)
Script Name: brasschest_scriptentity1

brasschest_scriptentity1 Script:

Code: Select all

brasschestlock_1:setWorldPositionY(-0.75)   --lowers the lock so it is level with the chest's lock

function unlockChest()
brasschest1.lock:disable()            --disables the lock model on the chest
brasschest1.clickable:enable()        --allows the player to open the chest by clicking
brasschest_lock1.clickable:disable()        --prevents the lock from getting in the way when players are opening the chest
end
Looks like, though that any brass key will do instead of a specific one. BUT, this does work.
User avatar
DaggorathMaster
Posts: 41
Joined: Thu Sep 08, 2022 7:29 pm

Re: How do you assign a key to a chest?

Post by DaggorathMaster »

For the active "clickable" component (brasschest1.clickable):
Have the onClick hook call a script function* which checks the id of the mouse item (the mouse item is what you are clicking with).

(Anything in <> brackets is whatever you name(d) it).

Code: Select all

function <try_open_chest> (clickable)
    if getMouseItem().go.id == <desired_id> then
        <open_chest> (clickable.go) -- Or whatever your function is to open it, and what argument(s) you call it with.
    else
        <fail_to_open_chest> (clickable.go) -- Optional function for failing to unlock it, may just play the sad no unlock sound.
    end
end
I would also go with naming the key after the chest, with _key at the end.
So, for example, felldurs_brass_chest and felldurs_brass_chest_key
Then, if you want to do this on more than one chest, it is simple to reuse the script:

(Editing just the one line with "getMouseItem"):

Code: Select all

if getMouseItem().go.id:find (clickable.go.id) then
...
*Having def files do little or nothing but call an embedded ("normal") script file saves some headaches - with reloading and debugging.
Post Reply