0 votes
by (1.4k points)

Hello all Twinists!

I am using Sugarcube 2.18.0, actually editing a previously Harlowe story to work in Sugarcube.

I am able to have random videos display in Harlowe using this setup:

(set: $var to (either: "a", "b", "c"))
(if: $var is "a")[<iframe width="560" height="560" align="center" src="https://www.youtube.com/embed/L28CnhmeAD4?autoplay=1;rel=0&amp;controls=0&amp;showinfo=0;iv_load_policy=3" frameborder="0" allowfullscreen></iframe>]
(if: $var is "b")[<iframe width="560" height="560" align="center" src="https://www.youtube.com/embed/w5qf9O6c20o?autoplay=1;rel=0&amp;controls=0&amp;showinfo=0" frameborder="0" allowfullscreen></iframe>]
(if: $var is "c")[<iframe width="560" height="560" align="center" src="https://www.youtube.com/embed/hEnfZjqMSy0?autoplay=1;rel=0&amp;controls=0&amp;showinfo=0;iv_load_policy=3" frameborder="0" allowfullscreen></iframe>]

 

Is there something similar that can be done in Sugarcube?

Thank you in advance!

1 Answer

0 votes
by (63.1k points)
edited by
 
Best answer

An "exact" transformation of your code as written might look like this: 

<<set $var to either("a", "b", "c")>>\
<<if $var is "a">>\
  <iframe...></iframe>\
<</if>>\
<<if $var is "b">>\
  <iframe...></iframe>\
<</if>>\
<<if $var is "c">>\
  <iframe...></iframe>\
<</if>>

However, I'd recommend some modifications. First, for something like this, I'd recommend you use numbers rather than letters. It doesn't really matter, but it seems like an odd choice, since numbers are generally easier to track and require less typing. 

Further, your code as written does not require the $var at all if you use the <<switch>> macro instead of the <<if>>: 

<<nobr>>
  <<switch random(2)>>
  <<case 1>>
    <iframe...></iframe>
  <<case 2>>
    <iframe...></iframe> 
  <<default>>
    <iframe...></iframe>
  <</switch>>
<</nobr>>

In the above code, we use the <<nobr>> tag to suppress line breaks. We then use the <<switch>> macro to evaluate the random() function, which we've set to return a random integer between 0 and 2. If the number comes back 0, it trips the <<default>> clause (sort of like an else) and that <iframe> is shown. If it comes back 1, that <iframe> is shown, etc. 

Some other notes: 

You could also probably assign the iframes classes in your css to handle the styling so you don't need to rewrite all the style rules every time. It's also possible to use a bit of jQuery to randomize just the src attribute of your iframes so you don't need to rewrite it at all, but I'm not sure it'd provide any benefit outside of that, and isn't really necessary. 

All things being equal, I'd use the <<switch>> if you can, as saving on the variable and using the more succinct syntax is probably worth it. 

Edit. 

Moved <<default>> to the end. Sorry about that. 

by (1.4k points)
edited by

Thank you Chapel!

The first try gave an error, saying the default case must be last; so I moved it to last and it works perfectly.

Here is the story I showed you before, now translated from Harlowe to Sugarcube: and very glad I am that I did it.

by (159k points)

@Patrick Lauser

I don't know if you have access to a blog (or the like) but if you do you may want to do a write up on the experience and efforts you took to convert a Harlowe based project to a SugarCube based one, including any problems you had and the workarounds you did.

This would help others that may be also thinking of doing the same conversion.

by (63.1k points)
I'm seeing errors for your audio macros at the bottom of the initial screen for your story.
by (1.4k points)

you may want to do a write up on the experience

Yes indeed, I am in process of creating a site which will definitely involve my Twine stories and how I made them.

 I'm seeing errors for your audio macros at the bottom of the initial screen for your story.

 Oh yes, hee hee. I forgot to sweep. I was copying and pasting these:

<<cacheaudio "" "">>
<<audio "" play>>

and pasting urls and titles into them, and left a few empty ones lying about. I remembered this morning and cleaned them up. All tidy now!

I also added an image link below the video which plays in one of the rooms, remembering that the undo button in Harlowe was the only way to leave that spot before.

...