Page 1 of 1

Destroying and then respawning a key puzzle item

Posted: Sat Oct 06, 2012 5:44 am
by akroma222
Hi folks,
I am trying to write some code to make sure a puzzle doesn't become impossible to complete.
I have a bunch of pits with alcoves all around that require gems. Problem is, if you place a gem in an alcove then fall into a pit after having the access to the alcove removed, when you walk back around to the puzzle you are cut off from the gem (i.e., puzzle cant be completed)
So I figured that a pressure plate leading up to the puzzle could trigger a check on certain alcoves to see whether a certain gem is present.
If it is, it is destroyed and respawned at the beginning of the puzzle - so no matter if you fall down a pit (or teleported away) - you are never cut off from the puzzle's key item.

Here is the code I tried:
SpoilerShow
function gemCheckRed()
local itemFound = false
for i in alcoveGemSecret1:containedItems() do
if i .name == "red_gem" then
itemFound = true
end
end
if itemFound then
red_gem:destroy()
spawn(("red_gem"),6,21,8,0)
end
end
However, nothing happens :?:
The pressure plate (hidden) is connected to the script

Ultimately, I will need about 4 checks to take place once the party walks over the pressure plate...
Helpful nudges or directions would be greatly appreciated!!
Thankyou :)

Re: Destroying and then respawning a key puzzle item

Posted: Sat Oct 06, 2012 6:30 am
by Lmaoboat
I'm a bit tiried to open up the editor, but have you tried something to the effect of
redgem = findEntity(i.id)
redgem:destroy()

Re: Destroying and then respawning a key puzzle item

Posted: Sat Oct 06, 2012 12:38 pm
by JohnWordsworth
I think you are confusing the property 'name', which tells you what type of object you have, with the property ID, which describes a single instance of that item. For instance, if you have 3 red gems, they will all have name=red_gem, but each will have a different ID (reg_gem_1, reg_gem_2, reg_gem_3).

So, you could update your script as so...
SpoilerShow

Code: Select all

function gemCheckRed()
  local itemFound = nil

  for i in alcoveGemSecret1:containedItems() do
    if i .name == "red_gem" then
      itemFound = i.id
    end
  end

  if itemFound ~= nil then
    gem = findEntity(itemFound)
    gem:destroy()
    spawn(("red_gem"),6,21,8,0)
  end
end
You could optimise it a bit further by just keeping hold of your reference to the gem as you find it...
SpoilerShow

Code: Select all

function gemCheckRed()
  local itemFound = nil

  for i in alcoveGemSecret1:containedItems() do
    if i .name == "red_gem" then
      itemFound = i
    end
  end

  if itemFound ~= nil then
    itemFound:destroy()
    spawn(("red_gem"),6,21,8,0)
  end
end
You could probably even just move the item to save on a bit more code (I assume you don't just destroy the object the loop because the iteration would get confused, but if you just move it then it should be ok). This is more of a guess - I don't know if you can move objects that are in a container this way and haven't tested it...
SpoilerShow

Code: Select all

function gemCheckRed()
  local itemFound = nil

  for i in alcoveGemSecret1:containedItems() do
    if i .name == "red_gem" then
      i.x = 21
      i.y = 8
      return
    end
  end

Re: Destroying and then respawning a key puzzle item

Posted: Sat Oct 06, 2012 1:13 pm
by akroma222
Thank you John! I tired each of your suggested codes, however, none have worked as of yet.
The second gives me an error- (attempt to call method 'destroy' (a nil value))
The last gives me an error about trying to change 'read only' info...

But I will try and play around with these a bit more, in some instances the gem did indeed dissappear, but just did not reappear! lol .... Thank you for replying!...

Re: Destroying and then respawning a key puzzle item

Posted: Sat Oct 06, 2012 2:56 pm
by akroma222
OK! My bad, the first code works a treat :D Thanks heaps John, this little conundrum was curtailing the deviousness of a couple of my puzzles.... Thank you!!