[Script] Altar that bestows a trait to items placed on it.
Posted: Fri Dec 05, 2014 4:42 am
I wanted to place any ordinary item on the altar along with a single "water_gem" (made from a renamed blue_gem) This will transmute the item into one with the aquatic trait so it can work underwater. If you place the aquatic item back on the altar, you undo the transmutation and get your water gem back. I had to remove stackable items from the list of valid items since you can lose your water_gems if the entire stack isn't the same trait.
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.
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