Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

REQUEST: Best Practice or Suggestion?

Lumper Allandale
Registered User
Join date: 24 Oct 2006
Posts: 7
02-26-2007 08:31
I've created a script to ping a web server every 25 seconds and reset the floating text (controled by the standard floating text script) and name of an object to the data returned by the web site.

I wanted to do this w/the lowest impact on the SIM and w/o creating more lag than necessary. I've used sleep and resetscript.

Expert coders: is this a good or best way to do this? Other suggestions and improvments welcomed. It seems to work well, but is it a resouce hog (beyond the fact that any script that pings a sever is gonna use some ticks...

CODE


key http_request_id;

default
{
state_entry()
{


{
string url="http://www.thewebsite/my.php";
http_request_id = llHTTPRequest(url, [], "");

}
}
http_response(key request_id, integer status, list metadata, string body)
{
if (request_id == http_request_id)
{
llSetText("\n" + body, <0.0,1.0,1.0>, 1);

}
llSleep(25);
llResetScript();

}
}


Thank you.
Stephen Zenith
Registered User
Join date: 15 May 2006
Posts: 1,029
02-26-2007 08:39
From: Lumper Allandale
I've created a script to ping a web server every 25 seconds and reset the floating text (controled by the standard floating text script) and name of an object to the data returned by the web site.

I wanted to do this w/the lowest impact on the SIM and w/o creating more lag than necessary. I've used sleep and resetscript.

Expert coders: is this a good or best way to do this? Other suggestions and improvments welcomed. It seems to work well, but is it a resouce hog (beyond the fact that any script that pings a sever is gonna use some ticks...

CODE


key http_request_id;

default
{
state_entry()
{


{
string url="http://www.thewebsite/my.php";
http_request_id = llHTTPRequest(url, [], "");

}
}
http_response(key request_id, integer status, list metadata, string body)
{
if (request_id == http_request_id)
{
llSetText("\n" + body, <0.0,1.0,1.0>, 1);

}
llSleep(25);
llResetScript();

}
}


Thank you.


Personally, I'd just use a timer event instead of the sleep & reset combo, but that's just because the code will be neater (to my eyes) - I don't know if it would be any kinder on the sim or not.
_____________________
Meade Paravane
Hedgehog
Join date: 21 Nov 2006
Posts: 4,845
02-26-2007 09:33
I think llSetText causes a full object update.. It might be a little friendlier to remember the last text and only call llSetText if it changed.
Ylikone Obscure
Amatuer Troll
Join date: 24 Jan 2007
Posts: 335
02-26-2007 10:04
Is this scripted object required to ping your server even if nobody is around to see the changing text? Maybe put a condition in your script that it will only contact your server when an avatar is detected within 30 meters or so.

In SL, the question "if a tree falls in the forest and nobody is around to hear it, does it make a sound" can be answered "no, not if the tree is scripted efficiently". Ha, I made a funny.
Masakazu Kojima
ケロ
Join date: 23 Apr 2004
Posts: 232
02-26-2007 10:34
In this thread it is speculated that llSleep() is a busy loop, based on some LL dev comments in a video from a mono presentation. No idea if it's true. Resetting the script is just a waste of time.

A timer is probably better, but I seriously doubt it has any measurable impact on a sim either way.
Talarus Luan
Ancient Archaean Dragon
Join date: 18 Mar 2006
Posts: 4,831
02-26-2007 11:43
All good suggestions.

One more:
If your webserver fails to respond for some reason, or the script just doesn't get the http_response event, then it will stop running. Using a timer will make sure it continues to operate regardless, and long timers are very efficient on the sim. Basically, your script is in its most inactive state between events, and a timer event every 25-30 seconds will have very little impact.
Lumper Allandale
Registered User
Join date: 24 Oct 2006
Posts: 7
Thanks!
02-26-2007 12:51
These have been very helpful. Thanks you!

I've gone back to a timer based script and am working on finding good ways to implement the suggestions.

Once I figure out the best way to strip off patterns from returned text (e.g, I want to strip off all the \n's returned by the server in order to test the llGetObjectDesc() vs. the "body" of the text returned from the server, which contains "invisible" \n's) - then, I'll be good to go.

Haven't had this much fun learning scription seat-of-the-pants style since my days on MOO.
Lumper Allandale
Registered User
Join date: 24 Oct 2006
Posts: 7
Current Code
03-07-2007 12:05
Here's my current code to grab the date (song&artist from a SHOUTcast stream), reset the obj name and speak the information.

I would appreciate it if someone would show me EXACTLY how to code in a sensor that would only reset the llText and llSay if and ony if an active avatar is w/in 30m. I'd like to make this as lag-free as possible and be SIM friendly.

CODE


//grabs artist & song from SHOUTCAST server, displays it and speaks it
key http_request_id;
string body2 = "Hello, Carbon Unit"; //dummy text for comparison


default
{
state_entry()
{
llSetTimerEvent(20); //timer 20 sec repeat

}

timer()

{

llSleep(5); //build in a little sleep time
string url="http://www.myshoutcast.com/myaddress.php"; //url location
http_request_id = llHTTPRequest(url, [], "");

}



http_response(key request_id, integer status, list metadata, string body) //std URL

{
if (request_id == http_request_id && body != body2) // test to see if we really need to set text
{
llSetText("\n\n\n" + body, <0.0,1.0,1.0>, 1);
body2 = body; // reset our body dummy to current body text
llSleep(3); //take a break
llSay(0, body); //speak the current song & artist info 20m

}

}
}



Thank you
Meade Paravane
Hedgehog
Join date: 21 Nov 2006
Posts: 4,845
03-07-2007 13:11
I don't know how clever the LSL compiler is with string constants but you might want to move the url to be a global - define it up at the top instead of every time you hit the timer. Since it never changes, it's better form, IMO, to have it as a global anyway.

Also, I think I've read that LSL is more effecient at runtime if you explictly use floating point constants for float parameters. IE, use llSleep(3.0) instead of llSleep(3). You probably won't notice the performance but I think it keeps the code neater.

Maybe something like this totally untested code...
CODE

//grabs artist & song from SHOUTCAST server, displays it and speaks it
key http_request_id;
string previousText = "";
float TIMER = 20.0;
string REQUEST_URL = "http://www.myshoutcast.com/myaddress.php";

default
{
state_entry()
{
llSetTimerEvent(TIMER);
}

timer()
{
llSetTimerEvent (0.0); // cancel the timer for now
http_request_id = llHTTPRequest(REQUEST_URL, [], "");
}

http_response(key request_id, integer status, list metadata, string body) //std URL
{
if (request_id == http_request_id)
{
if (body != previousText) // test to see if we really need to set text
{
llSetText("\n\n\n" + body, <0.0,1.0,1.0>, 1);
previousText = body; // reset our body dummy to current body text
llSay(0, body); //speak the current song & artist info 20m
}

// restart the timer
llSetTimerEvent (TIMER);
}
}
}

This would probably break if the http server never responded, tho. Maybe set the request key to null when it responds and keep the timer going. If the timer fires N times while the request key is not null, redo the request.