Skip to main content
n
1y ago

Flutter API not registering events/input changes triggered by nested artboard event

hi there. I am on latest rive and OSX 13.3.1. testing on latest chrome.

I am trying to get events to bubble up from nested artboards. works fine in rive but when using flutter api the onEvent and onInputChanged event handlers do not fire for this nested event. If I monitor the value of the bool in the API I see it never changes either. this is despite everything triggering fine inside the rive instance and editor.

I have a nested artboard called result. when its change event is fired I want it to fire a 'result-change' event in the parent artboard or toggle the resultChanged input but nothing is working. the result-change event fires in the editor but not when I try to listen w API.

14 replies
n
1y ago

Hi , nested events and nested inputs are not supported for any of the Rive runtimes. It's something that we have discussed enabling temporarily, but we are working on a new features that will replace the need for this altogether, and we're prioritising the work for that at this stage.

What you can do is "bubble up" events and "send down" inputs.

1y ago

Here is an example of affecting nested input values using an input from the main artboard. In this example, changing the "State" input (0, 1, 2) on the main artboard transitions to a different timeline, which triggers a change in the nested input "StarState" value. It is a workaround, however, in a sense, its a bit of a nice way to encapsulate nested inputs (for example, the main artboard can expose an input that sets several nested input values, so the runtime doesn't have to worry about each of the nested inputs separately).

NestedInputsViaRuntime.rev
1 KB
1y ago

That example is for inputs, but in a similar way the parent artboard can listen for events from a nested artboard and report a new event.

n
1y ago

thanks but I don't think I was clear enough. I am trying to use the API to listen to a parent artboard event (result-change), that has been triggered by a nested artboard event (change, inside the result nested artboard). This works in the rive file where the screenshot is from. But when I listen for parent artboard events using the API, only the ones not triggered by the nested artboard event are making it to the callback, despite the rive output behaving as expected.

I can see if I can find some time to recreate a stripped back example project with the problem

n
1y ago

hey just created a stripped back version and the events bubble up to the callback fine. Turns out what breaks this is some unsupported code I have come to depend on for accessing nested statemachine inputs. It seems that if I don't use this function when initialising my rive file the events will come through fine. But if I use it then it will break even though I am using it to access a different nested artboard instance.

I am really hanging in there for that data-binding feature! I know this is unsupported but if any advice on a tweak to this that could work until the feature arrives would be great. Currently I have a workaround for my project mirroring the events in the parent artboard and manually matching timing which is not ideal but seems less tedious than a bunch of boilerplate like the following which negates the benefit of having a nested artboard:

parent numeric input > 2 timelines in a blend with nested input keyframes (+ another state tab to reset input) > child input > 2 timelines in a blend for my desired animation (+ another state tab to reset input)

my code based on the answers in the flutter runtime github issues is here for reference:

import 'package:rive/rive.dart';
// ignore: implementation_imports
import 'package:rive/src/runtime_nested_artboard.dart';
// ignore: implementation_imports
import 'package:rive/src/rive_core/animation/nested_state_machine.dart';


mixin GameMixin {

  StateMachineController? getNestedController(Artboard artboard, String nestedName) {

    for (var nestedArtboard in artboard.activeNestedArtboards) {
      for (var animation
      in (nestedArtboard as RuntimeNestedArtboard).animations) {
        void callback(String stateMachineName, String stateName) =>
            onNestedStateChange(animation.nestedArtboard?.name ?? '',
                stateMachineName, stateName);
        if (animation is NestedStateMachine) {
          if (animation.stateMachineInstance
          is RuntimeNestedStateMachineInstance) {
            final cache = (animation.stateMachineInstance
            as RuntimeNestedStateMachineInstance);
            final StateMachineController controller = StateMachineController(
                cache.stateMachineController.stateMachine,
                onStateChange: callback);
            animation.stateMachineInstance = RuntimeNestedStateMachineInstance(
              cache.stateMachineController.artboard as RuntimeArtboard,
              controller,
            );

            String name = animation.nestedArtboard!.name;
            print('found nested $name');
            if (nestedName == name) return controller;

          }
        }
      }
    }
    return null;

  }

I appreciate this is annoying when you and team are actively working on a completely different and superior approach to solve this type of problem!

1y ago

Hi yes this is something that we don't officially support and spending time on this will take away from our desired solution. I won't have time soon to investigate this. But in a few weeks I might have time, if you can share a full sample project + rev that showcases how you're using the code that will be helpful.

n
11mo ago

sorry for the slow reply and thanks for humouring me :) I have created a stripped back version of the issue here:
https://drive.google.com/drive/folders/13fXwvX6_X01ctaw7PP3QI8CniFOAT0o2

the problem is here where I call the above getNestedController function:

void _onRiveInit(Artboard ab) {
  artboard = ab;
  final controller =
  StateMachineController.fromArtboard(artboard, 'State Machine 1', onStateChange: _onStateChange);
  artboard.addController(controller!);

  controller.addEventListener(_onRiveEvent);


  /// if I uncomment this nested input works
  /// but I stop receiving the forwarded 'result-change' event:
  // var nestedController = getNestedController(artboard, 'score');
  // var nestedInput = nestedController!.findInput<bool>('isVisible') as SMIBool;
  // nestedInput.value = true;

rev and riv file are in assets. with the above code uncommented some score UI appears top left

seems I can have EITHER access to nested inputs via the above hack OR I can get events forwarded from a different nested artboard to the parent artboard callback but not both :/ (a parent and a forwarded event should fire when the center popup appears in the loop)

FYI, I would also love a way to set text for text-runs with API in nested artboards (currently creating text runs in parent to overlay nested artboards and syncing animation)

Not blocking me as I have a workaround for my project and appreciate you have other priorities but if you get a chance to take a look before the data-binding feature makes this all redundant please do!

n
11mo ago

hi I spent some time today trying to refactor my project to avoid accessing nested inputs with the API so I can forward events as recommended. but turns out that the original bug is still present in my project after the changes.

the following fires in rive but not in the API:

nested artboard event > parent artboard event listens to nested event > fires parent artboard event in rive fine > but API event callback does not receive this parent event

however this workaround does work:

nested artboard event > parent artboard event listens to nested event > fires parent artboard trigger in rive > trigger changes animation timeline change with parent event reported in transition > API event callback does receive this parent event fine

I am installing this project next week so likely can't work up another example project until after then but wanted to let you know as the previous example might point you in the wrong direction. cheers.

11mo ago

Hi , some good news we will provide an official way to access Nested Inputs. It should release in the next few days!

n
11mo ago

That’s amazing !

in that case I’ll hold off refactoring to remove nested artboard inputs. Hopefully the new feature will take care of this bug. Will suss it out then :)

n
11mo ago

Fingers crossed for nested events and text runs!

11mo ago

Nested events and text runs not yet! This is only nested inputs for now

11mo ago

Nested inputs are now exposed on the latest version of the runtime! See our docs: https://rive.app/community/doc/state-machines/docxeznG7iiK#nested-inputs

n
11mo ago

Amazing thanks Gordon, can’t wait to try it out over the next week :)