Forum Discussion

SwiftC's avatar
SwiftC
Community Member
2 months ago

Issue with Execute JavaScript Trigger in Storyline 360

I am working on a project in Articulate Storyline 360 and I'm trying to execute a JavaScript function when the timeline starts on a slide. The function is supposed to replace the default values of project text variables with specific messages based on the slide number.

Here is the JavaScript code I'm using:

function setTexts(slideNumber) {
    console.log("setTexts function called with slideNumber: " + slideNumber); // Log the function call

    var correctTexts = [
        "This is a credit card statement with the amount of money that student owes.",
        "This document is intended to give a record of purchases and payments. It gives the card holder a summary of how much the card has been used during the billing period, as well as the amount that is due for that billing cycle."
    ];

    var incorrectPromptTexts = [
        "Here's a hint, there are dollar amounts on there and itemized activity, what type of document best fit these?",
        "Think about why you would need a statement for bills, please try again."
    ];

    var studentUnderstandTexts = [
        "Got it! I always get these confused with my car insurance for some reason.",
        "Yes now that I think about it, having an itemized record is very helpful, even if it's quite annoying to always get these in the mail."
    ];

    var positiveFeedbackTexts = [
        "_user_ you answered correctly! Awesome job!",
        "_user_ you got the question correct!",
        "_user_ you answered correctly awesome job!",
        "_user_! You answered correctly!",
        "_user_! You answered the question right!",
        "_user_, You answered right!",
        "_user_, You answered the question right!",
        "_user_, you answered correctly! Keep it up!",
        "_user_, You answered the question right! Keep up the great work",
        "_user_ you are doing great! Keep it up!"
    ];

    var negativeFeedbackTexts = [
        "_user_, sorry. the answer you gave wasn't what I was looking for.",
        "_user_, your answer was not quite right.",
        "_user_, It looks like you picked the wrong answer.",
        "_user_, I'm afraid the answer you chose wasn't the best one."
    ];

    var player = GetPlayer();

    // Log the text being set for each variable
    var correctText = correctTexts[slideNumber - 1];
    console.log("Setting Correct to: " + correctText);
    player.SetVar("Correct", correctText);

    var incorrectPromptText = incorrectPromptTexts[slideNumber - 1];
    console.log("Setting IncorrectPrompt to: " + incorrectPromptText);
    player.SetVar("IncorrectPrompt", incorrectPromptText);

    var studentUnderstandText = studentUnderstandTexts[slideNumber - 1];
    console.log("Setting StudentUnderstand to: " + studentUnderstandText);
    player.SetVar("StudentUnderstand", studentUnderstandText);

    // Randomly select positive and negative feedback
    var randomPositiveFeedback = positiveFeedbackTexts[Math.floor(Math.random() * positiveFeedbackTexts.length)];
    var randomNegativeFeedback = negativeFeedbackTexts[Math.floor(Math.random() * negativeFeedbackTexts.length)];

    console.log("Setting PositiveFeedbacktoUser to: " + randomPositiveFeedback);
    player.SetVar("PositiveFeedbacktoUser", randomPositiveFeedback);

    console.log("Setting NegativeFeedbacktoUser to: " + randomNegativeFeedback);
    player.SetVar("NegativeFeedbacktoUser", randomNegativeFeedback);
}
// Call the function with the current slide number
setTexts(GetPlayer().GetCurrentSlide().GetSlideNumber());

When I preview the project, the console provides the following message:

bootstrapper.min.js:2 actionator::exeJavaScript - Script1 is not defined

I have defined the setTexts function and ensured that it is called correctly in the "Execute JavaScript" trigger, but the error persists.

Steps I've Taken:

  1. Defined the setTexts function.
  2. Added the function call setTexts(GetPlayer().GetCurrentSlide().GetSlideNumber()); in the "Execute JavaScript" trigger.
  3. Verified that the function is being called correctly.

Any help or suggestions on how to resolve this issue would be greatly appreciated. Thank you!

  • Script not defined often occurs when there is a syntax error like unbalance braces. I know the code listed here seems correct, but make sure the code in your SL project is not missing any bits.

    Aside from that, what is GetPlayer().GetCurrentSlide().GetSlideNumber()? There are no such functions within GetPlayer(). The production player has the following:

    GetVar() and SetVar() are really the only useful entries for most users.

    If you want the slide number within the scene, starting with 1, you can use the built-in Scene.SlideNumber variable. Note, you must first assign this to a user-defined variable to access it from JavaScript via GetVar().

    Try substituting the value 1 as the argument to your setTexts function until you get everythign else working as it should, then worki on getting the slide numbers.

     

  • Nedim's avatar
    Nedim
    Community Member

    Define a variable named slideNumber in Storyline and set it to match the value of the built-in Project.SlideNumber variable when the timeline starts. Then, declare the slideNumber variable in your JavaScript code.

    let slideNumber = getVar('slideNumber');

    Then, call the function with the current slide number:

    setTexts(slideNumber);

    Logged: