Text randomizer in Lens Studio

Hi, I’m a bit struggling to create a text randomizer in LS. What I found in the LS forum, youtube and Asset Library only randomize the color or the object, which is not preferable in my case, cuz I want to be able to animate the alpha of the text with 1 alpha tween from 1 trigger.

I can simply do it in spark, which is by driving the text input from a set of strings in an option picker or script, shuffle the array order, reveal, and re randomize anytime using the same trigger that animate the alpha of the text.

Can someone teach me how to do it LS?

Thank youuu

can you show some use case of it?
here is how u access to the text component and adjust its alpha

// @input Component.Text textComponent

script.textComponent.textFill.mode = TextFillMode.Solid;

script.textComponent.textFill.color = new vec4(1, 0, 0, 1);
1 Like

Umm I’m afraid I can’t show the exact project yet cuz it’s for a client.
Nothing fancy tho, it’s like “what … are you?” type of filter that the answer is not an image, but a text.
the basic mechanics and sets are something like this:

  1. effect opens
  2. face found + randomizing the answer in the background/alpha 0
  3. some animations happen
  4. all animations in point 3 finished, then the result from point 2 gradually shows up/increasing opacity from 0 to 100%
  5. done. lol

Here’s a quick code snippet that will help. I haven’t run this in Lens Studio so maybe there are some errors, but it’ll get you started down the right path.

// @input Component.Text textObj

var options = [
    "option 1",
    "option 2",
    "option 3"
];

var index = 0;
var shouldShuffle = true;

script.createEvent("UpdateEvent").bind(function() {
    if (shouldShuffle) {
        index += 1;
        script.textObj.text = options[index % options.length];
    }
});

script.createEvent("MouthOpenedEvent").bind(function() {
    shouldShuffle = false;
});

Use whatever event you want, whether it be a Lens Studio event or a custom event you create, to stop the shuffling and trigger your alpha change (I’m assuming with a tween - the tween manager page in the LS docs shows examples of triggering tweens from scripts).

2 Likes

i was curious and try it and its work well :wink: thank you

Thank you! It solves my problem!
what if I want to stop the shuffle using custom trigger from another script? how to expose the trigger so it could be driven by another trigger?

// Add a callback function to call when the global custom trigger named "triggerName" is sent
//  global.behaviorSystem.addCustomTriggerResponse(triggerName, callback)

Take a look at the script referencing section here :point_right:
https://lensstudio.snapchat.com/guides/scripting/scripting-overview/