How can I create a user-defined trigger

I like the idea of the mesh action manager actions, especially chaining actions together in a sequence. However, it looks like the actions can only be triggered off of pre-defined triggers (OnPick, OnPointerEnter, etc…). What if I want to trigger an action sequence in code? Is there a way to create an action that listens to a user-defined trigger?

Hiya L. A long-time BJS pro named JcPalmer… once answered that for us (from our old forum)…

action.execute(optional ActionEvent);

Easy, eh? The optional actionEvent might seem weird. I’ll try to explain, but beware of possible/likely wrong-things being said by me. :slight_smile:

SOME actions use “predicates”… which are a type of “late-conditional”. For example, there is a meshUnderPointer property on actionEvents. An Action itself… can be defined with a predicate that might dictate only perform this action if event.meshUnderPointer === ground.

source is also sometimes important info to pass to action-handlers… and so is additionalData. All that stuff… could be important to things “downstream”… depending upon what type of “trigger” you are trying to “fake”. :slight_smile:

https://www.babylonjs-playground.com/#4HUQQ#99 Lines 77-80 shows an action defined, and then an execute() without using an actionEvent param.

ExecuteCodeAction is one of the most popular actions. In THIS PG, we can see some executeCodeActions defined (lines 104-106). It is easy to see how these actionEvents are passed-to the event handler (execute)Code. In lines 67, 93 and 97 … the actionEvent arrives as the 1st arg … and it is named bjsEvent in this case. Watch JS console while click-testing, as wanted.

Inside an executeCodeAction action-handler, you could check a few things… and determine if you want to ignore it, or process it normally, or create a HUGE “cascade” (sequence) of action.execute() done to MANY predefined actions. PLUS… in there, you can actually register or unregister actions (and triggers) onto/from ANY other (mesh-based) actionManager… AND to/from a scene.actionManager. Big-time power… for action sequencing/cascading. Also, study the actionManager’s then directive/feature… which can do “chaining” FOR you.

Ok, that should get you rolling. I wish I had more info as-to WHEN/WHERE you should construct an actionEvent for your action.execute(here) call. What you ask to do… is fairly rare. But it seems to me… that the actionEvent class/object… is a nice way to do a whole lot of delicious lying. :slight_smile:

Maybe others will comment soon… with more wisdom.

2 Likes