make an altar respond to multiple power gems

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!
User avatar
FeMaiden
Posts: 290
Joined: Wed Jan 16, 2013 8:16 am

make an altar respond to multiple power gems

Post by FeMaiden »

okay

i'm having trouble here


I have another puzzle where I want a teleporter to activate but only when 4 power gems are placed on an altar.

I have 4 power gems i placed in the editor for testing purposes and i linked the altar to this script

but when i place the 4 gems in the altar, the teleporter won't activate.
however, on a separate occasion, i had accidentally typed activate instead of deactivate in the "else" section and the teleporter activated when i placed 1 gem on the altar
probably because the script checked for a correct solution and didn't find it so it executed the "else" command to activate the teleporter.
what i want is for it to check the altar for the 4 gems and only activate the teleporter when all 4 gems are on the altar.
I don't know what i'm doing wrong here.

Code: Select all

function Win()
local gem1 = false
local gem2 = false
local gem3 = false
local gem4 = false

	for _,itm in altar_1.surface:contents() do        
		if itm.go.name == "power_gem_item_1" then                   
         	gem1 = true
         	break
      	end
   	end

   	for _,itm in altar_1.surface:contents() do
      	if itm.go.name == "power_gem_item_2" then
        	gem2 = true
         	break
      	end
   	end
    for _,itm in altar_1.surface:contents() do
     	if itm.go.name == "power_gem_item_3" then
         	gem3 = true
         	break
      	end
   	end

    for _,itm in altar_1.surface:contents() do
      	if itm.go.name == "power_gem_item_4" then
         	gem4 = true
         	break
      	end
   	end

	

	if gem1 and gem2 and gem3 and gem4 then
      	teleporter_3.controller:activate()   
	else
      	teleporter_3.controller:deactivate()  
   end
end

User avatar
FeMaiden
Posts: 290
Joined: Wed Jan 16, 2013 8:16 am

Re: make an altar respond to multiple power gems

Post by FeMaiden »

welp...i give up on this one....I just modified my story to use a different item on the altars.
it seems odd, that this script works perfectly with any other item but it won't respond to the power gems.

are the power gems unable to be used in a puzzle? do they have special properties attached to them?
I noticed that I can place them on the furnace and the furnace will automatically turn them into an essence without me having to do any kind of scripting whatsoever.
is it this trait which makes them unsuitable for other puzzles?
User avatar
Zo Kath Ra
Posts: 944
Joined: Sat Apr 21, 2012 9:57 am
Location: Germany

Re: make an altar respond to multiple power gems

Post by Zo Kath Ra »

FeMaiden wrote:welp...i give up on this one....I just modified my story to use a different item on the altars.
it seems odd, that this script works perfectly with any other item but it won't respond to the power gems.

are the power gems unable to be used in a puzzle? do they have special properties attached to them?
I noticed that I can place them on the furnace and the furnace will automatically turn them into an essence without me having to do any kind of scripting whatsoever.
is it this trait which makes them unsuitable for other puzzles?
You're confusing "itm.go.name" and "itm.go.id".
itm.go.name = "power_gem_item" (w/o quotes) for all power gems
itm.go.id = unique to each power gem, e.g. "power_gem_item_1", "power_gem_item_2", etc. (also w/o quotes)
User avatar
Isaac
Posts: 3192
Joined: Fri Mar 02, 2012 10:02 pm

Re: make an altar respond to multiple power gems

Post by Isaac »

There are at least two ways to do this, but the script is likely the best choice for a one-time effect.

Connect the altar to this script with two connectors [insert & remove], and it should work.
UPDATED: twice with a suggestion by Zo Kath Ra.

Code: Select all

function gemCheck(self)
	local t = teleporter_3.controller
	local check = 1
		 for _,item in self.go.surface:contents() do      
			if item.go.name == "power_gem_item" and check < 5 then
				check = check +1
			end	
		 end
		 t[iff(check == 5,'activate', 'deactivate')](t)
end
Last edited by Isaac on Sat Oct 24, 2015 10:47 pm, edited 3 times in total.
User avatar
Zo Kath Ra
Posts: 944
Joined: Sat Apr 21, 2012 9:57 am
Location: Germany

Re: make an altar respond to multiple power gems

Post by Zo Kath Ra »

What does this do?

Code: Select all

if item.go.id == string.sub(item.go.id, -#item.go.id)
If I understand the string.sub syntax correctly, it compares the string with itself?
User avatar
Isaac
Posts: 3192
Joined: Fri Mar 02, 2012 10:02 pm

Re: make an altar respond to multiple power gems

Post by Isaac »

Zo Kath Ra wrote:What does this do?

Code: Select all

if item.go.id == string.sub(item.go.id, -#item.go.id)
If I understand the string.sub syntax correctly, it compares the string with itself?
:shock: It's a bug.
It should have read:

Code: Select all

if string.sub(item.go.id, 1,#item.go.id-1) == "power_gem_item_" and check < 5 then 
User avatar
Zo Kath Ra
Posts: 944
Joined: Sat Apr 21, 2012 9:57 am
Location: Germany

Re: make an altar respond to multiple power gems

Post by Zo Kath Ra »

Isaac wrote:
Zo Kath Ra wrote:What does this do?

Code: Select all

if item.go.id == string.sub(item.go.id, -#item.go.id)
If I understand the string.sub syntax correctly, it compares the string with itself?
:shock: It's a bug.
It should have read:

Code: Select all

if string.sub(item.go.id, 1,#item.go.id-1) == "power_gem_item_" and check < 5 then 
How about

Code: Select all

if item.go.name == "power_gem_item" and check < 5 then
This also works if the ID ends with more than one digit, like "power_gem_item_10"
User avatar
Isaac
Posts: 3192
Joined: Fri Mar 02, 2012 10:02 pm

Re: make an altar respond to multiple power gems

Post by Isaac »

Zo Kath Ra wrote:How about

Code: Select all

if item.go.name == "power_gem_item" and check < 5 then
This also works if the ID ends with more than one digit, like "power_gem_item_10"
Considering that it's a also a constant that is what's being searcher for, I'd say it's the best choice. 8-)
(... And strange that it didn't occur to me the first time through.)

*I had originally used the id's on the chance that they were important; rather than match to just any power_gem_item.
User avatar
FeMaiden
Posts: 290
Joined: Wed Jan 16, 2013 8:16 am

Re: make an altar respond to multiple power gems

Post by FeMaiden »

okay.
I think I understood most of what you guys told me.
I appreciate it.

I alreayd modified my story to accomodate a different type of item so i'm just gonna roll forward, I still have 28 more maps to finish...
User avatar
Drakkan
Posts: 1318
Joined: Mon Dec 31, 2012 12:25 am

Re: make an altar respond to multiple power gems

Post by Drakkan »

FeMaiden wrote:okay.
I think I understood most of what you guys told me.
I appreciate it.

I alreayd modified my story to accomodate a different type of item so i'm just gonna roll forward, I still have 28 more maps to finish...
my oppinion - making so huge map without knowing some basics is a bad idea. You either end with many boring levels filled just with monsters, either you will stuck because of this size on many places and you will give up eventually.
Be on your place Id rather concentrate on lets say 6 levels big dungeon and try to bejewel it nicely. Alternatively, you can always expand it.
Breath from the unpromising waters.
Eye of the Atlantis
Post Reply