How to use "either" within "timed" ? And more [Sugarcube 2]

0 votes
asked Oct 9 by Seyru (560 points)

Hello there !

Still working on my own, and I'm facing an issue : how do you use "either" with a "timed" ? (I'd like the time of the timed to change randomly)

I've tried out stuff like 


<<set _random to either("1","2","3")>>

<<timed _random s>> 

But it didn't work, the "s" isn't taken into account. I've tried putting them together and it wasn't a success either. I've tried to use either without a variable, and I am still having the same issue. 


<<timed either("1","2","3")s>> 

Doesn't work :(


Another question : with a <<repeat>> macro, is it possible to make the loop start immediatly ? 
As it is, if I write <<repeat 4s>>, the loop will begin after 4s, whereas I'd want it to start right now ! 


And last question, is there a way to count how many words there are within the sentence of an array ? For instance, if I have 

$sentences = ["My lovely holidays", "My oh so lovely holidays"] 

is there a way for me to get as an output the number of words of $sentences[0] ? 


Thanks a lot guys, you're doing a marvellous work !



1 Answer

0 votes
answered Oct 9 by TheMadExile (14,870 points)
selected Oct 9 by Seyru
Best answer

TIP: Generally, you ask one question per Question & Answer thread.  Though, a multifaceted question and/or questions which are closely related are fine.

1. How to use either() with <<timed>>.

You need to concatenate the string "s" to the value returned by either():

<<set _random to either("1","2","3") + "s">>\
<<timed _random>>

Unless you need the _random temporary variable later, you could simply things by using a backquote expression right within the <<timed>> so that you don't need the variable:

<<timed `either("1","2","3") + "s"`>>


2. Can you start a <<repeat>> immediately?

No.  Though you may simply repeat the code once before the <<repeat>> to achieve the same effect.  For example

/* code (initial run) */
<<repeat 4s>>
	/* code (repeating run) */

If the code in question is lengthy, consider turning it into a widget to keep things tidier.

ALSO: You called <<repeat>> a loop, which is incorrect.  While it does loop, in the sense that it repeats, it is not a proper loop (see: <<for>> if you actually need a loop).


3. Counting words.

Until you know you need something more complex, I'd try something simple like the following, which splits the string on whitespace and returns the length of the resulting array:



commented Oct 9 by Seyru (560 points)
Thanks a lot !
I didn't want to open too many Q&A, but if it's easier for you guys I'll do so from now on :D