I found all of the variables necessary because if I altered the contents of the surface during the for loop, bad things happen. It is required to use a shutdown variable, "shutdownaltar" so the function can not be run in parallel.This was to prevent the spawned objects instantly removing themselves.
Code: Select all
shutdownaltar = false
function convertAquatic()
if shutdownaltar == false then
shutdownaltar = true
local s = water_altar.surface
local watergem = 0
local killgem = false
local aquatic = false
local stackable = false
local addgem = false
local count = s:count()
for n,e in s:contents() do
if e.go.name == "water_gem" then
watergem = watergem + 1
end
if e.go.item:hasTrait("aquatic") then
aquatic = true
end
if e.go.item:getStackable() then
stackable = true
end
end
for n,e in s:contents() do
if aquatic == true then
if count == 1 then
e.go.item:removeTrait("aquatic")
local un = e.go.item:getUiName()
local oldui = string.gsub(un, "Aquatic ", "")
e.go.item:setUiName(oldui)
addgem = true
water_imbue_effect.controller:activate()
end
else
if count == 2 then
if watergem == 1 and stackable == false then
e.go.item:addTrait("aquatic")
local ui = e.go.item:getUiName()
e.go.item:setUiName("Aquatic "..ui)
water_imbue_effect.controller:activate()
killgem = true
end
end
end
end
if addgem == true then
water_altar.surface:addItem(spawn("water_gem").item)
end
if killgem == true then
for n,e in s:contents() do
if e.go.name == "water_gem" then
e.go:destroy()
end
end
end
shutdownaltar = false
end
end