[SOLVED] Ideas for action trigger like architecture

I’m currently building a small framework to streamline reoccurring tasks.
As I’m usually integrate PC into PWAs using iframes, i have to rely heavily on messages (from main application to pc iframe and vice versa)

Here I thought a unity like trigger / action system could be usefull. My idea was to use script components (e.g. trigger → touch or click and receiver ->mainly message listening). How could I use a script attribute to decouple the action function from the the message listening?
My goal would be to be able to build simple functionality in the editor by simply attaching a script (message listener) and then assign a function to the attribute or alternative add an additional script which holds the actual “action” code without haven the entire listening code duplicated for different action functions.

I hope this makes sense, and as always thanks for any tips.

Hi @Rechi,

I would use events to trigger custom actions (and reuse the same logic), if I understand correctly what you are asking.

For example this is a script taken from the UI Buttons example, I extended it to fire a custom event (action) that can be set in editor for each button:

var ButtonLogic = pc.createScript('buttonLogic');
ButtonLogic.attributes.add('textEntity', {type: 'entity', description: 'The entity that we want to update when the button is clicked'});
ButtonLogic.attributes.add('description', {type: 'string'});
ButtonLogic.attributes.add('event', {type: 'string'});

// initialize code called once per entity
ButtonLogic.prototype.initialize = function() {
    this.entity.button.on('click', function(event) {
        this.textEntity.element.text = this.description;
        
        this.app.fire(this.event);
    }, this);
};
4 Likes

Thanks Leonidas,
that makes sense for the trigger → here the event-string allows the usage for different scenarios.
But how could I build an action reciever component which would allow similar flexibility?
What I would need in theory is a script-reference-attribute (I hope that makes any sense for you), which I then could configure similar to the event-string-attribute.
As this is not really possible in PC (as far as I know) maybe there is a way to add an additional script component to the receiver, which then has different implementations of a (somewhat) abstract action-Function.
All other solutions I can think have either
a) a quite large message receiver component, which has the general event receiving code + handling of all messages (and action code)
b) different script components for different actions, all copying the event receiving code

I’m not 100% sure what you are asking for here but this is my approach on the understanding: https://playcanvas.com/project/990833/overview/trigger-related-stuff

I would have a ‘listener’ script that would wait for some external event/trigger. Eg a window message event, keyboard press, physics collision etc

The listener would then fire an internal PlayCanvas event that the ‘action’ scripts are listening for and do something such as change size or material.

I’ve also added a second ‘listener’ script where you can add script and function names to explicitly call instead.

2 Likes

Thanks @yaustar.
on-key-2.js does exactly what I was aiming for, but also on-key-1 makes sense, as it reduces the overhead of the listening as well (listening only to the app-event) and might be more flexible on the first look.
Thanks for both solutions!

1 Like