Jump to content

Loop with exceptions


Paul Hexem
 Share

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

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

Recommended Posts

So, I've got a script I'm looking at, that looks up some textures on a list, based on another list, based on an input string... And then it applies those textures to a mesh, skipping 3 and 7. I'm just thinking there's gotta be a way to use a while or for loop to streamline the process?

 

write_address()
{
    integer num = llGetNumberOfPrims();
    integer i;
    for (i=1; i < num + 1; i++)
    {
        if(llGetLinkName(i) == "designation")
        {
            if(activity == TRUE)
            {
                integer a = llListFindList(index, [llGetSubString(gateid, 0, 0)]);
                llSetLinkPrimitiveParamsFast(i, [PRIM_TEXTURE, 0, llList2Key(textures, a), <1.0, 1.0, 0.0>, <0.0, 0.0, 0.0>, 0.0]);
                integer b = llListFindList(index, [llGetSubString(gateid, 1, 1)]);
                llSetLinkPrimitiveParamsFast(i, [PRIM_TEXTURE, 1, llList2Key(textures, b), <1.0, 1.0, 0.0>, <0.0, 0.0, 0.0>, 0.0]);
                integer c = llListFindList(index, [llGetSubString(gateid, 2, 2)]);
                llSetLinkPrimitiveParamsFast(i, [PRIM_TEXTURE, 2, llList2Key(textures, c), <1.0, 1.0, 0.0>, <0.0, 0.0, 0.0>, 0.0]);
                //d needs to be transparent
                llSetLinkPrimitiveParamsFast(i, [PRIM_TEXTURE, 3, TEXTURE_TRANSPARENT, <1.0, 1.0, 0.0>, <0.0, 0.0, 0.0>, 90.0*DEG_TO_RAD]);
                integer e = llListFindList(index, [llGetSubString(gateid, 4, 4)]);
                llSetLinkPrimitiveParamsFast(i, [PRIM_TEXTURE, 4, llList2Key(textures, e), <1.0, 1.0, 0.0>, <0.0, 0.0, 0.0>, 0.0]);
                integer f = llListFindList(index, [llGetSubString(gateid, 5, 5)]);
                llSetLinkPrimitiveParamsFast(i, [PRIM_TEXTURE, 5, llList2Key(textures, f), <1.0, 1.0, 0.0>, <0.0, 0.0, 0.0>, 0.0]);
                integer g = llListFindList(index, [llGetSubString(gateid, 6, 6)]);
                llSetLinkPrimitiveParamsFast(i, [PRIM_TEXTURE, 6, llList2Key(textures, g), <1.0, 1.0, 0.0>, <0.0, 0.0, 0.0>, 0.0]);
                //h needs to be transparent
                llSetLinkPrimitiveParamsFast(i, [PRIM_TEXTURE, 7, TEXTURE_TRANSPARENT, <1.0, 1.0, 0.0>, <0.0, 0.0, 0.0>, 0.0]);
            }
            else
            {
                llSetLinkPrimitiveParamsFast(i, [PRIM_TEXTURE, ALL_SIDES, TEXTURE_TRANSPARENT , <1.0, 1.0, 0.0>, <0.0, 0.0, 0.0>, 0.0]);
            }
        }
    }
}

I feel like this should work, but it doesn't seem very streamlined. Any suggestions?

write_address()
{
    integer num = llGetNumberOfPrims();
    integer i;
    for (i=1; i < num + 1; i++)
    {
        if(llGetLinkName(i) == "designation")
        {
            if(activity == TRUE)
            {
                integer t;
                for(t = 0; t<=7;t++)
                {
                    integer z = llListFindList(index, [llGetSubString(gateid, t, t)]);
                    {
                        if(t == 3 || t==7)
                        {
                            llSetLinkPrimitiveParamsFast(i, [PRIM_TEXTURE, t, TEXTURE_TRANSPARENT, <1.0, 1.0, 0.0>, <0.0, 0.0, 0.0>, 0.0]);
                        }
                        else
                        {
                            llSetLinkPrimitiveParamsFast(i, [PRIM_TEXTURE, t, llList2Key(textures, z), <1.0, 1.0, 0.0>, <0.0, 0.0, 0.0>, 0.0]);
                        }
                    }
                }
            }
            else
            {
                llSetLinkPrimitiveParamsFast(i, [PRIM_TEXTURE, ALL_SIDES, TEXTURE_TRANSPARENT , <1.0, 1.0, 0.0>, <0.0, 0.0, 0.0>, 0.0]);
            }
        }
    }
}

 

Edited by Gadget Portal
Link to comment
Share on other sites

Does this make sense, or should I stop trying to think about abstract code first thing after I wake up?

integer link_number = llGetNumberOfPrims ();
do
{
   integer face_number = 8;
   while (face_number--)
   {
        //...

   }
}
while (--link_number);

Link to comment
Share on other sites

Well yeah, I wasn't going to rewrite the whole thing. Having got as far as your example, I figured you already knew where you could stick your exceptions. It's a suggestion as to how you could use do-while and while loops to streamline your for loops.

I don't think there's much you can do about the exceptions, except maybe combine the "designation" and "activity" conditions with an && into a single if. But then again, unless things have changed, I understand that LSL will evaluate all the conditions in an if even when one of the earlier ones fails, so maybe it's better to do them separately.

A whole different approach might be to pre-prepare a list of links called "designation" at startup (and redo it on CHANGED_LINK) and a list of the faces you want to operate on and loop through those lists.

Link to comment
Share on other sites

24 minutes ago, KT Kingsley said:

A whole different approach might be to pre-prepare a list of links called "designation" at startup (and redo it on CHANGED_LINK) and a list of the faces you want to operate on and loop through those lists.

Yeah, depending how tight the script is for memory and whether anything relevant changes at runtime, I might consider storing a global variable containing a whole long parameters list with PRIM_LINK_TARGETs to execute in a single call to llSetLinkPrimitiveParametersFast() instead of consing-up all the little face-specific lists and calling that function separately for each link and face. (That wouldn't streamline the process of stepping through the links and faces, it just moves it to a pre-calculation phase. It would substantially reduce the number of object updates, though.)

[ETA: Looking at this again, am I understanding correctly that each link will get the same set of textures applied to its faces as all the other "designation"-named links? If so, it would be tighter to create that link-independent list of parameters one time, and re-use it for each link.]

Edited by Qie Niangao
  • Like 1
Link to comment
Share on other sites

building on what has already been mentioned.

if the contents of 'gateid' are dynamic then the Slppf parameters list could be build at the top of the function

another thing that could be done is to link the prims named "designation" as a contiguous block

Link to comment
Share on other sites

21 minutes ago, Qie Niangao said:

Yeah, depending how tight the script is for memory and whether anything relevant changes at runtime, I might consider storing a global variable containing a whole long parameters list with PRIM_LINK_TARGETs to execute in a single call to llSetLinkPrimitiveParametersFast() instead of consing-up all the little face-specific lists and calling that function separately for each link and face. (That wouldn't streamline the process of stepping through the links and faces, it just moves it to a pre-calculation phase. It would substantially reduce the number of object updates, though.)

Yes, I hadn't really looked beyond the code itself, but a single call to llSetLinkPrimitiveParamsFast would have more streamlining effect overall than any amount of code tweaking.

The parameters list wouldn't need to be a global variable, just local to the function. Loop through the pre-prepared lists (which would be global) and rather than calling SLPPF for each face just add the parameters (including PRIM_LINK_TARGET and the face numbers) to the list and call SLPPF once for LINK_SET after the loops have been completed.

Edited by KT Kingsley
  • Like 2
Link to comment
Share on other sites

Actually, getting and storing the link numbers on the changed event is a good idea. Ideally there should only be the one with said name, but as the object could potentially change, I wanted to use names instead of numbers. That's a good thought and will actually streamline things overall.

Then KT's first thought should work even better.

Link to comment
Share on other sites

According to the Wiki there is no difference between a while-loop and for-loop, when compiled in Mono. Most of us with background in C/C++ though is used to a while-loop always is faster.

But as mentioned by others, streamlining llSetLinkPrimitiveParamsFast() and update the textures in one call is the best. Downside is code hard to understand and maintain, especial if more complex face changes at same time with say light, shine. materials etc.
 

 

Edited by Rachel1206
Link to comment
Share on other sites

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