Alcove puzzle Problem

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
Xardas
Posts: 83
Joined: Fri Jul 28, 2017 12:30 am

Alcove puzzle Problem

Post by Xardas »

Hi Folks,
I´m currently getting started with modding in Grimrock 2.
For my new alcove puzzle, i want the Player to put 3 items from a specific type (here consumable) in an alcove. When he does that i want the items in the alcove to be destroyed and to spawn an item behind him. I also want the Event to trigger only once.

I used the following Code:

Code: Select all

function checkitems(alcove)

local count=0

for v,i in alcove:contents() do
if i:hasTrait ("consumable") then
count=count+1
end
end

if count==3 then
spawn("altar", 1, 26, 4, 0, 0).surface:addItem(spawn("diviner_cloak").item)
playSound("teleport")
hudPrint("Thank you")
party.party:shakeCamera(1,1)
end
if findEntity("dungeon_alcove_10") ~= nil then
for i, ent in dungeon_alcove_10.surface:contents() do
if ent:hasTrait ("consumable") then
ent.go:destroy()
end
end
end 
end
end
As a newbie in Lua Code i don´t really understand why it won´t work properly. It doesn´t destroy all items in the alcove. Also i have no idea how i should make my puzzle to trigger only once. I tried it with a Counter, but for some reason it still triggers multiple times. Any help for my Problem is appreciated :D .

Greetings, Phil
User avatar
Isaac
Posts: 3192
Joined: Fri Mar 02, 2012 10:02 pm

Re: Alcove puzzle Problem

Post by Isaac »

This (thrice :o ) updated function records the first three suitable items found in the alcove. When there are three, it destroys each one, and ignores further use.

The last update added some optional particle effects.

Place the script_entity in the location (and face direction) that you want the altar to appear.

Code: Select all

function checkitems(alcove)
      local edibles = {}               --records detected consumables
      for v,i in alcove:contents() do
         if i:hasTrait ("consumable") then
            edibles[#edibles+1] = i   
         end
      end
      if #edibles>=3 then 
         local altar = self.go:spawn("altar")
            altar.surface:addItem(spawn("diviner_cloak").item)
            
            ---[[ optionally destroy the altar after emptied
               altar.surface:addConnector("onRemoveItem", self.go.id, "cleanUp")         
            --]]
            
            ---[[ optional particle effects
            alcove.go:createComponent("Particle")
            alcove.go.particle:setParticleSystem("teleporter")
            alcove.go.particle:setDestroySelf(true)
            alcove.go.particle:fadeOut(.8)
            alcove.go.particle:setOffset(vec(0,0,1.5))
            altar:createComponent("Particle")
            altar.particle:setParticleSystem("death")
         --]]   
         
         playSound("teleport")
         hudPrint("Thank you")
         party.party:shakeCamera(.5,.5)   
         
         for x = 1, 3 do              --destroys three food items
            edibles[x].go:destroy()
         end
         
         for x = 1, alcove:getConnectorCount() do  --removes the alcove's connectors
            alcove:removeConnector(alcove:getConnector(x))
         end         
      end   
end

function cleanUp(altar)              --removes altar once emptied of the last item  
   if altar.go.surface:count() < 1 then
       
      ---[[ optional particle effects
         altar.go:createComponent("Particle")
         altar.go.particle:setParticleSystem("teleporter")
         altar.go.particle:setDestroySelf(true)
         altar.go.particle:fadeOut(.7)
       --]]
      
      party.party:shakeCamera(.5,.5)
      playSound("teleport")
      delayedCall(self.go.id, .5, "eraseAltar", altar.go.id)
   end
end

function eraseAltar(altarId)        --destroys altar when called from cleanUp
   findEntity(altarId):destroy()
end
*Updated
Post Reply