How to undo an action from a customization

Sample code on how to get undo history and send an UndoT

Creating an UndoT

ScenarioUndoT undoT = new ScenarioUndoT(a_sd.Scenario.Id);

ScenarioUndoT.Node undoNode = new ScenarioUndoT.Node(undoId, transmissionIdx, false);

In order to undo an action, we need the following information:

1. Scenario Id.

2. UndoSet ID

3. Transmission Index


Scenario Id

ScenarioId can generally be retrieved from a ScenarioDetail reference however it can be retrieved by searching through each scenario:

using (SystemController.Sys.ScenariosLock.TryEnterRead(out sm, 100))
//Enumerate through sm, each being a Scenario


Retrieving the UndoSet ID and TransmissionIdx by TransmissionNbr

//m_transmissionNbr in this example was retrieved by listening to transmission processed during SimulationInitialization. 
//However as long as the transmissionNbr is known this method will work.
Id undoId = Id.MaxValue;
int transmissionIdx = 0;
//Lock and get the scenario undo sets
using (a_sd.Scenario.UndoSetsLock.TryEnterRead(out Scenario.UndoSets undoSets, AutoExiter.THREAD_TRY_WAIT_MS))
for (int i = 0; i < undoSets.Count; i++)
Scenario.UndoSet undoSet = undoSets[i];
for (int j = 0; j < undoSet.Count; j++)
Scenario.TransmissionJar jar = undoSet[j];
if (jar.TransmissionInfo.TransmissionNbr == m_transmissionNbr) //m_transmissionNbr
undoId = undoSet.m_undoNbr;
transmissionIdx = j;

Transmissions cannot be undone until they have been processed. This means that the transmission cannot be undone as part of am EndOfSimulationCustomization because during EndOfSimulationCleanup, the transmission has not yet been added to the undo sets.


Next Steps
Explore the following articles to learn all there is to know about customization projects in PlanetTogether:

Getting started with customizations

Advanced topics