Thanks knighthawk75, I woke up this morning with the exact same solution in mind. Perhaps it was the subconscious remembering your words.[;)]
I have another question for you.
I currently have 2 functions to delete events, one that I wrote based upon your code: CleanupEvent =
function( eventName )
local e,p = pcall(ScenEdit_GetEvent,eventName);
if(e == true and p ~=nil) then
--remove all the actions connected to this event!
for k,v in pairs(p.actions) do
local aName = ''
if (v.LuaScript ~= nil) then aName = v.LuaScript.Description
elseif (v.ChangeMissionStatus ~= nil) then aName = v.ChangeMissionStatus.Description
elseif (v.NewStatus ~= nil) then aName = v.NewStatus.Description
elseif (v.EndScenario ~= nil) then aName = v.EndScenario.Description
elseif (v.Message ~= nil) then aName = v.Message.Description
elseif (v.Text ~= nil) then aName = v.Text.Description
elseif (v.Points ~= nil) then aName = v.Points.Description
elseif (v.SideID ~= nil) then aName = v.SideID.Description
elseif (v.TeleportInArea ~= nil) then aName = v.TeleportInArea.Description
else aName = nil end
if (aName ~= nil) then
--print('disconnecting action: '..aName..'from event: '..eventName)
ScenEdit_SetEventAction(eventName,{mode='remove',name=aName})
--print('destroying action: '..aName)
ScenEdit_SetAction({mode='remove',name=aName})
end
end
--remove all the triggers connected to this event!
for k,v in pairs(p.triggers) do
local tName = ''
if (v.Points ~= nil) then tName = v.Points.Description
elseif (v.RandomTime) then tName = v.RandomTime.Description
elseif (v.RegularTime) then tName = v.RegularTime.Description
elseif (v.ScenEnded) then tName = v.ScenEnded.Description
elseif (v.ScenLoaded) then tName = v.ScenLoaded.Description
elseif (v.Time) then tName = v.Time.Description
elseif (v.UnitDamaged) then tName = v.UnitDamaged.Description
elseif (v.UnitDestroyed) then tName = v.UnitDestroyed.Description
elseif (v.UnitDetected) then tName = v.UnitDetected.Description
elseif (v.UnitEmissions) then tName = v.UnitEmissions.Description
elseif (v.UnitEntersArea) then tName = v.UnitEntersArea.Description
elseif (v.UnitRemainsInArea) then tName = v.UnitRemainsInArea.Description
elseif (v.UnitBaseStatus) then tName = v.UnitBaseStatus.Description
elseif (v.UnitCargoMoved) then tName = v.UnitCargoMoved.Description
else tName = nil end
if (tName ~= nil) then
--print('disconnecting tigger: '..tName..'from event: '..eventName)
ScenEdit_SetEventTrigger(eventName,{mode='remove',name=tName})
--print('destroying tiggger: '..tName)
ScenEdit_SetTrigger({mode='remove',name=tName})
end
end
--remove all the conditions connected to this event!
for k,v in pairs(p.conditions) do
local cName = ''
if (v.LuaScript~= nil) then cName = v.LuaScript.Description
elseif (v.ScenHasStarted) then cName = v.ScenHasStarted.Description
elseif (v.SidePosture) then cName = v.SidePosture.Description
else cName = nil end
if (cName ~= nil) then
--print('disconnecting condition: '..cName..'from event: '..eventName)
ScenEdit_SetEventCondition(eventName,{mode='remove',name=cName})
--print('destroying condition: '..cName)
ScenEdit_SetCondition({mode='remove',name=cName})
end
end
--remove the event
ScenEdit_SetEvent(eventName, {mode='remove'})
end
end
and two written by a co-worker
RemoveEvent =
function(eventName)
local event = ScenEdit_GetEvent(eventName)
ScenEdit_SetEvent(event.guid, {mode = 'remove'})
end
andLeMay.Event.RemoveEventAndTCAs =
function(eventName)
local event = ScenEdit_GetEvent(eventName)
local eventTriggers = event.triggers
local eventConditions = event.conditions
local eventActions = event.actions
ScenEdit_SetEvent(event.guid, {mode = 'remove'})
--The first loop gets all the TCAs, the second loop gets the TCA details which is where the id is.
for k, v in pairs(eventTriggers) do
for key, value in pairs(v) do
if type(value) == "table" then
local id = value.ID
ScenEdit_SetTrigger({description = id, mode = 'remove'})
end
end
end
for k, v in pairs(eventConditions) do
for key, value in pairs(v) do
if type(value) == "table" then
local id = value.ID
ScenEdit_SetCondition({description = id, mode = 'remove'})
end
end
end
for k, v in pairs(eventActions) do
for key, value in pairs(v) do
if type(value) == "table" then
local id = value.ID
ScenEdit_SetAction({description = id, mode = 'remove'})
end
end
end
end
The co-worker obviously prefers his function, and with implementing your suggestion(above) I will be consolidating/removing some delete code (most likely remap all the functions to a single function that implements your suggestion.)
Talk to me about what is wrong with the implementations.
My understanding is as follows:
The RemoveEvent only deletes the event and can leave orphaned triggers, conditions, and actions. I got that. This is a bad thing.
The RemoveEventAndTCAs functions will delete the event and all TCAs connected to it in any way. This will include TCAs that may be shared with other events, which can cause them to error and abort your scripts. Clearly a bad thing.
The CleanupEvent function deletes the event and any TCAs that are connected to them AND share the same name. If the TCA that has the same name as the event happens to be used by another event, then that event will error out if called/triggered.
It seems to me that the whole area of events, trigers, conditions, and actions,is fairly complex and while it provides a great deal of flexibility, great care must be taken to keep these all straight and make sure that deletion happens correctly.
Aside from your suggestion above, which involves a recurring EventCleanup event and a global list of events for deletion, what is the best way to allow easy and safe cleanup of events (and their associared TCAs) allowing for the user to share TCAs?
I am thinking of startign with the CleanupEvent function, alter the parameter to a table and allow the user to specify the level and type of delete to perform.
{ name='eventName', delete=[eventOnly|allActions|namedActions|allTriggers|namedTriggers|allConditions|namedConditions|allTCAs|namedTCAs] }
Then based upon the delete parameter place the guid of the element on perform the appropriate delete list (event, trigger, condition, action), then every yea-often (perhaps 15 minutes) delete all the items on the delete lists..
Please let me know what you think of the plan. Thanks.