Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Help with arrays

Explorer ,
Jun 01, 2016 Jun 01, 2016

I'm spending an eternity trying to figure out how to perform a simple action, so I hope someone can help me.

I just want to use JavaScript to create a list of comma-separated text values, and then display one of these values (according to its position). I know how to display when there's only one value in a variable - but I think this requires an array.

Example:

var = animal

the values are:  cat, dog, snake

and I want to

setVariableValue  "animal" to display the second value ("dog").

I've been learning javascript, but can't adapt any of the examples to work for Captivate.

Sorry for the basic question.  Thanks.

Bill

4.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

People's Champ , Jun 02, 2016 Jun 02, 2016

I had an error in the script, parens instead of braces in myArray[ 1 ]:

var myArray = [ "cat", "dog", "snake" ];

window.cpAPIInterface.setVariableValue( "animal", myArray[ 1 ] );

It works for me in both swf and html.

Translate
Contributor ,
Jun 01, 2016 Jun 01, 2016

Hi Bill,

So I'm guessing you're asking the following:

1: How to write Javascript that saves the values cat, dog, and snake into an array.

2: How to write Javascript that gets one of those values out of an array and then writes it into a Captivate variable.

Is that correct?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 02, 2016 Jun 02, 2016

Yes, that's basically it. I've been going through the Khan Academy course on arrays, but I can't figure out how to make them work in Captivate. I also tried to use the listbox widgets, but couldn't figure out how to get the comma separated values from those.    I think an example might set me on the right path. Any suggestions?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 02, 2016 Jun 02, 2016

What is your goal? Although JS can extend Captivate's functionality a lot, since you are talking about 'listbox' widget (old widget), maybe you can do this in a much easier way? Can you also please tell which CP-version you are using, and if you want to publish to SWF or to HTML? Because the listbox widget is SWF only.

Have a look at this article, where I explained how to use the List box widget:

http://blog.lilybiri.com/widgets-and-custom-questions-part-3

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 02, 2016 Jun 02, 2016

Hi Lilybiri,

I've looked through your posts pretty closely, and they've been helpful in the past, but am still at a loss un this case.  I really need to use JS because I intend to do some other things with the arrays down the road.  I mentioned the listbox, but I really don't need to use a widget.  I just want to be able to parse a simple array for now, as in my example.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 02, 2016 Jun 02, 2016

PS- I'm using CP9.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 02, 2016 Jun 02, 2016

You want to be able to push items from an array into Captivate variables, using JS? Or the way round: pushing content of Captivate variables to an array? Or maybe both? Did you read this:

Common JS interface

Because other experts are talking a lot about JS I keep focus in my blog on advanced/shared actions.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 02, 2016 Jun 02, 2016

Hi Bill,

To help with this we really need to know if your project is being exported for HTML5 or for SWF.

Tristan,

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jun 02, 2016 Jun 02, 2016

Without knowing how you want it to be triggered you can execute this JavaScript:

var myArray = [ "cat", "dog", "snake" ];

window.cpAPIInterface.setVariableValue( "animal", myArray[1] );

This will populate "animal" with "dog";

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 02, 2016 Jun 02, 2016

Thanks - I want to trigger this with a click button on the same slide.  I tried to preview your code both ways: "in browser" and "HTML5 in browser".  I put the variable in a text box, and after the trigger it was blank for "in browser" (which i assume is swf?) and all three animal names appeared in html5. Do I need to reference CP above this code? If so- how should I do that?

At this point, I'd be happy to have it work for either swf or html5.

Thanks for everyone's help.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 02, 2016 Jun 02, 2016

Did you define the variable 'animal' in Captivate? That is the name the JS is referencing to. That variable has to be inserted in a text container (is that what you mean by 'text box'?).  API is same for SWF and HTML.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jun 02, 2016 Jun 02, 2016

I had an error in the script, parens instead of braces in myArray[ 1 ]:

var myArray = [ "cat", "dog", "snake" ];

window.cpAPIInterface.setVariableValue( "animal", myArray[ 1 ] );

It works for me in both swf and html.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 02, 2016 Jun 02, 2016

Fantastic.  Thanks, it worked!  Now if I want to show 2 values "dog" and "snake", does that require a loop? I tried:

var myArray = [ "cat", "dog", "snake" ];

window.cpAPIInterface.setVariableValue( "animal", myArray[ 1 ][ 2 ] );

But that just populated with the "g" at the end of dog (which is pretty interesting actually).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jun 02, 2016 Jun 02, 2016

Your code represents a multi-dimensional array, you would need to use:

window.cpAPIInterface.setVariableValue( "animal", myArray[ 1 ] + ", " + myArray[ 2 ] );

This concatenates the two values separated by a comma and a space: dog, snake

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 02, 2016 Jun 02, 2016

Thanks again! I'll start a new thread with any new questions, but this gives me something to work with.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 02, 2016 Jun 02, 2016

Sorry, David, but I had corrected your first answer for that small typo with the square brackets. Hope you didn't mind.

Arrays, random numbers and formatting of numbers are the three simple work flows that I'm using JS for as well...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jun 02, 2016 Jun 02, 2016

Not a problem Lieve, thanks. I wondered how he even got it to work in HTML5.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 10, 2019 Jan 10, 2019

Hi there David,

I was wondering how I could reset the array OnEnter.

If I take the above example, I would not want the "animal" to be populated with "dog", "cat" or anything. I wanted it to be for instance "choose an animal"

Thanks in advance

Bobby

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jan 10, 2019 Jan 10, 2019

Reset the array:

var myArray = ["", "", ""];

Not sure what you are using to display the "choose an animal" text.

You could put that text in 1 or all of the positions:

var myArray = ["choose an animal","choose an animal","choose an animal"]

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 10, 2019 Jan 10, 2019

Sorry

I should have explained more.

OnEnter I have this JS hooked

var profileCharacters = [ "", "Jamie", "John" ]

there are buttons with this attached to them

window.cpAPIInterface.setVariableValue("variableForProfile", profileCharacters[1]);

window.cpAPIInterface.setVariableValue("variableForProfile", profileCharacters[2]);

etc.

They would change the value.

The initial value is empty.

That is all good but when I go to the next slide and then come back to this slide, the variable has the last value assigned.

I want to reset it.

Thanks again

Bobby

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jan 10, 2019 Jan 10, 2019

window.cpAPIInterface.setVariableValue("variableForProfile","");

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 11, 2019 Jan 11, 2019

Thanks David,

This works.

So here is what I use. Do you think I should "polish" it a bit? Do I need all of that code?

I have this variable declared in Project/Variables: varProfileCharacter - it does not have any value.

I have also a smart shape with 3 states called "container_Character" and 2 of the states are Jamie and John.

On enter I have the following JS attached with "Current" and Continue Playing the Project ticked.

window.cpAPIInterface.setVariableValue("varProfileCharacter","");

var profileCharacters = [ "", "Jamie", "John" ]

On the button to change to profile 1 I have

window.cpAPIInterface.setVariableValue("varProfileCharacter", profileCharacters [1]);

cp.changeState("container_Character","Jamie");

On the button to change to profile 2 I have

window.cpAPIInterface.setVariableValue("varProfileCharacter", profileCharacters [2]);

cp.changeState("container_Character","John");

Does that seem neat?

It works well but I am just asking myself if there maybe too much of lines.

Thanks

Bobby

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jan 11, 2019 Jan 11, 2019

It looks fine to me. I don't know what you mean by "JS attached with 'Current'" though.

If you are resetting the variable, are you also resetting the state of the object that has the Characters?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 11, 2019 Jan 11, 2019

Thanks David

The state would reset automatically unless I choose "Retain State on Slide Revisit"

AS to the JS attached with Current I mean that the Script window is Current, sorry

Thanks again

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jan 11, 2019 Jan 11, 2019
LATEST

You're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Help resources