Stack Overflow

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
GoldenShadowGS
Posts: 168
Joined: Thu Oct 30, 2014 1:56 am

Stack Overflow

Post by GoldenShadowGS »

Code: Select all

function levertoggler()
	if lowertrigger1.floortrigger:isActivated() then
		upper_lever1.lever:toggle()
		lower_lever2.lever:toggle()
	end
	
	if lowertrigger2.floortrigger:isActivated() then
		lower_lever1.lever:toggle()
		lower_lever3.lever:toggle()
		upper_lever2.lever:toggle()
	end
	
	if lowertrigger3.floortrigger:isActivated() then
		lower_lever2.lever:toggle()
		upper_lever3.lever:toggle()
	end
	
	if uppertrigger1.floortrigger:isActivated() then
		lower_lever1.lever:toggle()
		upper_lever2.lever:toggle()
	end
	
	if uppertrigger2.floortrigger:isActivated() then
		lower_lever2.lever:toggle()
		upper_lever1.lever:toggle()
		upper_lever3.lever:toggle()
	end
	
	if uppertrigger3.floortrigger:isActivated() then
		lower_lever3.lever:toggle()
		upper_lever2.lever:toggle()
	end
end
I have 6 levers in a 2x3 arrangement. When I pull one lever, I want the adjacent ones to toggle. I thought I was being clever with this script by putting an invisible floortrigger in front of each lever. but its giving me a stack overflow error. I think its getting itself into some kind of infinite loop anyway.

All 6 levers connect to this one function.
GoldenShadowGS
Posts: 168
Joined: Thu Oct 30, 2014 1:56 am

Re: Stack Overflow

Post by GoldenShadowGS »

I figured it out. I just needed to make each lever connect to a unique function. Works perfect now. Can someone explain why the first case gave me a problem?

Code: Select all

function levertog1()
	if lowertrigger1.floortrigger:isActivated() then
		upper_lever1.lever:toggle()
		lower_lever2.lever:toggle()
	end
end
function levertog2()
	if lowertrigger2.floortrigger:isActivated() then
		lower_lever1.lever:toggle()
		lower_lever3.lever:toggle()
		upper_lever2.lever:toggle()
	end
end
function levertog3()
	if lowertrigger3.floortrigger:isActivated() then
		lower_lever2.lever:toggle()
		upper_lever3.lever:toggle()
	end
end
function levertog4()
	if uppertrigger1.floortrigger:isActivated() then
		lower_lever1.lever:toggle()
		upper_lever2.lever:toggle()
	end
end
function levertog5()
	if uppertrigger2.floortrigger:isActivated() then
		lower_lever2.lever:toggle()
		upper_lever1.lever:toggle()
		upper_lever3.lever:toggle()
	end
end
function levertog6()
	if uppertrigger3.floortrigger:isActivated() then
		lower_lever3.lever:toggle()
		upper_lever2.lever:toggle()
	end
end
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: Stack Overflow

Post by minmay »

Because when you toggle a lever, it triggers its connectors just like it was pulled. Let's say you pulled the lever corresponding to lowertrigger1: your script sees that you're standing on the corresponding floor trigger, so it toggles upper_lever1. upper_level1 is connected to the levertoggler() function, so it gets called again. It sees that lowertrigger1 is activated, so it toggles upper_lever1. This calls the function again, which sees that lowertrigger1 is activated, which toggles upper_lever1, which calls the function again, which sees that lowertrigger1 is activated, which calls the function again...
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
GoldenShadowGS
Posts: 168
Joined: Thu Oct 30, 2014 1:56 am

Re: Stack Overflow

Post by GoldenShadowGS »

Thanks. Now I see where I went wrong.
Post Reply