Ok, once again, just a test script.

Posted by Stratus on Wed 04 Sep 2002 08:59 PM — 11 posts, 34,773 views.

#0
I wanted to try out a couple of scripts, first off is there anyway that I could make a script to tell someone the current time like this?

function OnTime (thename, theoutput, thewildcardsVB)
{
var Name
var Time
Name = world.GetTriggerInfo (thename, 101)
Time = ??
world.send ("\"Hello, " + Name + ". The current time is: " + Time + ".")
} //end of OnTime

And second, I wanted to make a test script that would send, "Hello, I will repeat this message X times.", three times. But each time change the X to how many times are left. Is there any way to do this without just writing that three times?
USA #1
The first problem... I am not sure.. I believe most scripting engines provide a way to do it, but I haven't a clue how in java.

As for the second problem.. Easiest way would be something like this:

function Repeat(name, output, wildcards)
{
  var Wilds = VBArray(wildcards).toArray();
  var Rep = int Wilds[1];
  if (Rep > 0) {
    world.send("\"Hello, I will repeat this message " + Rep + " times.");
    Rep = Rep - 1;
    Repeat (" "," ",Rep)
  }
}

or even easier (and probably more likely to work):

function Repeat(name, output, wildcards)
{
  var Wilds = VBArray(wildcards).toArray();
  int Rep = int Wilds[1];
  int x;
  for(x=Rep; x>0; x = x-1)
    world.send("\"Hello, I will repeat this message " + x + " times.");
}

Note that there is 'no' certainty this will work. The first one uses recursion, which may not work with scripts. Also, I know next to nothing about proper Jscript, so I can't be sure either one works. ;) lol Both allow you to send the script a number through the alias that determines how many times it will repeat. If you don't give it one, they just exit.

Another method would have been to use a timer and a mushclient variable to do the same thing every second or so,however if your intent is to merely send something a set number of times, without a delay in between, then one of the two above should do it. Assuming I didn't goof something of course. ;)
Amended on Wed 04 Sep 2002 10:03 PM by Shadowfyr
#2
Ok, I just tried both of those and neither of them worked.
USA #3
I did say I hadn't a clue how to code in Jscript. lol

The structure and concept is more or less right, but I probably got a bit of the syntax wrong.
Amended on Wed 04 Sep 2002 10:40 PM by Shadowfyr
#4
Nick help me! lol
Australia Forum Administrator #5
Show the date and time:


world.note ("The time is now " + Date().toString ());


Repeating a message:


for (x = 3; x >= 1; x--)
 world.note ("Hello, I will repeat this message " + x + " times.");
USA #6
Hmm. Ok. But what did I do wrong in my versions. lol I like to know these things so I don't stick my foot in my mouth the next time around. ;) lol

Would be interesting to know how scripting handles recursion as the very least.
Australia Forum Administrator #7
The recursion didn't work because on the 2nd call (first recurse) you were no longer passing down an array, here ...

Repeat (" "," ",Rep)

Rep was a scalar, but in the function you called VBArray(wildcards).toArray();

Too complex anyway, for doing something repeatedly. Your other solution was close, this works ...


function Repeat(name, output, wildcards)
          {
            var Wilds = VBArray(wildcards).toArray();
            var Rep = Wilds[0];
            var x;
            for(x=Rep; x>0; x = x-1)
              world.send("\"Hello, I will repeat this message " + x + " times.");
          }



I had to get rid of the "int" cast, and change "int" to "var". Also the first array item is 0, not 1.

A smaller version would be:


function Repeat(name, output, wildcards)
  {
  Wilds = VBArray(wildcards).toArray();
  for (x = Wilds[0]; x > 0; x--)
     world.send("\"Hello, I will repeat this message " + x + " times.");
  }
Portugal #8
or as i rather like to do you can do an up counter FOR cicle instead of a down counter.


function Repeat(thename, theoutput, wildcards)
{

wildcards = VBArray(wildcards).toArray();

times = wildcards [0]

for (i = 1 ; i <= times ; i++)
world.send("\"Hello, I will repeat this message " + times + " times.");
}
Amended on Thu 05 Sep 2002 03:49 PM by Avariel
USA #9
Thanks Nick.. Didn't think about the variable being a scalar instead of an array there. Still, wonder if it could work if you did pass the correct type... Hard to say with scripting.

Sorry Avariel, you perhaps didn't read all the thread? He was asking that the <times> in there reflect how many times it was going to continue to repeat. ;)
Portugal #10
oops i miss the line 'But each time change the X to how many times are left.' :/ sorry guys.