Page 1 of 1

bug script destroy/respawn

Posted: Fri Jan 02, 2015 8:11 pm
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

Re: bug script destroy/respawn

Posted: Fri Jan 02, 2015 8:38 pm
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.

Re: bug script destroy/respawn

Posted: Sat Jan 03, 2015 12:26 pm
by bongobeat
thanks for help! :)