Don't click here unless you want to be banned.

LSL Wiki : LibraryChannelNumberGenerator

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are crawl338.us.archive.org
Do you have a tough time choosing and remembering unique channel numbers? I did, so I wrote a script to do it for me, kind of like domain names on the Internet represent IP addresses.

Sometimes I use the object name as the string, sometimes the script name; it all depends. The choice is yours.

I keep my functions and their "test drivers" in scripts in a function library folder. Because LSL has no #include or anything, I copy just the comment and the function into other scripts.

//
// generateChannel(): Given a text string, generate a (relatively) unique channel number.
// This conversion is repeatable, so you can use it to easily communicate with a script by
// remembering a text string instead of a channel number.
//
// You can copy or modify this script if you give me credit.
// Petre Lamar, 2006-07-31
//
integer generateChannel(string text)
{
    list hexDigits = ["dummy", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"];
    string md5 = llMD5String(text, 0);
    integer length = llStringLength(md5);
    integer channelNumber = 1; // zero makes a bad multiplier :-)
    integer hexValue;
    string character;
        
    for (length--; length >= 0; length--)
    {
        character = llGetSubString(md5, length, length);
        hexValue = llListFindList(hexDigits, [ character ]);
        channelNumber *= hexValue;
        if (channelNumber == 0)
        {
            // zero times anything remains zero, so force it to one
            channelNumber++;
        }
    }

    return channelNumber;
}

default
{
    state_entry()
    {
        llWhisper(0, "Type something and I'll convert that into a channel number!");
        llListen(0, "", llGetOwner(), "");
    }

    listen(integer channel, string name, key id, string message)
    {
        integer result = generateChannel(message);
        llWhisper(0, "'" + message + "' yields '" + (string)result + "'");
    }
}
There is no comment on this page. [Display comments/form]