[Solved!] Want to make a chest openable with a key

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!
MrChoke
Posts: 324
Joined: Sat Oct 25, 2014 7:20 pm

Re: Want to make a chest openable with a key

Post by MrChoke »

Prozail wrote: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.
Yup, that is what I see too. There is a hard-coded Clickable that you cannot touch and you cannot stop. When I try to replace via defineObject, I kill the chest altogether. I did do addConnector() after spawn (Scotty's suggestion). I get to my onClick to fire but it fires BEFORE the hard-coded Clickable event. When I print getLocked() at this time it is still true with lock picks in hand. It then unlocks it. I cannot stop it. Returning false doesn't do it either.
User avatar
JKos
Posts: 464
Joined: Wed Sep 12, 2012 10:03 pm
Location: Finland
Contact:

Re: Want to make a chest openable with a key

Post by JKos »

Prozail wrote: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.
Oh, I didn't even notice that the lock was missing, tested it so quickly. That's too bad.
- LoG Framework 2http://sites.google.com/site/jkoslog2 Define hooks in runtime by entity.name or entity.id + multiple hooks support.
- cloneObject viewtopic.php?f=22&t=8450
User avatar
Doridion
Posts: 256
Joined: Tue Jun 10, 2014 9:23 pm

Re: Want to make a chest openable with a key

Post by Doridion »

I even done this system with my unlockable doors, you can pick-up the code ;)
MrChoke
Posts: 324
Joined: Sat Oct 25, 2014 7:20 pm

Re: Want to make a chest openable with a key

Post by MrChoke »

Doridion wrote:I even done this system with my unlockable doors, you can pick-up the code ;)
I saw your code on clickable doors. You have code where you added lockable too? I would like to see that if you have the URL handy. Though tbh, I think chest are different. There is something hard-coded with clickable and lock picks. I wish I could override it.
User avatar
Doridion
Posts: 256
Joined: Tue Jun 10, 2014 9:23 pm

Re: Want to make a chest openable with a key

Post by Doridion »

MrChoke wrote:
Doridion wrote:I even done this system with my unlockable doors, you can pick-up the code ;)
I saw your code on clickable doors. You have code where you added lockable too? I would like to see that if you have the URL handy. Though tbh, I think chest are different. There is something hard-coded with clickable and lock picks. I wish I could override it.
Doors ad chests are n the same system : they must add lock component to be locked, so in this way, I as my code is on the clickable class, it could runs ^^ Belows my door code, and after a possible chest script.
SpoilerShow

Code: Select all

defineObject{
	name = "my_clickable_door",
   baseObject = "[color=#40FFBF][name of the door you want to use][/color]",
	components = {
	{
		class = "Door",
		sparse = true,
		killPillars = true,
    },
	{
		class = "Clickable",
		maxDistance = 1,
		offset = vec(0,1.3,0),
		size = vec(2.5,2.5,0.3),
		onClick = function(self)
			local used_item = getMouseItem()
			if self.go.lock:getOpenedBy() == ("" or nil) then
				self.go.door:toggle()
			else
				if used_item == nil then
					hudPrint("Door is locked ")
				elseif used_item.go.item:hasTrait("key") then
					if self.go.lock:getOpenedBy() == used_item.go.name then
						self.go.door:open()
						self.go.lock:setOpenedBy("")
						setMouseItem(nil)
						hudPrint("Door is unlocked")
					else
						hudPrint("Not the right key")
					end
				else
					hudPrint("You must open the door with a key")
				end
			end
		end
	},
	{
		class = "Controller",
		onOpen = function(self, name)
			self.go.door:open()
		end,
		onClose = function(self)
			self.go.door:close()
		end,
		onToggle = function(self)
			self.go.door:toggle()
		end,
	},
	{
		class = "Lock",
	},
  },
  placement = "wall",
  editorIcon = 128,
}
Script for chest :
SpoilerShow

Code: Select all

defineObject{
   name = "chest_padlocked",
   baseObject = "chest",
   components = {
		{
        class = "Lock",
        openedBy = nil,
		},
		{
		class = "Clickable",
		maxDistance = 1,
		onClick = function(self)
			local used_item = getMouseItem()
			if self.go.chest:getLocked() then
				if used_item == nil then
					return false
				elseif used_item.go.item:hasTrait("key") then
					if self.go.lock:getOpenedBy() == used_item.go.name then
						self.go.chest:setLocked(false)
						self.go.lock:setOpenedBy("")
						setMouseItem(nil)
						hudPrint("Chest is unlocked")
						return true
					else
						hudPrint("Not the right key")
						return false
					end
				else
					hudPrint("You must open the chest with a key")
					return false
				end
			end
		end
		},
	},
}
There it is ;) ( tested )
User avatar
Prozail
Posts: 158
Joined: Mon Oct 27, 2014 3:36 pm

Re: Want to make a chest openable with a key

Post by Prozail »

There are still the same problems. The Model of the lock is not there, and you can still unlock it with a lock_pick.
User avatar
Doridion
Posts: 256
Joined: Tue Jun 10, 2014 9:23 pm

Re: Want to make a chest openable with a key

Post by Doridion »

Prozail wrote:There are still the same problems. The Model of the lock is not there, and you can still unlock it with a lock_pick.
Prozail, with my solution, no need to have lock model ... already coded to recognize if chest is locked/unlocked, what key you have, and unlocking chest if it's the right key. For the lock_pick, yep, I must work on it :D Seems that chest component pass thought the clickable ((

For the lock model, you can either add it with :

Code: Select all

{
class = "Model",
name = "lockModel",
model = "my_model.fbx",
offset = vec(x,y,z)
},
User avatar
Prozail
Posts: 158
Joined: Mon Oct 27, 2014 3:36 pm

Re: Want to make a chest openable with a key

Post by Prozail »

Ok, i officially give up on this one. There is prob. no way to remove the native click-event. I solved the lock-model disappearing (if you grab doridion's code, you can remove the lock-component entirely as it never fires anyway, just hardcode the name of the key further down). You still unlock, and zoom in on the chest in one go, and you can still unlock with lockpicks..

I'll let someone else find a solution to this one as it is driving me nuts ;) Fix one problem and you trigger another.
User avatar
JKos
Posts: 464
Joined: Wed Sep 12, 2012 10:03 pm
Location: Finland
Contact:

Re: Want to make a chest openable with a key

Post by JKos »

Here is my last effort, added the lock model and sounds. Based on Doridion's example, thanks.
(I guess we can't do anything about the lock picks)

Code: Select all

defineObject{
   name = "chest_padlocked",
   baseObject = "chest",
   components = {
		{
			class = "Lock",
			openedBy = nil,
		},
	  	{
			class = 'Model',
			name = 'lockModel',
			model='assets/models/env/treasure_chest_lock.fbx',
			offset = vec(0,0.41,0.55)
		},
		{
			class = "Clickable",
			maxDistance = 1,
			onClick = function(self)
				local used_item = getMouseItem()
				if not self.go.chest:getLocked() then return false end
				if used_item == nil then return false end
				if not used_item.go.item:hasTrait("key") then
					hudPrint("You must open the chest with a key")
					return false
				end
				if self.go.lock:getOpenedBy() == used_item.go.name then
					self.go.chest:setLocked(false)
					setMouseItem(nil)
					self.go.lockModel:disable()
					hudPrint("Chest is unlocked")
					playSound("key_lock")
					return true
				else
					hudPrint("Not the right key")
					playSound("key_lock_faint")
					return false
				end		
			end
      },
   },
}
- LoG Framework 2http://sites.google.com/site/jkoslog2 Define hooks in runtime by entity.name or entity.id + multiple hooks support.
- cloneObject viewtopic.php?f=22&t=8450
User avatar
JKos
Posts: 464
Joined: Wed Sep 12, 2012 10:03 pm
Location: Finland
Contact:

Re: Want to make a chest openable with a key

Post by JKos »

I got an idea when I was walking our dogs, and it worked :) Here is a workaround for lock picks.

Code: Select all

defineObject{
   name = "chest_padlocked",
   baseObject = "chest",
   components = {
		{
			class = "Lock",
			openedBy = nil,
		},
	  	{
			class = 'Model',
			name = 'lockModel',
			model='assets/models/env/treasure_chest_lock.fbx',
			offset = vec(0,0.41,0.55)
		},
		{
      class = "Clickable",
      maxDistance = 1,
      onClick = function(self)
        local used_item = getMouseItem()
        if not self.go.chest:getLocked() then return false end
		if used_item == nil then return false end
		if used_item.go.name == 'lock_pick' then 
			local stack = used_item:getStackSize()
			setMouseItem(nil)
			playSound("lock_incorrect")
			hudPrint("The lock pick slips out from your hand. This lock is impossible to pick.")
			spawn('lock_pick',party.level,party.x,party.y,party.elevation)
				.item:setStackSize(stack)
			return false
		end
		if not used_item.go.item:hasTrait("key") then
		   hudPrint("You must open the chest with a key.")
		   return false
		end
	    if self.go.lock:getOpenedBy() == used_item.go.name then
		  self.go.chest:setLocked(false)
		  setMouseItem(nil)
		  self.go.lockModel:disable()
		  playSound("key_lock")
		  return true
	    else
		  hudPrint("Not the right key.")
		  playSound("key_lock_faint")
		  return false
	   end		
      end
      },
   },
}
Edit: fixed stack problem with lock picks.
- LoG Framework 2http://sites.google.com/site/jkoslog2 Define hooks in runtime by entity.name or entity.id + multiple hooks support.
- cloneObject viewtopic.php?f=22&t=8450
Post Reply