Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

link_message issue

Dragon Keen
Registered User
Join date: 24 Apr 2006
Posts: 245
08-01-2006 10:56
trying to figure out why this doesnt work

CODE
link_message(integer sender_num, integer num, string str, key id)
{
if(num == LINKMSG_GET_PIECE)
{
piecesDone++;
string percent_done = (string)llRound(100*(piecesDone / 1024)) ;
llSetText( "Working... " + percent_done + "%", <1,0,0>, 1.0 );
}
}


piecesDone DOES increment, but percent_done always stays 0

any suggestions?
Takuan Daikon
choppy choppy!
Join date: 22 Jun 2006
Posts: 305
08-01-2006 11:00
Could it have something to do with integer rounding? Until piecesDone exceeds 1024 the result of piecesDone/1024 is less than 1 and immediate integer rounding in that subexpression would make it zero. Does that make any sense?
Adriana Caligari
Registered User
Join date: 21 Apr 2005
Posts: 458
08-01-2006 11:04
I have no clue how LSL works out bracketted equations and mixed types - but i would clarify it by specifying float.

string percent_done = (string)llRound(100.0 * (((float)piecesDone / 1024.0 ))) ;

or even breaking it up and seeing where it goes wrong

float ta = (float)pieceDone / 1024.0;
float tb = ta * 100.0;
integer tc = llRound( tb );
string percent_done = (string)tc;

And put some debug in.
_____________________
Maker of quality Gadgets
Caligari Designs Store
Ordinal Malaprop
really very ordinary
Join date: 9 Sep 2005
Posts: 4,607
08-01-2006 11:05
Yes, that might be it, I've had some odd results with integer division. It doesn't always cast to a float. Try (float)piecesDone instead of just piecesDone.

Edit: snap!
Dragon Keen
Registered User
Join date: 24 Apr 2006
Posts: 245
08-01-2006 11:06
it SHOULD work

this is an excerpt from another script i have, but not in the 'link_message' event

CODE

pieces_done++;
string percent_done = (string)llRound( 100 * ( pieces_done / pieces_total ) ) ;
llSetText( "Working... " + percent_done + "%", <1,0,0>, 1.0 );


so im doubting its an issue with the code itself, maybe a weird linked message issue?
Ordinal Malaprop
really very ordinary
Join date: 9 Sep 2005
Posts: 4,607
08-01-2006 11:08
Is pieces_total a float?

I don't think it's a link_message issue, it's more likely to be a type quirk.
Dragon Keen
Registered User
Join date: 24 Apr 2006
Posts: 245
08-01-2006 11:14
it 'HAS" to be a quirk...

the code works inside a normal event like touch... but inside link_message I did what Adriana Caligari suggested, split it all up and debugged it.

CODE
float ta = (float)piecesDone / 1024.0;
float tb = ta * 100.0;
integer tc = llRound( tb );
string percent_done = (string)tc;

llSay(0, (string)ta + " : " + (string)tb + " : " + (string)tc + " : " + percent_done);

OUTPUT
0.341797 : 34.179688 : 34 : 34

and it worked flawless

just strange that if the code I originally posted isnt inside link_message it works fine, but inside link_message it has a heart attack

Thanks for the info everyone though....
Joannah Cramer
Registered User
Join date: 12 Apr 2006
Posts: 1,539
08-01-2006 11:26
From: Dragon Keen
trying to figure out why this doesnt work
CODE
link_message(integer sender_num, integer num, string str, key id)
string percent_done = (string)llRound(100*(piecesDone / 1024)) ;

any suggestions?

You are working with integers here. Until piecesDone equals or exceeds 1024 the result of division will be rounded down to 0.

The easiest way to deal with it is to multiply by 100 first, then divide the result. This means you'll only need 11 pieces for the amount to exceed 1024 and result in 1%

string percent_done = (string)( 100 * piecesDone / 1024 );

it's possible to get exact results with casting into floats like mentioned above, but unless you show the final result as float then all this accuracy gets lost in the end anyway.
Dragon Keen
Registered User
Join date: 24 Apr 2006
Posts: 245
08-01-2006 11:43
may be the case... however like i said it does work as is in another script i made

I think i found the cause.... it seems you can flood link_message, and results will just be ignored.... I modified by code a lil to send less messages, and the original works now.
Joannah Cramer
Registered User
Join date: 12 Apr 2006
Posts: 1,539
08-01-2006 12:19
From: Dragon Keen
I think i found the cause.... it seems you can flood link_message, and results will just be ignored....

There's a limit of 64 events of any kind (including link_message) that can be in the script 'queue' waiting for script to process them. Any event generated while the queue remains full gets silently dropped... So if you have something like 200 prims sending one message each all at once, the script which receives them gets its queue overflown.

A sort-of workaround in such situation could be to add randomly generated (or fixed and based on prim number in set) delay in each script that sends its message, before the message is sent. This can slow down rate at which messages are generated, giving the receiving script chance to process them and keep its queue free. o.O
Spiritfire Musketeer
Designing Knight
Join date: 1 Oct 2005
Posts: 65
08-01-2006 14:09
Well, the problem is actually quite obvious. Every time the event is fired, you're declaring the string. a declaration resets the variable. Make it global and you won't have this problem.
_____________________
Owner of Love's Retreat, a mountain resort with a shopping mall, dance club/lodge, and The Chained Tail Dungeon.