Page 2 of 3

Re: Want to make a chest openable with a key

Posted: Mon Nov 17, 2014 3:21 pm
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.

Re: Want to make a chest openable with a key

Posted: Mon Nov 17, 2014 3:51 pm
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.

Re: Want to make a chest openable with a key

Posted: Mon Nov 17, 2014 4:00 pm
by Doridion
I even done this system with my unlockable doors, you can pick-up the code ;)

Re: Want to make a chest openable with a key

Posted: Mon Nov 17, 2014 4:09 pm
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.

Re: Want to make a chest openable with a key

Posted: Mon Nov 17, 2014 6:32 pm
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 )

Re: Want to make a chest openable with a key

Posted: Mon Nov 17, 2014 6:43 pm
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.

Re: Want to make a chest openable with a key

Posted: Mon Nov 17, 2014 7:06 pm
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)
},

Re: Want to make a chest openable with a key

Posted: Mon Nov 17, 2014 7:23 pm
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.

Re: Want to make a chest openable with a key

Posted: Mon Nov 17, 2014 8:06 pm
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
      },
   },
}

Re: Want to make a chest openable with a key

Posted: Mon Nov 17, 2014 9:17 pm
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.