bug script destroy/respawn

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
bongobeat
Posts: 1076
Joined: Thu May 16, 2013 5:58 pm
Location: France

bug script destroy/respawn

Post by bongobeat »

Please is there an experienced scripter who can tell me if there is something in this script, that can cause a crash in the game?

it's a script used to recharge a gun, when placing it in an alcove, and "actionning" the script with a button.
in fact, it destroy the empty gun in the alcove, then it destroy the alcove
it respawn a new alcove, with a new gun.

sometimes, when actionning the buton after having placed a empty gun in the alcove, the game crash.
SpoilerShow

Code: Select all

function checkAlcoveForItems()
local quantity = false
-- Offer
local itemName = "empty_uranium_gun"
-- offer alcove
local altarID = ualco01
-- checks if the offer alcove has an item
if altarID:getItemCount() == 0 then
hudPrint("First you have to place the empty neutron cannon into the alcove!")
quantity = false
elseif altarID:getItemCount() > 1 then
hudPrint("The recharger is overloaded, please use only one item!")
quantity = false
else
-- provide flag that it is okay to proceed with transformation
quantity = true
end

-- checks if the offer is equal to the item you want
if quantity == true then
for i in altarID:containedItems() do
if i.name == itemName then
hudPrint("The recharge was successful!")
removeItem2()
spawn("solaris_prison_alcove", 21, 29, 20, 0, "ualco01")
playSound("laser")
ualco01:addItem(spawn("uranium_gun"))
else
hudPrint("Place the empty neutron cannon in the alcove.")
end
end
end

function removeItem2()
 if findEntity("ualco01") ~= nil then
     for i in ualco01:containedItems() do
         i:destroy()
     end
     ualco01:destroy()
end
end
end
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: bug script destroy/respawn

Post by minmay »

The crash occurs because you attempt to call removeItem2() before defining it. Just move the definition of removeItem2() outside of checkAlcoveForItems(), like this:
SpoilerShow

Code: Select all

function checkAlcoveForItems()
  local quantity = false
  -- Offer
  local itemName = "empty_uranium_gun"
  -- offer alcove
  local altarID = ualco01
  -- checks if the offer alcove has an item
  if altarID:getItemCount() == 0 then
    hudPrint("First you have to place the empty neutron cannon into the alcove!")
    quantity = false
  elseif altarID:getItemCount() > 1 then
    hudPrint("The recharger is overloaded, please use only one item!")
    quantity = false
  else
    -- provide flag that it is okay to proceed with transformation
    quantity = true
  end

  -- checks if the offer is equal to the item you want
  if quantity == true then
    for i in altarID:containedItems() do
      if i.name == itemName then
        hudPrint("The recharge was successful!")
        removeItem2()
        spawn("solaris_prison_alcove", 21, 29, 20, 0, "ualco01")
        playSound("laser")
        ualco01:addItem(spawn("uranium_gun"))
      else
        hudPrint("Place the empty neutron cannon in the alcove.")
      end
    end
  end
end

function removeItem2()
  if findEntity("ualco01") ~= nil then
    for i in ualco01:containedItems() do
      i:destroy()
    end
    ualco01:destroy()
  end
end
This will define it when the dungeon starts, since script entity code is executed immediately.
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.
bongobeat
Posts: 1076
Joined: Thu May 16, 2013 5:58 pm
Location: France

Re: bug script destroy/respawn

Post by bongobeat »

thanks for help! :)
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
Post Reply