How to start random video

Hello i make random image:

<<set _number = random(1,10)>>
<<set $imageitog = "images/ (" + _number + ").jpg">>
[img[$imageitog]]

but how i can set random video? besides

<if..
 <video>
<source src="URL">
</video>
<else...
 <vi..

SugarCube 2.28.2

2 Likes

first, push video url to an array

<<set $url = []>>
<<set $url.push(video url 1)>>
<<set $url.push(video url 2)>>
...
<<set $url.push(video url n)>>

and when you call video url, use either($url)

<<= '<iframe width="560" height="315" src="'+either($url)+'" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>'>>

I’m not good at this so I suppose that there are much better solutions

1 Like

Thanks, this work cool

<<set _number = random(1,2)>>
<<set $imageitog = "images/ (" + _number + ").webm">>

<<= '<iframe width="560" height="315" src="'+$imageitog+'" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>'>>

or this

<<set _number = random(1,2)>>
<<set $imageitog = "images/ (" + _number + ").webm">>

<<= '<video autoplay="true" loop="true">
   <source src="'+$imageitog+'">
   </video>'>>

The <<= some HTML code here>> method is known as the “stupid print trick”, and you had to use that with older versions of SugarCube for things like this.

However, with newer versions of SugarCube (preferably v2.23.5+) it’s a lot simpler to use attribute directives instead. Using that, your code would simply be:

<video @src="'images/(' + random(1, 2) + ').webm'" autoplay loop>

The @ before the “src” attribute directs SugarCube to evaluate the contents of that attribute and use that value. Thus the above will randomly display either the images/(1).webm or the images/(2).webm video.

Enjoy! :wink:

1 Like