Scripting Baby Steps


#1

I’ve never really got that deep into coding in JavaScript, but since that’s the language of HiFi, I’m starting to work out getting things going. I did quite a bit of LSL scripting in SL, and cut my teeth on BASIC (the old school MS Basics), and have done some assembly in Z80, x86 and 6809. So, now that JavaScript is on the table, I need to work that out. Overall, coding is coding, and it’s mostly a matter of syntax when moving from language to language, especially these days since virtually everything is these days is based on C. (Java, Flash Action Script, even LSL). So, I decided to take a stab at something here… something small, a baby step, to get started. From there, I’ll move on to more complex stuff and work my way up.

To get started, after browsing the wiki page to look at what functions we have to play with, I decided to write a small script that would change my display name every second so that it would ready “My”, “Name”, “Is”, “John” over and over. Nothing mind blowing, but something that runs without throwing any errors and does something were the requirements for this first “baby step”. Here’s the code if anyone wants to try it out.

var i=0;

function loopTimer()
{
    if (i == 0)
    {
        MyAvatar.displayName = "My";
        i++;
        return;
    }

    if (i == 1)
    {
        MyAvatar.displayName = "Name";
        i++;
        return;
    }

    if (i == 2)
    {
        MyAvatar.displayName = "Is";
        i++;
        return;
    }

    if (i == 3)
    {
        MyAvatar.displayName = "John";
        i++;
        return;
    }
    if (i == 4)
    {
        i = 0;
    }
}

var multiple_timer = Script.setInterval(loopTimer,500);

If you detach the script when your name isn’t displayed it’ll be “stuck” on My, Name, or Is, to fix it you can go into preferences and reset it, or to avoid it, close the script when your name is showing.


#2

Hey there, I’ve modded your script a little:

var i = 0;
var text = "My name is very long.... And the story goes on and on.. Are you AFK? I might be....";
var forwards = true;
var delay = 1500; // setting this lower might cause other people not to see it update quick enough
var message_length = 16; //set this to make your name ticker wider or narrower

function loopTimer() {
    var tickertext = "";
    for (var ib = 0; ib < text.length && ib < message_length; ib++) {
        tickertext += text[(i + ib) % text.length];
    }
    MyAvatar.displayName = tickertext;
    if (forwards) {
        i = (i + 1) % text.length;
    } else {
        i = (i - 1) % text.length;
        if ( i < 0 ) {
            i = text.length - 1;
        }
    }
}
var multiple_timer = Script.setInterval(loopTimer, delay);

Enjoy :smile:


#3

The ticker idea occurred to me to, and I planed on writing it eventually, but thoys saw my script and beat me to it… and did a great job of it too. :wink: