Certain items to trigger pressure plates?

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
zimberzimber
Posts: 432
Joined: Fri Feb 08, 2013 8:06 pm

Certain items to trigger pressure plates?

Post by zimberzimber » Tue May 05, 2015 10:26 am

I am trying to code a puzzle where you need to place a specific item on a pressure plate to complete a puzzle, but I have no idea how to make it work.
I tried to code something using a script entity, but failed.
I also searched for a way for the script to check if the item is present in that tile, but failed, again.

Could anyone please help me? :D
My asset pack [v1.10]
Features a bit of everything! :D

alois
Posts: 103
Joined: Mon Feb 18, 2013 7:29 am

Re: Certain items to trigger pressure plates?

Post by alois » Tue May 05, 2015 12:14 pm

1) The pressure plate has to be triggered by item (not party, not monster, not digging), and must not "disable self".
2) Let's say the item is a "blue_gem" (as an example).
3) In a script_entity put the following:

Code: Select all

function isThere(self)
	local isOk = false
	for ent in self.go.map:entitiesAt(self.go.x,self.go.y) do
		if (ent.name == "blue_gem") then
			isOk = true
		end
	end
	if (isOk) then
		-- do whatever you want
		self:disable() -- one shot only if the item is ok.
	end
end
4) Connect the pressure plate to the "isThere" function of the script.

Alois :)

User avatar
zimberzimber
Posts: 432
Joined: Fri Feb 08, 2013 8:06 pm

Re: Certain items to trigger pressure plates?

Post by zimberzimber » Tue May 05, 2015 3:14 pm

Thank you so much!
After some modifications I managed to get it to work with 3 pressure plates, but I have stumbled upon a slight problem which might break the puzzle for those unwary of the code.

The pressure plate doesn't trigger the script if it already active. (ex: Already has an item on it (placed by player)
So if a player accidentally throws a rock on a plate, and places the right item, the script will not trigger.

Anyway to solve this?
My asset pack [v1.10]
Features a bit of everything! :D

alois
Posts: 103
Joined: Mon Feb 18, 2013 7:29 am

Re: Certain items to trigger pressure plates?

Post by alois » Tue May 05, 2015 3:52 pm

The only way I can think of resolving it is to create a new pressure place with a surface component (so that it works like it were an alcove).
In items.lua (in your scripts directory), add the following

Code: Select all

defineObject{
	name = "pressure_plate_surface",
	baseObject = "base_pressure_plate",
	components = {
		{
			class = "Model",
			model = "assets/models/env/dungeon_pressure_plate.fbx",
			staticShadow = true,
		},
		{
			class = "Animation",
			animations = {
				activate = "assets/animations/env/dungeon_pressure_plate_down.fbx",
				deactivate = "assets/animations/env/dungeon_pressure_plate_up.fbx",	
			},
		},
		{
			class = "Occluder",
			model = "assets/models/env/dungeon_floor_01_occluder.fbx",
		},
		{
			class = "Surface",
			offset = vec(0, 0.1, 0),
			size = vec(2, 2),
			--debugDraw = true,
		},
		{
			class = "Clickable",
			offset = vec(0, 0.0, 0),
			size = vec(2, 0.0, 2),
			maxDistance = 1,
			--debugDraw = true,
		},
	},
}
Then add a "pressure_plate_surface" to your dungeon and connect the two "onInsertItem" (drop) and "onRemoveItem" (take) to two functions in a script:

Code: Select all

function drop(self)
	local object = false
	for _,ent in self.go.surface:contents() do
		object = true
		if (ent.go.name == "blue_gem") then
			-- do what you want
		end
	end
	if (object) then
		self.go.animation:play("activate") -- lower the plate
		playSound("pressure_plate_pressed")
	end
end
and

Code: Select all

function take(self)
	local none = true
	for _,ent in self.go.surface:contents() do
		none = false
	end
	if (none) then
		self.go.animation:play("deactivate") -- raise the plate
		playSound("pressure_plate_released")
	end
end
I'm sure there is a faster way to do this, but I'm out of ideas... :)

Alois :)

User avatar
zimberzimber
Posts: 432
Joined: Fri Feb 08, 2013 8:06 pm

Re: Certain items to trigger pressure plates?

Post by zimberzimber » Tue May 05, 2015 7:19 pm

I think I'll simply add a small piece of instructions concerning the inconvenience.
Thank you anyways! :D
My asset pack [v1.10]
Features a bit of everything! :D

alois
Posts: 103
Joined: Mon Feb 18, 2013 7:29 am

Re: Certain items to trigger pressure plates?

Post by alois » Tue May 05, 2015 7:47 pm

zimberzimber wrote:I think I'll simply add a small piece of instructions concerning the inconvenience.
Thank you anyways! :D
I agree... :)

Alois :)

Post Reply