-- object_singleton - Make an object into a singleton -- (system_utils_a component) -- Template attachment: any -- Created by: Patrick Ferland -- Created on: 06/23/2008 -- Revision History -- 06/23/2008 P.Ferland - New script -- 07/02/2008 P.Ferland - Destroyed() no longer removes singleton reference if self is not world singleton --[[ How to use the Singleton script 1. Attach object_singleton[11661:4] to the desired template 2. Attach world_singleton[11661:5] to the world object 3. Object creation is now only allowed in a singleton fashion NOTE: The world must be saved (manually, by a world builder) whenever a singleton is added or removed, if the reference must be maintained -- persistent world properties cannot be saved via script! (Not that it doesn't try.) ]]-- -- Constants -- no constants -- Script Properties -- no properties -- Commands -- no commands -- Triggers Trigger attach() Debug('Created singleton of ' .. self.type .. ' : ' .. self.id) local w = GetWorld() if (w.singleton_tracker ~= nil) then local singleton = w.singleton_tracker[self.type] if (singleton ~= nil) then if (GetObjectById(singleton.id).id ~= self.id) then Debug('Existing ' .. self.type .. ' singleton : ' .. singleton.id) DestroyObject(self) return end end w.singleton_tracker[self.type] = self else Debug('World singleton tracker could not be accessed. Allowing default creation.') end SaveToDb(w) end Trigger destroyed(place_id) Debug('Destroying singleton of ' .. self.type .. ' : ' .. self.id) local w = GetWorld() if (w.singleton_tracker ~= nil) then local singleton = w.singleton_tracker[self.type] if (singleton ~= nil) then if (singleton.id == self.id) then w.singleton_tracker[self.type] = nil end end else Debug('World singleton tracker could not be accessed. Cannot remove singleton reference.') end SaveToDb(w) end -- Local functions -- no functions