Talk:AI Coding Examples

From OPU Wiki

"This version of code has 1 disadvantage: it will eventually crash OP2, because of a trigger-limit: you can only create 127 triggers and/or groups. So in this example it will take about 2500 time-marks for it to crash. Preventing this problem is somewhat tricky, but can be done."

-- How's it tricky? Store the trigger in a global var at creation, in the trigger proc use .Destroy() to delete it; then create the trigger again in that global var.

If you can use the repeating mode, do it if you can to save code and make everything nicer.

Footnote on destroying triggers

> That's what i do: i'm an excessive user of triggers, i guess.. i had op2 crash multiple times, so i have ALL my vars global. Also because Triggers and Groups use the same sub-class called ScStub, of which you have 127 MAX!

So; solution: i have this little function called DestroyTrigger. Whenever you create a new trigger (or group) assign it to a global var, and call DestroyTrigger(yourtrigger) before you assign it:

void DestroyTrigger(Trigger &trig)
{
  if (trig.IsInitialized())
  {
    trig.Disable();
    trig.Destroy();
  }
}

What does it do?

It first checks if the trigger is already in use. If it is> it destroys it, if its unused (or already destroyed) it does nothing.


A side-note: you could change the param (Trigger &trig) into (ScStub &stub) (and change trig into stub within the function), and it would also be usable to kill anused groups; but since i never kill groups (i always reuse them) i didn't use it in that way...