Hi there,
how can I create a custom behavior that run a javascript to randomize 3 timelines and finally start one of the three?
I studied the random scene javascript function and tried to emulate it but it does not run at all. Any suggestion?
var links = ["timeline1","timeline2","timeline3"];
function untitledFunction() {
// Chooses a random link:
var i = Math.floor(Math.random() * links.length);
// Directs the to the chosen target:
hypeDocument.startTimelineNamed(links[i];
return false;
}
untitledFunction();
// Get an array of the names of all timelines
let timelineNames = ["attivita1", "attivita2", "attivita3"];
// Generate a random index from 0 to 2
let randomIndex = Math.floor(Math.random() * 3);
// Pick the timeline at the random index
let timelineName = timelineNames[randomIndex];
// Start the randomized timeline
hypeDocument.startTimelineNamed(timelineName, hypeDocument.kDirectionForward);
The reason you initial code did not work is because of a syntax error.
When code does not work, it is always best to see what the browser console is saying before you go down a rabbit hole..
hypeDocument.startTimelineNamed(links[I];
should be
hypeDocument.startTimelineNamed(links[I]);
I assume chatGPT could have told you that also if you asked it why the code is not working?
Also apart from the original code being inside a sub function inside a hype function for no real good reason I can se, imo where it uses. * links.length
is better than a hard coded number as chatGPT has done.
If you want you can use this code on scene load to randomize 3 or more different timelines. I've personally used this in a custom behavior but that's your choice.
I totally agree with you. Better to manage the code and do stuff by yourself. I didn't notice the error and I didn't think either to check the code with chatGPT. But I did this check now so the answer is yes, it spots the errors, look at the image