Jump to content

Object collision counter with color change


Gothica Crazyboi
 Share

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

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

Recommended Posts

I am trying to make a target system where I shoot it up to five times each time shows a new color, on the 5th time reset the counter and start fresh with it going back to white.

 

this is what I have thus far but it's getting stuck on the 5th color and not working from there.


default
{
    state_entry()
{
    }  
     collision_start(integer number_detected)
{
    llSay(0, "Ding!");
    {
    integer i;
    for( i = 0; i < number_detected; i++ )
    {
        if(i=1)
        {
            llSetColor(<1,1,0.0>, ALL_SIDES);
        }
        if(i=2)
        {
            llSetColor(<0.0,0.0,0.0>, ALL_SIDES);
        }
        if(i=3)
        {
            llSetColor(<0.0,0.0,1>, ALL_SIDES);
        }
        if(i=4)
        {
            llSetColor(<0.0,1,0.0>, ALL_SIDES);
        }
        if(i=5)
        {
            llSetColor(<0.5,0.2,0.0>, ALL_SIDES);
        }
        if(i > 5)
        {
            llSetColor(<1,1,1>, ALL_SIDES);
            llResetScript();
        }
    }
    }
}
    collision_end(integer number_detected)
    {
    }
}

 

Link to comment
Share on other sites

There are a couple of things to look at.  The most serious is the way you have constructed your if tests.  As written, they do a bitwise comparison, but you want a boolean comparison here, so each one should be in the form

if (i == 1)

and so forth.  It's an easy mistake to make, no matter how long you have been scripting. 

The other thing to look at, just as serious, is your use of a for loop in this context.  As written, your script will try to run through the entire loop each time you collide with the object.  That's really not what you want.  Instead, you want it to change color once when you hit it.

Finally, your use of llResetScript is overkill, although it's technically not a mistake.  All you really need to do is start the color change sequence over again, not the whole bloody script.  So, a much more compact and logical way to write the script might be:
 

integer Count;    // A global counter

list Colors  = [<1,1,0>,<0,0,0>,<0,0,1>,<0,1,0>,<0.5,0.2,0>];    // All your color choices

default
{
    collision_start(integer num)
    {
        llSay(0,"Ding!");
        llSetColor(llList2Vector(Colors,Count),ALL_SIDES);   // Pick a color based on the current value of Count, and apply it.
        Count = (++Count%5);   // Increment Count and start over if Count == 5
    }
}

Notice that since you're no longer making all of the changes every time someone collides, you no longer need the if tests or the for loop.  Writing your script this way eliminates all three of the issues you were running into.

Edited by Rolig Loon
  • Like 5
Link to comment
Share on other sites

This is the way we learn.  If everything always worked right the first time, you wouldn't learn nearly as much.  ;)

Incidentally, the deep lesson here is to devote most of your energy to being sure that you understand the logic involved in whatever you are trying to script before you start writing.  We all make errors with syntax -- I have to look up the proper form of LSL functions all the time, even after scripting in SL for a decade -- but a script that doesn't have clear, unambiguous logic will give you maddeningly bad results, even if you get all the functions right and don't make typos.  I know --- easier said than done. :)  It helps to be marginally OCD.

Good luck.

  • Like 2
Link to comment
Share on other sites

That is a super cool way of doing it. I'da made it work,but it would have looked more like spaghetti. Also, thank you to the OP for bringing this subject up, because it reminded me I've been meaning to make an archery target that shows hits by changing colors temporarily...

I should probably get on that. xD 

Link to comment
Share on other sites

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