✅ Solved: Trigger only works once in Data Binding but works every time as input
I have a rive artboard to show toasts in my game. I set it up to use just the view model and had a show trigger in the view model. I set the data and then triggered whenever a toast was added to the queue. This worked the first time but none of the following triggers actually went through. I added a log to make sure the function was being called.
I knew trigger inputs always worked before so I added a show input trigger and changed the code to use that and now it works perfectly. Is there a reason triggers only work once in the ViewModel but not in inputs? All the other properties work correctly.
Here’s my code. I left the place where I pulled the trigger from the view model and the only other change was I had showTrigger.trigger() instead of showTrigger.fire()
I attached the backup and the code here.
class RiveToastType {
static Item = 'Item';
static Card = 'Card';
static Quest = 'Quest';
}
class RiveToast {
constructor(rive,file,canvas,renderer) {
this.rive = rive;
this.file = file;
this.canvas = canvas;
this.renderer = renderer;
this.artboard = file.artboardByName('Toast');
this.width = this.artboard.bounds.maxX;
this.height = this.artboard.bounds.maxY;
this.stateMachine = new rive.StateMachineInstance(this.artboard.stateMachineByName('State'), this.artboard);
this.transform = new rive.Mat2D();
this.viewModel = file.defaultArtboardViewModel(this.artboard).defaultInstance();
this.stateMachine.bindViewModelInstance(this.viewModel);
// get view model properties
this.type = this.viewModel.enum('Type');
this.text = this.viewModel.string('Text');
this.showTrigger = this.viewModel.trigger('Show');
this.shown = false; // Track if the toast has been shown
for(var i = 0; i < this.stateMachine.inputCount(); i++) {
var input = this.stateMachine.input(i);
if(input.name == "Show") {
this.showTrigger = input.asTrigger(); // Get the trigger for showing the toast
}
}
// create array for toasts
this.toasts = new Array();
this.show = (type, message) => {
this.toasts.push(new Toast(type, message));
}
this.processQueue = () => {
// Process the queue to show the next toast if available
if (this.toasts.length > 0 && !this.shown) {
this.shown = true; // Mark as shown
var toast = this.toasts.shift(); // Get the first toast from the queue
this.type.value = toast.type; // Set the type in the view model
this.text.value = toast.message; // Set the message in the view model
this.showTrigger.fire(); // Trigger the show action
console.log("Showing toast: " + toast.type + " - " + toast.message);
}
}
this.draw = (elapsedTimeSec) => {
//if (this.toasts.length == 0 && this.shown == false) return;
// No toasts to draw if none are queued and not currently shown
// If the toast is not currently shown and there are toasts in the queue
this.processQueue();
// listen for the toast to finish showing
if(this.shown) {
for(var e = 0; e < this.stateMachine.reportedEventCount(); e++) {
var event = this.stateMachine.reportedEventAt(e);
if(event.name == "Complete") {
this.shown = false; // Reset shown state
console.log("Toast completed showing");
this.processQueue(); // Process the next toast in the queue
}
}
}
// offset the artboard position so it appears at the bottom center of the screen
this.transform.tx = ((this.canvas.width - this.width) / 2) * Game.UIScale;
this.transform.ty = (this.canvas.height - this.height - 100) * Game.UIScale; // 100px from the bottom
this.transform.xx = Game.UIScale;
this.transform.yy = Game.UIScale;
//render the toast
this.stateMachine.advance(elapsedTimeSec);
this.artboard.advance(elapsedTimeSec);
this.renderer.save();
this.renderer.transform(this.transform);
this.artboard.draw(this.renderer);
this.renderer.restore();
}
}
}
class Toast {
constructor(type, message) {
this.type = type;
this.message = message;
}
}