Jump to content

3 steps sequence by touch


Tattooshop
 Share

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

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

Recommended Posts

Keep a global variable (integer) and increment it every time the object is touched.

After the increment, use a modulo (counter % 3) to get a number in the range [0, 1, 2] and check which one of the three it is. Then do that action.

integer action = ++counter % 3;
if (action == 0) {;}
else if (action == 1) {;}
else if (action == 2) {;}

 

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

17 hours ago, Wulfie Reanimator said:

Keep a global variable (integer) and increment it every time the object is touched.

After the increment, use a modulo (counter % 3) to get a number in the range [0, 1, 2] and check which one of the three it is. Then do that action.



integer action = ++counter % 3;
if (action == 0) {;}
else if (action == 1) {;}
else if (action == 2) {;}

 

Thank you so much! Works like a charm! :)👍

 

Edited by Tattooshop
Link to comment
Share on other sites

5 hours ago, Tattooshop said:

Works like a charm! :)👍

a little FYI

in our production code we should ensure that 'counter' is bounded, as when we continually add positive values to a signed integer then it will eventually return a negative result if not bounded

2147483647 + 1 = -2147483648

when it does go negative the modulus range (% 3) is [-2,-1,0]

in our production code is best to set some upper bound to counter depending on the use case

example boundings:

1) counter = (++counter % 3);  // counter is always in [0,1,2]

2) if (counter > someN) counter = 0;

or we can bound the modulus result to the absolute (positive) range

action = llAbs(++counter % 3);

 

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

14 hours ago, Mollymews said:

a little FYI

in our production code we should ensure that 'counter' is bounded, as when we continually add positive values to a signed integer then it will eventually return a negative result if not bounded

You're absolutely right, but at the same time, the point at which this issue becomes relevant is pretty unrealistic.

If you were to increment the counter every second, it would take you almost 70 years to reach the point where it becomes negative (and obviously breaks for another 70 years).

This is basically the Year-2038 problem but I don't think anyone's going to reach 1 touch per second on an object and I guarantee SL won't be here in 70 years.

  • Like 1
Link to comment
Share on other sites

10 hours ago, Wulfie Reanimator said:

You're absolutely right, but at the same time, the point at which this issue becomes relevant is pretty unrealistic.

If you were to increment the counter every second, it would take you almost 70 years to reach the point where it becomes negative (and obviously breaks for another 70 years).

This is basically the Year-2038 problem but I don't think anyone's going to reach 1 touch per second on an object and I guarantee SL won't be here in 70 years.

Yes, there is no way to reach maxint in this case. No need to take care for that.

But it's exactly that thinking that leads to year 2k and year 2038 problems. It starts with  "you will never get to ..." 😁
So I suggest to stay alert and have a 2nd thinking in all cases where you think you will never get to the breaking point.

  • Like 3
Link to comment
Share on other sites

10 hours ago, Tattooshop said:

Thanks a lot for the addition @Mollymews! then it will look like this? :)



integer action = llAbs(++counter % 3);
if (action == 0) {;}
else if (action == 1) {;}
else if (action == 2) {;}

in this use case if there were only ever going to be 3 actions: 0, 1 and 2 then go with not using llAbs function to obtain a tiny performance gain of one less function call
 

integer action = 0; // global

touch
{
   if (action == 0)
   else if (action == 1)
   else // action == 2

   action = (++action % 3);
}

where in the code we update action is important. In the above then on script reset the first action that can be actioned is 0

if we reorder the code:

integer action = 0; // global

touch
{
   action = (++action % 3);
   if (action == 0)
   else if (action == 1)
   else // action == 2
}

then the first action on script reset will be 1

Edited by Mollymews
actioned
  • Like 1
  • Thanks 1
Link to comment
Share on other sites

9 hours ago, Nova Convair said:

It starts with  "you will never get to ..." 😁

 

pretty much yes.  When we can write our codes to stay within the bounds of the physical system we are working with then is always best practice to do so from a commercial/production pov.

Edited by Mollymews
typs
  • Like 1
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 1337 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...