Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

ok so im new and all

Harminoff Hadlee
Registered User
Join date: 13 Jul 2004
Posts: 35
07-19-2004 19:33
im trying to make a script that creates a random number, the see's if it = user input.

I always start out with this program when i learn a new language, but im really confused with this one.

its really ugly code, as im not good at making code look good. and its also prob. really screwed up. i cant find any good tutorials for newbies that teach you a lot.


    [CODE]
    default

    {
    state_entry()
    {
    llSay(0, "play me!";);
    }

    touch_start(integer total_number)
    {

    integer user = llListen( 0, "", NULL_KEY, "" );
    llSay(0, "Welcome to guess the number.";);
    llSay(0, "input first number.";);
    float random = llFrand(100);
    integer Number = llRound(random);
    string saynumber = (string) Number;
    string guess = (string) user;
    llSay(0, saynumber);
    if (guess == saynumber);
    llSay(0,"nice";);


    }



    }

    [/CODE]


    ok its ugly i know, but im a newb :)

    i have it display the nubmer first so that i know what to type to check it.

    i know that my if statement is messed, im just used to using if then statements.
Ama Omega
Lost Wanderer
Join date: 11 Dec 2002
Posts: 1,770
07-19-2004 20:10
Good (best?) resource:
www.badgeometry.com/wiki

In particular for your script check out
www.badgeometry.com/wiki/tutorial
www.badgeometry.com/wiki/listen

You are mostly on the right track but you need to set up a listener (so the script knows it is listening to chat, and what to listen to) and put most of your code in the listen event which will give you the chat said.
_____________________
--
010000010110110101100001001000000100111101101101011001010110011101100001
--
Christopher Omega
Oxymoron
Join date: 28 Mar 2003
Posts: 1,828
Re: ok so im new and all
07-19-2004 20:14
From: someone
Originally posted by Harminoff Hadlee

CODE

default

{
state_entry()
{
llSay(0, "play me!");
}

touch_start(integer total_number)
{

integer user = llListen( 0, "", NULL_KEY, "" );
llSay(0, "Welcome to guess the number.");
llSay(0, "input first number.");
float random = llFrand(100);
integer Number = llRound(random);
string saynumber = (string) Number;
string guess = (string) user;
llSay(0, saynumber);
if (guess == saynumber);
llSay(0,"nice");


}



}



The main problem with your code is its use of llListen. LSL is an event-oriented language, not many of its functions block the script. This rule espicially goes for functions that seemingly take in user input.

What I think your doing is expecting llListen to return a value when the user says something.

Here's an example of proper llListen usage:
CODE

default {
// When the state is entered:
state_entry() {
// Ask the server that this script wants
// to be notified of user input that matches the constraints.
llListen(0, "", NULL_KEY, "");
// The above line translates to:
// Trigger the listen event when any thing (object or avatar),
// with any name or UUID, chats anything on channel 0.
// Channel 0 is the avatar chat channel.
}

// When the script "hears" something:
listen(integer channel, string name, key id, string message) {
llSay(0, "Heard " + message);
}
}


The majority of your code is correct, but slightly redundant. Here's the code to do what you want:
CODE

integer listenHandleID = -1;

integer randomNumber = 0;
default {
state_entry() {
llSay(0, "Play me!");
}
touch_start(integer numTouchers) {
// Remove the old listen:
llListenRemove(listenHandleID);
// Listen to the person who touched the object:
listenHandleID = llListen(0, "", llDetectedKey(0), "");
// Generate the random number:
randomNumber = llRound(llFrand(100));
llSay(0, "Welcome to guess the number.");
llSay(0, "Input first number.");
}

listen(integer channel, string name, key id, string message) {
// Converts message to an integer.
integer userGuess = (integer) message;

if (userGuess == randomNumber) {
llSay(0, "Nice");
}
}
}
Harminoff Hadlee
Registered User
Join date: 13 Jul 2004
Posts: 35
07-19-2004 20:58
ok works great. next thing, how do you add a kill command so that they cant just type in the same number over and over, they got to click and restart?
Christopher Omega
Oxymoron
Join date: 28 Mar 2003
Posts: 1,828
07-19-2004 21:02
From: someone
Originally posted by Harminoff Hadlee
ok works great. next thing, how do you add a kill command so that they cant just type in the same number over and over, they got to click and restart?


Remove the listen when the user gets the answer right.