Jump to content
  • 0

Sincronizar dos cronometros


Irene Muni
 Share

You are about to reply to a thread that has been inactive for 5180 days.

Please take a moment to consider if this thread is worth bumping.

Question

Hola tod@s.

Enttre las muchas cosas de SL de las que no tengo ni idea está el LSL. Así que no sé si lo que voy a preguntar es algo muy elemental o es algo dificilísimo o imposible. Pero allí voy.

Me gustaría saber si puedo sincronizar dos cronómetros. Me explico.

  • Yo tengo un objeto "A" con el script que pongo abajo. Cuando alguien lo toca empieza a correr el cronómetro y cuando se vuelve a tocar se para (y si se escribe "cero" en el chat el cronómetro se pone a cero).
  • Lo que me gustaría es tener a cierta distancia otro objeto "B" que estuviera sincronizado con "A". Que cuando alguien tocara "A" empezara a correr el cronómetro no sólo en "A" sino también en "B". Y que cuando luego alguien tocara en "B" el cronómetro también se parara en "A".

¿Eso es posible?

Gracias adelantadas. Copio aquí abajo el script opensource que uso:

//  StopWatch
//  Created by Water Rogers for IBM/Opensource

//  Purpose
//  --------------------------------------------------------------
//  This is a basic example of how you can create a stopwatch.
// 

//  Requirements
//  --------------------------------------------------------------
//  A single prim is all that is necessary for this example.

//  Usage
//  --------------------------------------------------------------
//  Touch the object to toggle the timer off/on
//  type "cero" to reset the timer. Not case-sensitive.

//  GLOBAL VARIABLES
//  --------------------------------------------------------------

integer g_Seconds   = 0;        //  Globaly store the Seconds
integer g_Minutes   = 0;        //  Globaly store the Minutes
integer g_Hours     = 0;        //  Globaly store the Hours
integer g_Ticking   = FALSE;    //  Toggles the timer off/on
vector  g_TextColor = <1,1,1>;  //  Text color for the timer. <1,1,1> = White


//  FUNCTIONS
//  --------------------------------------------------------------

//  This is a simple function to zero pad the numbers 2 spaces to make
//  the timer look more authentic.  It takes an integer as an argument
//  and outputs a string.
string zero_pad(integer number)
{
    if(number < 10) return "0" + (string)number;
    return (string)number;
}


//  EVENTS
//  --------------------------------------------------------------

default
{
    state_entry()
    {
        //  ------------------------------------------------------
        //  This is the entry-point of the script.  After a script
        //  has been saved or reset, this event will fire off first
        //  ------------------------------------------------------
       
        //  We call llSetText() first to reset the text above the object
        //  back to 00:00:00 time.
        llSetText("00:00:00", g_TextColor, TRUE);
       
        //  Set up a listener so that we can reset the timer by typing
        //  "reset".  This particular listener will listen to any chat
        //  typed by anyone (or any object)
        llListen(0, "", "", "");
       
        //  Finally, we set up a timer that will repeat itself every
        //  g_Ticking seconds.  Since g_Ticking could only ever possibly
        //  equal 0 or 1, we can use this to our advantage in optimizing.
        //  This particular example is not necessary, but can have a
        //  dramatic impact on larger scripts.  Note that setting a timer
        //  to 0 (False) will indeed turn off the timer.
        llSetTimerEvent(g_Ticking);
    }
   
    touch_start(integer num_detected)
    {
        //  If someone touches the object, it will toggle the timer off/on
       
        //  We can avoid using if/else statements here because of the
        //  nature of our timer.  Using an exclamation in front of the
        //  variable "flips" the bits.  In this case, our variable is either
        //  only ever TRUE (1) or FALSE(0).  Since we need the timer to
        //  tick each second, this becomes a nice optimization method.
        g_Ticking = !g_Ticking;
        llSetTimerEvent(g_Ticking);

    }
   
    listen(integer channel, string name, key id, string message)
    {
        //  This event is fired off whenever anyone chats around the object
        //  and a proper listener is set up, such as in state_entry()
       
        //  We take the message and make it all lowercase so that the word
        //  "reset" is not case-sensitive
        message = llToLower(message);
        if(message == "cero")
        {
            //  The filter determined that the word "reset" was typed.  So
            //  we need to reset everything back to 0, and turn the timer off
            g_Ticking = FALSE;
            llSetTimerEvent(0);
            llSetText("00:00:00", g_TextColor, TRUE);
            g_Seconds = 0;
            g_Minutes = 0;
             g_Hours;
        }
    }
       
    timer()
    {
        //  This event fires off each second because the integer 1 is being
        //  passed as the argument for seconds in llSetTimerEvent(float seconds)
       
        //  Incriment the global Seconds variable by 1
        g_Seconds++;
       
        //  If seconds are at 60, then we've just made a minute
        if(g_Seconds >= 60)
        {
            //  So we increment the global minutes by 1, and reset the seconds.
            g_Minutes++;
            g_Seconds = 0;
           
            //  If the minutes are at 60, then we've just made an hour
            if(g_Minutes >= 60)
            {
                //  Increment the global hours by 1, and reset the minutes.
                g_Hours++;
                g_Minutes = 0;
            }
        }
       
        //  Display everything above the object.  Notice the use of the zero_pad()
        //  function that we created earlier to make the timer look better.
        llSetText(zero_pad(g_Hours) + ":" + zero_pad(g_Minutes) + ":" + zero_pad(g_Seconds), g_TextColor, TRUE);
    }
}

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

Fijate si te sirve esto:

Con unos cambios en el código para hacer que cada objeto envie un mensaje al otro cuando hay un cambio de estado (es decir, cuando es tocado), el listen espera tres mensajes:

"Activar" para comenzar el conteo

"Apagar" para detenerlo sin reiniciar el contador

"cero" para reiniciar el contador

Los dos primeros son enviados por cada objeto y el ultimo se envia por el chat en el canal definido. Lo ideal para evitar generar mas lag, es no usar el canal cero (público) porque haría que el listen este atento a todo lo que se diga, por eso, se ha definido otro canal. En las pruebas que he realizado funcionó perfecto, pero seguramente es perfectible y optimizable el código.

 

//  StopWatch
//  Created by Water Rogers for IBM/Opensource
//
//  Purpose
//  --------------------------------------------------------------
//  This is a basic example of how you can create a stopwatch.
// 
//
//  Requirements
//  --------------------------------------------------------------
//  A single prim is all that is necessary for this example.
//
//  Usage
//  --------------------------------------------------------------
//  Touch the object to toggle the timer off/on
//  type "cero" to reset the timer. Not case-sensitive.
//
//  GLOBAL VARIABLES
//  --------------------------------------------------------------
//
integer g_Seconds   = 0;                  //  Globaly store the Seconds
integer g_Minutes   = 0;                    //  Globaly store the Minutes
integer g_Hours     = 0;                       //  Globaly store the Hours
integer g_Ticking   = FALSE;        //  Toggles the timer off/on
vector  g_TextColor = <1,1,1>;  //  Text color for the timer. <1,1,1> = White
integer Canal    = 1000;                  //  Canal de recepción comunicacion entre objetos


//  FUNCTIONS
//  --------------------------------------------------------------
//
//  This is a simple function to zero pad the numbers 2 spaces to make
//  the timer look more authentic.  It takes an integer as an argument
//  and outputs a string.
//
string zero_pad(integer number)
{
   if(number < 10) return "0" + (string)number;
   return (string)number;
}

//  EVENTS
//  --------------------------------------------------------------
//
default
{
     state_entry()
     {
             //  ------------------------------------------------------
             //  This is the entry-point of the script.  After a script
             //  has been saved or reset, this event will fire off first
             //  ------------------------------------------------------
       
             //  We call llSetText() first to reset the text above the object
             //  back to 00:00:00 time.
             llSetText("00:00:00", g_TextColor, TRUE);
       
             //  Set up a listener so that we can reset the timer by typing
             //  "reset".  This particular listener will listen to any chat
             //  typed by anyone (or any object)
             llListen(Canal, "", "", "");
       
             //  Finally, we set up a timer that will repeat itself every
             //  g_Ticking seconds.  Since g_Ticking could only ever possibly
             //  equal 0 or 1, we can use this to our advantage in optimizing.
             //  This particular example is not necessary, but can have a
             //  dramatic impact on larger scripts.  Note that setting a timer
             //  to 0 (False) will indeed turn off the timer.
             llSetTimerEvent(g_Ticking);
     }
   
     touch_start(integer num_detected)
     {
             //  If someone touches the object, it will toggle the timer off/on
       
             //  We can avoid using if/else statements here because of the
             //  nature of our timer.  Using an exclamation in front of the
             //  variable "flips" the bits.  In this case, our variable is either
             //  only ever TRUE (1) or FALSE(0).  Since we need the timer to
             //  tick each second, this becomes a nice optimization method.
             g_Ticking = !g_Ticking;
             llSetTimerEvent(g_Ticking);
             if(g_Ticking)  llRegionSay(Canal, "ACTIVAR" );
             if(!g_Ticking) llRegionSay(Canal, "APAGAR" );
           }
   
     listen(integer channel, string name, key id, string message)
     {
             //  This event is fired off whenever anyone chats around the object
             //  and a proper listener is set up, such as in state_entry()
       
             //  We take the message and make it all lowercase so that the word
             //  "reset" is not case-sensitive
             message = llToLower(message);
             if(message == "cero")
             {
                     //  The filter determined that the word "reset" was typed.  So
                     //  we need to reset everything back to 0, and turn the timer off
                     g_Ticking = FALSE;
                     llSetTimerEvent(0);
                     llSetText("00:00:00", g_TextColor, TRUE);
                     g_Seconds = 0;
                     g_Minutes = 0;
                      g_Hours;
             }
             else if(message == "apagar")
             {
                     //  The filter determined that the word "reset" was typed.  So
                     //  we need to reset everything back to 0, and turn the timer off
                     g_Ticking = FALSE;
                     llSetTimerEvent(0);
             }
             else if(message == "activar")
             {
                     g_Ticking = TRUE;
                     llSetTimerEvent(g_Ticking);
             }  
      }
       
      timer()
      {
               //  This event fires off each second because the integer 1 is being
               //  passed as the argument for seconds in llSetTimerEvent(float seconds)
       
               //  Incriment the global Seconds variable by 1
               g_Seconds++;
       
                //  If seconds are at 60, then we've just made a minute
                if(g_Seconds >= 60)
                {
                          //  So we increment the global minutes by 1, and reset the seconds.
                          g_Minutes++;
                          g_Seconds = 0;
           
                //  If the minutes are at 60, then we've just made an hour
                          if(g_Minutes >= 60)
                          {
                                   //  Increment the global hours by 1, and reset the minutes.
                                   g_Hours++;
                                   g_Minutes = 0;
                          }
                }
       
                //  Display everything above the object.  Notice the use of the zero_pad()
                //  function that we created earlier to make the timer look better.
                llSetText(zero_pad(g_Hours) + ":" + zero_pad(g_Minutes) + ":" + zero_pad(g_Seconds), g_TextColor, TRUE);
       }
}
Link to comment
Share on other sites

  • 0

Bienvenido sea que te haya servido.

Supongo te habras dado cuenta pero por las dudas te comento que al utilizar "llRegionSay" para pasarse los datos entre si, hace posible que puedas colocar ambos cronómetros en cualquier parte del sim sin problema.

He basado la lógica en un script que escribí para encender y apagar las luces de mi parcela en forma automática según sea de día o de noche (en SL, este cambio dia/noche se produce 4 veces durante un dia RL) y los mensajes enviados por el control de iluminación es recibido por las luminarias aun cuando se encuentran a 4000 mts de altura.

 

SaludOS/2

  • Like 1
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 5180 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...