salahzar icon

20022008 Lezione Avanzata 1. Interruttore per accendere

salahzar | PRO | 02/16/20 07:55:46 AM UTC | 0 ⭐ | 882 👁️ | Never ⏰ | []
text |

6.92 KB

|

None

|

0 👍

/

0 👎

Lezione avanzata 1.
 * Interruttore per accendere/spegnere luci
* aprire/chiudere porte
* aprire/chiudere finestre
* telecomandi e teleport
 1.1. Lo scopo di queste lezioni intermedie è di offrirvi le capacità di poter scriptare degli
oggetti per cose che vi servono effettivamente, quindi come supporto alle vostre
attività di costruzione. Ecco perchè ho riunito insieme queste cose in questo corso
della durata di circa 1 ora.
 1.2. Convenzioni:
 Per esigenze di pulizia formale io uso le seguenti convenzioni:
 * le variabili di norma sono prefissate con il tipo (i, l, s, v, f, r) integer, list, string, vector, float, rotation), es. iPippo,fNumber...
* le variabili globali (a parte il prefisso) sono tutte MAIUSCOLE, es. iTIMER...
* le graffe le metto su nuova linea per chiarezza di dove iniziano e finiscono
if(x==1)
{
    <a>
}
else
{
    <b>
}
 1.3. Debug
* uso sempre una funzione debug(string str)
debug(string str)
{
    if(iDEBUG==1) llSay(10,str);
}
l'uso del canale 10 è legato al fatto che non disturba gli altri in modalità debug. Per "leggere" il debug di un oggetto siffatto basta scriptare quanto segue:
 * debugger (lo trovate nella box)
default
{
    state_entry()
    {
        llListen(10, "",NULL_KEY,"");
    }
    listen(integer channel,string objname,key id,string str)
    {
        llOwnerSay("DEBUG obj: "+objname+": "+str);
    }
}
 1.4. Comunicazione
 In comune queste attività hanno il fatto che gli oggetti si parlano "a distanza". Questo
vuol dire che 2 o più prim possano scambiarsi informazioni attraverso dei canali di 
comunicazione.
 Parlare vuol dire che l'oggetto A vuole mandare un messaggio M all'oggetto B, che lo "sente" e di conseguenza esegue determinate azioni, eventualmente rispondendo 
all'oggetto A. 
 Vi sono molti modi ma i più semplici sono fondamentalmente 2: parlarsi con i canali Say, oppure parlarsi con i canali linked, se gli oggetti sono linkati assieme. 
 1.5. Come si fa a parlarsi con i canali Say:
 Gli oggetti A e B si devono mettere d'accordo su un canale comune, possibilmente non utilizzato da altri oggetti. Il canale di solito è:
1.5.1.  predefinito nei programmi
1.5.2. selezionato attraverso una notecard
1.5.3. ottenuto indirettamente dal nome dell'oggetto
1.5.4. concordato attraverso una procedura di handshake
 Noi utilizzeremo inizialmente il sistema 1.5.1, vale a dire specificheremo un canale prefissato.
 1.6 Esempio di trasmissione elementare
Oggetto A (Trasmittente):
integer iCHANNEL=-3000; // negativo per non essere utilizzato da avatars
integer iDEBUG=1; // abilita debug
debug(string str)
{
    if(iDEBUG==1) llSay(10,str);
}
default
{
    state_entry()
    {
        debug("Talking to channel "+(string)iCHANNEL+" click me to make me sending a message");
    }
    touch_start(integer count)
    {
        llSay(iCHANNEL,"Here I am");
    }
}
Oggetto B (ricevente):
integer iCHANNEL=-3000; // negativo per non essere utilizzato da avatars
integer iDEBUG=1; // abilita debug
debug(string str)
{
    if(iDEBUG==1) llSay(10,str);
}
default
{
    state_entry()
    {
        llListen(iCHANNEL,"",NULL_KEY,"");
        debug("Listening  to channel "+(string)iCHANNEL);
    }
    listen(integer channel,string objname,key id,string str)
    {
        if(objname=="A")
        {
            llSay(0,"Receiving "+str+" from "+objname);
        }
    }
}
 1.7 Finestra che cambia la sua trasparenza
Ora usiamo questo sistema per fare in modo che l'oggetto B (che rappresenta una finestra possa cambiare la sua trasparenza quando riceve 
da A un comando opportuno. Usiamo un dialogo per controllare il livello di trasparenza.
 Oggetto A (trasmittente)
integer iCHANNEL=-3000;
integer iDIALOG=-3001; // canale per llDialog
integer iDEBUG=1;
integer iLISTEN=0;
 debug(string str) {}
default
{
    touch_start(integer count)
    {
        if(iLISTEN!=0) llListenRemove(iLISTEN);
        key avatar=llDetectedKey(0);
        debug("touched, presenting menu");
        llDialog(avatar, "Choose transparency",["0%","50%","100%"],iDIALOG);
        iLISTEN=llListen(iDIALOG,"",avatar,"");
    }
    listen(integer channel,string name,key id,string str)
    {
        debug("received the command "+str+" transmitting to the window");
        llListenRemove(iLISTEN);
        iLISTEN=0;
        llSay(iCHANNEL,str);
    }
}
 Oggetto B(finestra)
integer iCHANNEL=-3000;
integer iDEBUG=1;
debug()....
default
{
    state_entry()
    {
        debug("Listening to channel "+(string)iCHANNEL);
        llListen(iCHANNEL,"",NULL_KEY,"");
    }
    listen(integer channel,string name,key id,string str)
    {
        debug("Received command: "+str);
        if(str=="0%") llSetAlpha(1,ALL_SIDES);
        if(str=="50%") llSetAlpha(0.5,ALL_SIDES);
        if(str=="100%") llSetAlpha(0,ALL_SIDES);
    }
}
 1.9.A Usando il metodo MessageLinked
Oggetto A (trasmittente)
integer iCHANNEL=-3000; // questo è privato al linkset
integer iDIALOG=-3001; // canale per llDialog
integer iDEBUG=1;
integer iLISTEN=0;
 debug(string str) {}
default
{
    touch_start(integer count)
    {
        if(iLISTEN!=0) llListenRemove(iLISTEN);
        key avatar=llDetectedKey(0);
        debug("touched, presenting menu");
        llDialog(avatar, "Choose transparency",["0%","50%","100%"],iDIALOG);
        iLISTEN=llListen(iDIALOG,"",avatar,"");
    }
    listen(integer channel,string name,key id,string str)
    {
        debug("received the command "+str+" transmitting to the window");
         llListenRemove(iLISTEN);
        iLISTEN=0;
        llMessageLinked(LINK_SET, iCHANNEL, str, "");
    }
}
1.9.B Oggetto B(finestra)
integer iCHANNEL=-3000;
integer iDEBUG=1;
debug()....
default
{
    state_entry()
    {
    }
    link_message(integer sender, integer num, string str, key id)
    {
        if(num!=iCHANNEL) return;
        debug("Received command: "+str);
        if(str=="0%") llSetAlpha(1,ALL_SIDES);
        if(str=="50%") llSetAlpha(0.5,ALL_SIDES);
        if(str=="100%") llSetAlpha(0,ALL_SIDES);
    }
}
 1.10 Come si vede i due sistemi sono analoghi e presentano vantaggi e svantaggi:
 Vantaggi dei canali Say:
 * gli oggetti si possono parlare anche se "molto" distanti (96m),
* sono abbastanza facili da programmare
 Svantaggi dei canali Say:
 * se altri oggetti usano gli stessi canali possono entrare in conflitto
* producono in generale lag
 1.11. Vantaggi dei canali MessageLinked:
 * sono molto veloci
* sono "riservati" (non ci sono problemi di collisione)
 Svantaggi dei canali MessageLinked
 * agiscono solo fra oggetti molto vicini e linkati
* sono un po' più difficili da programmare
 1.12. Esercizi da farsi a casa:
 * interruttore che accende / spegne una luce.  (si può usare llSetPrimitiveParams([PRIM_POINT_LIGHT, TRUE, <1, 1, 1>, 1.0, 10.0, 0.75] );
* potete progettare uno "slider" per controllare in modo regolare la luce o l'apertura delle finestre

Comments