Help: levers that move another levers.

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
TheLastOrder
Posts: 104
Joined: Wed Oct 17, 2012 1:56 am

Help: levers that move another levers.

Post by TheLastOrder »

Well, after almost one hour trying to find a solution, even using counters and pressure plates, zero results.

I'm trying to make a copy of the Keelbreach southern puzzle where you have lots of levers, and each lever moves the 2 nearest ones. I'm tyring to use the connectors, but it seems that another lever can't be referred for this.
I'm quite desesperated and I can promise I searched in the forum for some reference to this kind of puzzle, but I wasn't lucky.

Is there any solution without recurring to scripting? If not... any suggestions about how to script this stuff? I only need at least one line of the code to understand how it works...

Aaaaaaaaaargh!

Thanks in advance!!
Grimfan
Posts: 369
Joined: Wed Jan 02, 2013 7:48 am

Re: Help: levers that move another levers.

Post by Grimfan »

Something like this should work:

Code: Select all

function pullLever()
if lever_1.lever:isActivated() then
lever_2.lever:setState("activated")
lever_3.lever:setState("deactivated")
end
I am a lua noob as well (but trying to get better), so someone better could point out a more efficient way of doing it or point out one of my mistakes. ;)
User avatar
TheLastOrder
Posts: 104
Joined: Wed Oct 17, 2012 1:56 am

Re: Help: levers that move another levers.

Post by TheLastOrder »

Grimfan wrote:Something like this should work:

Code: Select all

function pullLever()
if lever_1.lever:isActivated() then
lever_2.lever:setState("activated")
lever_3.lever:setState("deactivated")
end
I am a lua noob as well (but trying to get better), so someone better could point out a more efficient way of doing it or point out one of my mistakes. ;)
Thanks a lot for the quick answer>

I tried this>

Code: Select all

function pullLever()
if beach_lever_4:isActivated() then
beach_lever_8:setState("Deactivated")
beach_lever_10:setState("Deactivated")
end
if beach_lever_4:isDeactivated() then
beach_lever_8:setState("Activated")
beach_lever_10:setState("Activated")
end
end
I linked the lever to the lua script. When I run the game, it's okey, but as soon as I push that lever, the game crashes> "attempt to call method 'isActivated' (a nil value).

Why?? :( Is that because 'isactivated' is not a valid entry?

Thanks in advance again.
Grimfan
Posts: 369
Joined: Wed Jan 02, 2013 7:48 am

Re: Help: levers that move another levers.

Post by Grimfan »

You need to call the component. In this case the lever (beach_lever_1.lever not just beach_lever_1).

Code: Select all

function pullLever()
 if beach_lever_1.lever:isActivated() then
 beach_lever_2.lever:setState("activated")
 beach_lever_3.lever:setState("activated")
 else
 if beach_lever_1.lever:isDeactivated() then
 beach_lever_2.lever:setState("deactivated")
 beach_lever_3.lever:setState("deactivated")
 end
end
end
This works.

Again, the scripting maestros may have an even better way.
GoldenShadowGS
Posts: 168
Joined: Thu Oct 30, 2014 1:56 am

Re: Help: levers that move another levers.

Post by GoldenShadowGS »

User avatar
TheLastOrder
Posts: 104
Joined: Wed Oct 17, 2012 1:56 am

Re: Help: levers that move another levers.

Post by TheLastOrder »

Great answers from great people!!! Thanks Grimfan and GoldenShadowGS.

It was such an easy problem... I'm sorry, begginer level on scripting still...
About the other one it seems to be risky in order to do a puzzle with a douzen of levers calling each others, and I want to be sure that the puzzle is free of bugs and it has only one solution.

Thanks again, I'll post you back about the results of the puzzle.
User avatar
Prozail
Posts: 158
Joined: Mon Oct 27, 2014 3:36 pm

Re: Help: levers that move another levers.

Post by Prozail »

Say you have levers named "lever_1", "lever_2" etc...
make sure the lever-names are unique to the puzzle, otherwise you risk flipping levers outside of what was intended.
They are placed in order.
Say you want to toggle the levers on both sides of lever_2, that would be lever_1 and lever_3, so 2-1 and 2+1

Code: Select all


function toggleLever(sender)
	-- "sender" here is a bit of magic, it contains the lever calling the function. 
	-- so with a bit of more magic, we can find out what this lever has as its id-number
	local leverid = tonumber(string.match(sender.go.id, "^lever_(.*)"))
	
	--then it's simple to toggle the levers on each side. 
	
	local leftLever = findEntity("lever_"..leverid-1)
	local rightLever = findEntity("lever_"..leverid+1)
	
	-- we need to check if theese lever exists, so... 
	if leftLever then
		--then we toggle it...
		if leftLever.lever:isActivated() then
			leftLever.lever:setState("deactivated")
		else
			leftLever.lever:setState("activated")
		end
	end

	-- and the same for the right one...
	if rightLever then
		--then we toggle it...
		if rightLever.lever:isActivated() then
			rightLever.lever:setState("deactivated")
		else
			rightLever.lever:setState("activated")
		end
	end
	
end
User avatar
TheLastOrder
Posts: 104
Joined: Wed Oct 17, 2012 1:56 am

Re: Help: levers that move another levers.

Post by TheLastOrder »

THAT is EXACTLY what I was looking for. How did I not find it before!!??

I saw this in the Scripting Reference:

LeverComponent:getDisableSelf()
LeverComponent:getSound()
LeverComponent:isActivated()
LeverComponent:isDeactivated()
LeverComponent:setDisableSelf(boolean)
LeverComponent:setSound(string)
LeverComponent:setState(state)
LeverComponent:toggle()
LeverComponent.onActivate(self)
LeverComponent.onDeactivate(self)
LeverComponent.onToggle(self)

So I guess that this is ALL the actions/options you can manage to do puzzles with levers. That is a lot, considering the posibilities.

Guys, you're awesome, I love you in an heterosexual way. I'll give you credits for your support!
User avatar
TheLastOrder
Posts: 104
Joined: Wed Oct 17, 2012 1:56 am

Re: Help: levers that move another levers.

Post by TheLastOrder »

Prozail wrote:Say you have levers named "lever_1", "lever_2" etc...
make sure the lever-names are unique to the puzzle, otherwise you risk flipping levers outside of what was intended.
They are placed in order.
Say you want to toggle the levers on both sides of lever_2, that would be lever_1 and lever_3, so 2-1 and 2+1

Code: Select all


function toggleLever(sender)
	-- "sender" here is a bit of magic, it contains the lever calling the function. 
	-- so with a bit of more magic, we can find out what this lever has as its id-number
	local leverid = tonumber(string.match(sender.go.id, "^lever_(.*)"))
	
	--then it's simple to toggle the levers on each side. 
	
	local leftLever = findEntity("lever_"..leverid-1)
	local rightLever = findEntity("lever_"..leverid+1)
	
	-- we need to check if theese lever exists, so... 
	if leftLever then
		--then we toggle it...
		if leftLever.lever:isActivated() then
			leftLever.lever:setState("deactivated")
		else
			leftLever.lever:setState("activated")
		end
	end

	-- and the same for the right one...
	if rightLever then
		--then we toggle it...
		if rightLever.lever:isActivated() then
			rightLever.lever:setState("deactivated")
		else
			rightLever.lever:setState("activated")
		end
	end
	
end

The 2nd "end" is unnecessary, or it won't work. So this is what I have>

function toggleLever(sender)

local leftLever = findEntity("kb_lever_02")
local rightLever = findEntity("kb_lever_06")

if leftLever.lever:isActivated() then
leftLever.lever:setState("deactivated")
else
leftLever.lever:setState("activated")
end

if rightLever then
if rightLever.lever:isActivated() then
rightLever.lever:setState("deactivated")
else
rightLever.lever:setState("activated")
end
end
end

It's working and it's amazing!!! Thanks again guys!
Post Reply