heronarts / LX

Core library for 3D LED lighting engines
https://chromatik.co/
Other
41 stars 25 forks source link

How to wait until mixer setup is complete? (LXMixer & LXChannels) #71

Closed worldveil closed 2 years ago

worldveil commented 2 years ago

I've got some logic for indexing and cataloging patterns and channels that happens when a new project file (.lxp) is loaded.

Right now, this setup sometimes explodes because the channels aren't created yet.

Other than wrapping code like this in an exponential backoff:

while ( ... ) {
  try {
      lx.engine.mixer.channels.get(EFFECTS_CHANNEL_INDEX_INT_HERE);
      // ...
  } catch (IndexOutOfBoundsException e) {
     // double the wait, and try again
  }
}

are there any good hooks for putting said initialization code into?

I think initialize(LX lx) is only run on UI start, not project load, and therefore is also before the mixer is fully loaded with channels specified in the .lxp

mcslee commented 2 years ago

Yep, project load happens after all the initialization hooks and setup.

If you want to monitor project load and perform actions after, do this:

lx.addProjectListener(new LX.ProjectListener() {
  public void projectChanged(File file, Change change) {
    // File will be the project file
    // Change will be one of TRY, NEW, SAVE, or OPEN
    // You probably only care about doing something on OPEN
  }
});

Closing this for now, but let me know if you have any issues getting that working and feel free to re-open if so!