Author Topic: Managing script to script conflicts  (Read 349 times)

garry23

  • Contributor
  • Hero Member
  • *****
  • Posts: 1772
Managing script to script conflicts
« on: April 01, 2019, 05:53:54 PM »
@a1ex

I’m looking to refactor my scripts so they work better together.

The issue I usually face is that script A looks to use a key 1, but so does script B.

I’m therefore looking to somehow use my cycler script as a master control script, ie to manage event.keypress and event.shoot_task, ie to deconflict scripts.

What I don’t fully understand is how the keypress event is handled, ie does it pass through each script in turn?

When you have a spare moment, I hope you can throw some education my way  ;)

Cheers

Garry

a1ex

  • Administrator
  • Hero Member
  • *****
  • Posts: 12282
  • Emergencies only
Re: Managing script to script conflicts
« Reply #1 on: April 01, 2019, 07:42:13 PM »
I think I've answered it before, but... here it goes.

https://builds.magiclantern.fm/lua_api/modules/event.html#keypress

The Lua module passes the key press event to all scripts that want to use it. Each script may decide to use the event, or not. This decision is in the return value - if you return false, that means the key event is not going to be forwarded to other scripts, modules, or to Canon firmware.

So, if you want to take over the MENU key, and to prevent the user from entering Canon menu, you will need to return "false" when receiving this key event.

If more than one script registers a keypress event, the first one (alphabetically, from file name) will receive it first. Same for modules. Not the best idea, but at least slightly more predictable than random.

garry23

  • Contributor
  • Hero Member
  • *****
  • Posts: 1772
Re: Managing script to script conflicts
« Reply #2 on: April 01, 2019, 08:05:21 PM »
@a1ex

Thanks, that’s what I thought, however, I’m ‘confused’ a little.

If I have two scripts loaded, a.lua and b.lua, then a.lau script deals with the key first. 

If a.lua returns true for that key, then b.lua doesn’t see the key, correct? In other words it has been ‘consumed’.

But what if a.lua returns false, does that mean b.lua gets access to managing the key?

And once all scripts have been addressed, then and only then the key, if still true, is handled by the Canon side.

Have I got it right?

Cheers

Garry

a1ex

  • Administrator
  • Hero Member
  • *****
  • Posts: 12282
  • Emergencies only
Re: Managing script to script conflicts
« Reply #3 on: April 01, 2019, 08:09:42 PM »
If a.lua returns false for that key, then b.lua is not going to see it, i.e. "return false" means it has been ‘consumed’.

If all the handlers (from scripts, modules, ML core) return true for that key (i.e. "not consumed"), only in this case that key is going to reach Canon firmware.

garry23

  • Contributor
  • Hero Member
  • *****
  • Posts: 1772
Re: Managing script to script conflicts
« Reply #4 on: April 01, 2019, 08:19:24 PM »
Got it!

Many, many thanks.

Cheers

Garry