Page 1 of 1

Deleting Events

Posted: Wed Mar 03, 2021 8:38 am
by jkgarner
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.
e.g.
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?

Any thoughts?

RE: Deleting Events

Posted: Wed Mar 03, 2021 10:08 am
by KnightHawk75
At the risk of responding without adding much or helping at all.
On #1 If not could just queue up the deletes and have a watchdog event clean up the events every X amount of time.
On #2 The only thing I can think to remind about is testing things in the console with Tool_EmulateNoConsole(true) while building events as it should better represent how things will act under real event execution environment, as you mentioned, sometimes there may be a difference in expected results due to err suppression.
On #3 Fair question, I got nothing for you.
On #4 hmm I need to chew on it more and try replicating something similar before speculating on that.




RE: Deleting Events

Posted: Wed Mar 03, 2021 10:34 am
by jkgarner
Yeah, I threw a lot out there.

I would like a definitive answer on #1, as it will radically alter my approach if it is not possible.

Re #2: I had forgotten about that function, until you mentioned it. I need to incorporate that in my manual test code, so that I don't forget to call it.

I'll keep thinking about #3, as I really wand full test coverage on my code, especially the event code that is so dynamic.

Re #4: if #1 is not possible, it may be that the deletion of a running event somehow craps on the event driving system within Command. My next step is to refactor the code (delete the previous event instead of the current event) to see if things work better. It would be quicker and easier to build the 'watchdog' event, but polling can be very expensive... An easy test is to simply comment out the deletion code, allowing the events to build up, and see what happens. If this fixes things then it is the concurrent deletion that is the problem.

I appreciate your thoughts.

RE: Deleting Events

Posted: Fri Mar 05, 2021 12:17 am
by jkgarner
To answer the first questions:

I've hooked the code up to delete the events from the scritp called by the event...
Once I had all my scripts CORRECT, everything worked like a charm. I see no issues with deleting events from the code triggered by the event.

I still would like to figure out a way to unit test the event code in an automated fashin, instead of this piecemeal stiff I've been forced to do.