Forum Discussion
How to send and receive variables with Javascript?
Hey Jordan Best - I know this is a really late reply, but that seems to be the nature of a lot of these SL forums :)
I was tinkering around with some JavaScript in Storyline and managed to accomplish what you wanted - I was able to take internal Storyline variables and use them to populate a graph in a web object. You were so close!
I think your var1 function at the end of your post made var1 a '0' because the function doesn't return the value that you're after.
Here's what worked for me. I only added 3 lines to your code - the two lines above the var ctx = document.getElementById('myChart');
line (I used player2
instead of player
because I already have var player = GetPlayer();
in some JavaScript in Storyline.) and on the line with data:
I added var1
instead of 12
.
I hope this helps you, or someone else in a similar situation :)
<body>
<canvas id="myChart"></canvas>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
var player2 = parent.GetPlayer();
var var1 = player2.GetVar("StorylineVar1");
var ctx = document.getElementById('myChart');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: '# of Votes',
data: [var1, 19, 3, 5, 10, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
</script>
</body>
Hi Jim! Wow, can't believe how long ago that was... I forgot all about it and jettisoned that idea when I couldn't figure it out!
I'm curious, I didn't spend too much time on it yet, but when I created a variable "StorylineVar1" and set it to a number, then copied/pasted your code into a web object index.html, I'm not seeing anything. Was there something else you had to set up to get it to work?