Using a Reading Speed Variable in the "Timed" Macro - Sugar Cube 2.31.1


New to this. Really enjoying it! I’m working on my first Twine project. I’m working in Sugar Cube 2.31.1.

I have a lot of text that fades in over the course of each passage.

I created “Reading Speed” variable, which detemines how fast the user reads as words/second ($wps). I’d like to then apply this to individual lines of text by multiplying $wps by the number of words as they build up over a passage.

<<timed ($wps*4) t8n)>>The previous passage has 4 words.<</timed>>

So a passage might look something like this:

This is the start of the passage.<<timed ($wps*7)>>Then a new sentence appears.<</timed>><<timed ($wps*12)>>And another one.<</timed>><<timed$wps*15>>That's the idea.<</timed>>

Only the <<timed>> macro only recognises numbers.

Is there a way I can convert the function into a number that works with the macro?

Many thanks! Ben

<<= "<<timed +"+($wps*7)+"s>>Then a new sentence appears.<</timed>>">>

Thanks Egas! It still doesn’t like the syntax. I tried out what you posted as well as a few variations. Any other thoughts would be most welcome. All the best, Ben

If you take a look at the “Argument type macros: passing an expression as an argument” section of the SugarCube documentation, it shows how you can use backquotes (found on the ~ key on most keyboards) around an expression to pass the value of that expression. For example:

<<timed `($wps * 7) + "s"`>>...<</timed>>

If $wps was set to 0.25, for example, then that would be the same as doing:

<<timed 1.75s>>...<</timed>>

So, that should do the trick.

Have fun! :smiley:

That’s the one. Of course my math was backwards given the way I had calculated wps. It was taking ages for the next sentence to load. Because the actual calculation should be: 7/$wps

Well. it works on my twine 2 project… not sure why it does not work on yours.

The calculation works either way. If $wps is set to 4 then ($N / $wps) is the same as ($N * $wps) when $wps is set to 0.25 (a.k.a. 1/4th).

The reason why is that (1/4) * $N is the same as $N/4.

So you can do it either way.

Why not use the <<timed>> macro’s <<next>> tag, rather than multiple instances of <<timed>>?

Unless your actual code is different from the example you gave, then that would be better for various reasons. Not the least of which is that you’d only need to calculate the delay between lines, if it wasn’t static, rather than figuring out each individual <<timed>> invocation’s delay. You’d also only be running a single timer, rather than one for each <<timed>>.

Can I add a variable time to the next tag? EG
First line of text has seven words.<<timed ($wps * 7) + "s">>Four words of text<<next ($wps * 4) + "s">>Two words<<next ($wps * 2) + "s">>The end<>

Thanks! Ben

The answer really hasn’t changed since HiEv pointed you at the appropriate section of the docs and provided examples.

Yes, you may use a variable. Also yes, since your variable holds a number and you need a time value instead, you may use a backquote expression to make the latter from the former.

For example:

<<timed `($wps * 7) + "s"`>> …
<<next `($wps * 4) + "s"`>> …
<<next `($wps * 2) + "s"`>> …

FYI - He was using the backquote in his example, you just can’t see it properly because he didn’t wrap the code in a preformatted text block.