When writing Discrete Event Simulations, executing events often create subsequent events in a chain of events that mimic behavior, then events that are fired are generally destroyed upon firing. I have done this for many years across many simulations in several languages.
As I am writing events in Command, I am finding that I am writing chains of events.
EventA on unit X, is followed by EventB on unit X, then EventC on unit X.
The same series happens on several other units, as well.
Other series of events happen on the units as well.
As there are variable times between the events, and these times can be significant, several series may interleave so the exact execution of events is never guaranteed, not consistent across runs, however the following is always true:
EventA on unit X is ALWAYS followed by EventB on unit X, which is always followed by EventC on unit X.
All this is true for all Discrete Event Simulations.
Technically, I could generate all the events in the series, and throw them at the system, however, since the times are variable, I would prefer to generate the events in sequence in a chain: let EventA generate EventB, and let EventB generate EventC (and so forth)
Ideally, I would like to delete the events as soon as they are executed, to keep the system clean, so I have built a test with several units that are all running a series of events. Each event in the series has a small script that calls a function. Though the functions are distinct and different, each function, does the following: delete the event (trigger and action) that fired to call the function, modify the state of the unit in the desired manner, generate an event (trigger and action) to cause an event to fire for the subsequent event in the chain. I even have a function (We shall call 'ScriptUnit' in the discussion) that I call to generate the first event, and store information about the subsequent events as KeyValues. This all seems fine and dandy. All the code loads in, every function has been tested and works as described. The scripts that are generated to call the functions have been tested in the console and function as described on all the units. However, as soon as I try to get them to execute as events (not from the console) ONLY the first event of the first unit called by ScriptUnit is fired. All the other events appear in the GUI, with their triggers and actions. Everything is in place, but ONLY the first event of the first unit works as expected. After that event fires, the second event for the first series appears with its trigger and action. And it will fire at the appropriate time. But nothing else works. If I change the order of the calls to ScriptUnit, then a different unit's events will function, specifically the first unit called by ScriptUnit. The others are loaded into the system and appear in the GUI, but are not fired.
So the first question is: Can I reliably delete an event while executing the function triggered by the event? My whole coding structure is dependent upon this being the case, and looking through the forums, some seem to be of the opinion that this might not work.
The second question is: what is different about the Lua memory space and execution space when executing an event vs. executing from the console? Print statements are disabled, for example. Error reporting is different... Are there recommendations on how to code 'properly' for events vs the console? Clearly, I must trap my errors, and strongly consider using pcall if a ScenEdit.. function call could fail. Are there other recommendations that I should follow?
The third question is, how do I test my event code? Manually running events is a very slow way to test, and does not allow me to automate my testing.
The final question is what might cause the described behavior in my system?