Shymus Roffo Posted November 28, 2011 Share Posted November 28, 2011 i've been working on an unpacker script and i noticed some people made prims fade away once someone unpacks or fade to solid when they are rezzed and i have completed but i was wondering how to do it with a "while" or a "for" loop. I am still learning how to use the loops. llSetAlpha(0.1, ALL_SIDES); llSleep(0.1); llSetAlpha(0.2, ALL_SIDES); llSleep(0.1); llSetAlpha(0.3, ALL_SIDES); llSleep(0.1); llSetAlpha(0.4, ALL_SIDES); llSleep(0.1); llSetAlpha(0.5, ALL_SIDES); llSleep(0.1); llSetAlpha(0.6, ALL_SIDES); llSleep(0.1); llSetAlpha(0.7, ALL_SIDES); llSleep(0.1); llSetAlpha(0.8, ALL_SIDES); llSleep(0.1); llSetAlpha(0.9, ALL_SIDES); llSleep(0.1); llSetAlpha(1, ALL_SIDES); llSleep(0.1); this is what i am trying to do with a loop to shorten it. Link to comment Share on other sites More sharing options...
Rolig Loon Posted November 28, 2011 Share Posted November 28, 2011 Your instincts are good. Avoid using llSleep when you can, because that stalls the entire script. In a simple script, that might not make any diffrerence, but if you had a listen event and a touch_start event, for example, the script would miss any messages,or touches that came in while it was sleeping. It's smarter to do your fade in a timer event. Here's one way ... llSetTimer(0.1);gFade = 0; // gFade is a global integer variabletimer(){ llSetAlpha( (float) (++gFade)*0.1, ALL_SIDES); if (gFade == 10) { llSetTimerEvent(0.0); }} Link to comment Share on other sites More sharing options...
Shymus Roffo Posted November 28, 2011 Author Share Posted November 28, 2011 is there a way to get this made into a function and without timer or is it only able to work in the timer event? Link to comment Share on other sites More sharing options...
PeterCanessa Oh Posted November 28, 2011 Share Posted November 28, 2011 The first of these examples uses llSleep() as you have but that will make the script stop completely until the fade-in is complete. The second example uses llSetTimerEvent() so that other events can be processed. Both examples are for fading from invisible to solid when the object is rezzed. default{ on_rez(integer StartParam){ integer Alpha; for(Alpha = 0; Alpha <= 10; Alpha++){ llSetAlpha(((float) Alpha) / 10), ALL_SIDES); llSleep(0.1); } }}// =====================================================================float Alpha;default{ on_rez(integer StartParam){ llSetTimerEvent(0.1); } timer(){ llSetAlpha(Alpha, ALL_SIDES); Alpha += 0.1; if(1.0 < Alpha){ llSetTimerEvent(0.0); } }} /me waves to Rolig Link to comment Share on other sites More sharing options...
PeterCanessa Oh Posted November 28, 2011 Share Posted November 28, 2011 shymus Roffo wrote: is there a way to get this made into a function and without timer or is it only able to work in the timer event? Like this - which will fade-in when rezzed and fade-out when touched? Any nn.nn between 0.0 and 1.0 will work in the call FadeTo(nn.nn) float Alpha;FadeTo(float Target){ while(llFabs(Alpha - Target) >= 0.1){ if(Alpha > Target){ Alpha -= 0.1; }else{ Alpha += 0.1; } llSetAlpha(Alpha, ALL_SIDES); llSleep(0.1); }}default{ on_rez(integer StartParam){ FadeTo(1.0); } touch_end(integer HowMany){ FadeTo(0.0); }} Link to comment Share on other sites More sharing options...
Shymus Roffo Posted November 28, 2011 Author Share Posted November 28, 2011 it fades in when it rezzes and after it is solid it sends the contents except script once the contents are sent then it fades completly out and then removes its self Link to comment Share on other sites More sharing options...
Rolig Loon Posted November 28, 2011 Share Posted November 28, 2011 shymus Roffo wrote: is there a way to get this made into a function and without timer or is it only able to work in the timer event? No. If you are watching a clock, you have to have a timer event. You could take the version that Peter built into the on_rez event in his example and convert it into a user defined function, because that version uses the llSleep function. As we both pointed out, however, that will stall your script until the function has done its work. You may not care in this simple case, and that's fine. We're just pointing out that over the long run, it's wise to avoid llSleep if you can. /me waves back to Peter Link to comment Share on other sites More sharing options...
PeterCanessa Oh Posted November 28, 2011 Share Posted November 28, 2011 And example 4 (using the previously-defined FadeTo() function and global float Alpha): default{ on_rez(integer StartParam){ FadeTo(1.0); // ... Give Everything Here FadeTo(0.0); llDie(); }} Link to comment Share on other sites More sharing options...
Shymus Roffo Posted November 28, 2011 Author Share Posted November 28, 2011 ok i used the second one with the timer in it but i am curious if you are able to get that into a function or does it require to used llSleep()? Link to comment Share on other sites More sharing options...
Shymus Roffo Posted November 28, 2011 Author Share Posted November 28, 2011 i have one question about that one is that its not really going completly solid and i am trying to get it solid? Link to comment Share on other sites More sharing options...
PeterCanessa Oh Posted November 28, 2011 Share Posted November 28, 2011 shymus Roffo wrote: ok i used the second one with the timer in it but i am curious if you are able to get that into a function or does it require to used llSleep()? If you want a function to be self-contained you need to use llSleep(). Using llSetTimerEvent() allows and requires that the function return and that the script gets on with whatever else may be happening. That could be touches, collisions, movement, reading a configuration notecard or anything else of interest to the script. Then, whenever the timer fires and SL raises a timer event, the timer() event-handler processes the next iteration of the 'loop' after which the script goes back to doing 'whatever'. In your case - fade-give-fade - you don't want anything else to happen, or be taken notice of, while the object is fading in/out so you might as well use llSleep() to keep things straightforward. ETA: If the FadeTo() function is not making the object completely solid that's because of the difficulty of comparing floats - computers don't always hold them very accurately. EDIT: The simplest option is to put a 'llSetAlpha(Target, ALL_SIDES);' after the closing '}' of the while loop. PS: Unless you're pre-declaring the list of contents to give you need to read all those too - presumably while the object is fading-in. That means you DO want something else to happen ... script coming. Link to comment Share on other sites More sharing options...
PeterCanessa Oh Posted November 28, 2011 Share Posted November 28, 2011 Still don't need a timer, this fades in according to how much of the contents it's read (so will be slower with larger contents) default{ on_rez(integer StartParam){ // No timer - just fade in the fraction of contents that have been read list Contents; integer Counter; integer Total = llGetInventoryNumber(INVENTORY_ALL); string Name; for(Counter = 0; Counter < Total; Counter++){ llSetAlpha(Counter / Total, ALL_SIDES); Name = llGetInventoryName(INVENTORY_ALL, Counter); if(Name != llGetScriptName()){ Contents += [Name]; } llSleep(1.0 / Total); // The more there is to read the less time it will spend sleeping } llSetAlpha(1.0, ALL_SIDES); llGiveInventoryList(llGetOwner(), llGetObjectName(), Contents); for(Counter = 9; Counter >= 0; Counter--){ llSetAlpha(Counter / 10, ALL_SIDES); llSleep(0.1); } llDie(); }} Link to comment Share on other sites More sharing options...
Recommended Posts
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