diff --git a/extensions/ObjectVariables.ext b/extensions/ObjectVariables.ext index f9107a1..99fac71 100644 --- a/extensions/ObjectVariables.ext +++ b/extensions/ObjectVariables.ext @@ -6,48 +6,62 @@ -- filename.ext files are loaded before normal .lua files +-- +-- This extension allows saving data to specific object for it's lifetime in current runtime session +-- Supports Map, Player, Creature, GameObject +-- +-- SetData sets a value +-- obj:SetData(key, val) +-- +-- GetData gets the data table or a specific value by key from it +-- local tbl = obj:GetData() +-- local val = obj:GetData(key) +-- + local variableStores = { + Map = {}, Player = {}, Creature = {}, GameObject = {}, } -local function DestroyData(event, obj) - if event == 18 or event == 17 then - local map = obj:GetMapId() - local inst = obj:GetInstanceId() - for k,v in pairs(variableStores) do - if inst == 0 then - v[map] = nil - else - local mapdata = v[map] - if mapdata then - mapdata[inst] = nil - end - end +local function DestroyMapData(event, obj) + local map = obj:GetMapId() + local inst = obj:GetInstanceId() + for k,v in pairs(variableStores) do + local mapdata = v[map] + if mapdata then + mapdata[inst] = nil end - return end - local mapdata = variableStores[obj:GetObjectType()][obj:GetMapId()] +end + +local function DestroyObjData(event, obj) + local map = self:GetMapId() + local inst = self:GetInstanceId() + local otype = self:GetObjectType() + local guid = otype == "Map" and 1 or self:GetGUIDLow() + local mapdata = variableStores[otype][map] if mapdata then - local instancedata = mapdata[obj:GetInstanceId()] + local instancedata = mapdata[inst] if instancedata then - instancedata[obj:GetGUIDLow()] = nil + instancedata[guid] = nil end end end local function GetData(self, field) - local varStore = variableStores[self:GetObjectType()] - local guid = self:GetGUIDLow() local map = self:GetMapId() local inst = self:GetInstanceId() + local otype = self:GetObjectType() + local guid = otype == "Map" and 1 or self:GetGUIDLow() + local varStore = variableStores[otype] varStore[map] = varStore[map] or {} varStore[map][inst] = varStore[map][inst] or {} varStore[map][inst][guid] = varStore[map][inst][guid] or {} - if field then + if field ~= nil then return varStore[map][inst][guid][field] else return varStore[map][inst][guid] @@ -55,10 +69,11 @@ local function GetData(self, field) end local function SetData(self, field, val) - local varStore = variableStores[self:GetObjectType()] - local guid = self:GetGUIDLow() local map = self:GetMapId() local inst = self:GetInstanceId() + local otype = self:GetObjectType() + local guid = otype == "Map" and 1 or self:GetGUIDLow() + local varStore = variableStores[otype] varStore[map] = varStore[map] or {} varStore[map][inst] = varStore[map][inst] or {} @@ -72,9 +87,8 @@ for k,v in pairs(variableStores) do _G[k].SetData = SetData end -RegisterPlayerEvent(3, DestroyData) -- login -RegisterPlayerEvent(4, DestroyData) -- logout -RegisterServerEvent(31, DestroyData) -- creature delete -RegisterServerEvent(32, DestroyData) -- gameobject delete -RegisterServerEvent(17, DestroyData) -- map create -RegisterServerEvent(18, DestroyData) -- map destroy +RegisterPlayerEvent(4, DestroyObjData) -- logout +RegisterServerEvent(31, DestroyObjData) -- creature delete +RegisterServerEvent(32, DestroyObjData) -- gameobject delete +RegisterServerEvent(17, DestroyMapData) -- map create +RegisterServerEvent(18, DestroyMapData) -- map destroy