Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

How to sum?

Seagel Neville
Far East User
Join date: 2 Jan 2005
Posts: 1,476
06-04-2005 07:39
Hello there,

There are three objects all linked. If I touch one, each one says random number
by llMessageLinked. And I want to let each object know the sums. What should
I do?
CODE
integer x;
integer sums;

default
{
state_entry()
{
sums = 0;
}
touch_start(integer tatal_number)
{
llMessageLinked(LINK_SET,0, "Report", "");
}
link_message(integer sender_num, integer num, string str, key id)
{
if(str == "Report")
{
llMessageLinked(LINK_SET, (integer)(llFrand(2.0)+1.0), "", "");
state counting;
}
}
}

state counting
{
link_message(integer sender_num, integer num, string str, key id)
{
for(x = 1; x<= 3; x++)
{
sums = sums + num;
}
llOwnerSay((string)sums);
state default;
}
}
hmm... I'm afraid if the state changing works well. And I don't think
that the times of "for-loop" is correct, either. Besides, can't link_message
receive its own llMessageLinked? Anyone help plz.
_____________________
:) Seagel Neville :)
Eloise Pasteur
Curious Individual
Join date: 14 Jul 2004
Posts: 1,952
06-04-2005 08:01
I've not tested it with link messages, but I think a script can't hear itself, although the object can.

Since you're passing integer random numbers why not put it in the integer part of the link message.

Each object 'knows' its own number and can pick up the numbers from the other two through the linked message, then it just adds them together. Each object then knows the sum of them.
Seagel Neville
Far East User
Join date: 2 Jan 2005
Posts: 1,476
06-04-2005 08:53
Thank you, Eloise,

From: Eloise Pasteur
I've not tested it with link messages, but I think a script can't hear itself, although the object can.
You mean I should separate the script?
From: Eloise Pasteur
Since you're passing integer random numbers why not put it in the integer part of the link message.
Excuse me, but did I put it in the wrong part?
From: Eloise Pasteur
Each object 'knows' its own number and can pick up the numbers from the other two through the linked message, then it just adds them together. Each object then knows the sum of them.
I got it. Thank you. But was my script wrong?
When I tested it, only the other two objects said their sums ,but they were sometimes not the same. hmm?
_____________________
:) Seagel Neville :)
Eloise Pasteur
Curious Individual
Join date: 14 Jul 2004
Posts: 1,952
06-04-2005 09:50
What I'd do, in psuedo code is:

Touch
Generate 2 random numbers, one the one you want, one a psuedo password.
send link message with my random number to other two and store mine, and with the password in one of the other fields.
make total = my number

link_message
generate a random number IF the password is not the one I generated AND I haven't used this password before.
make total = my number
add number from link message
send message forwarding the password and my number.

that gives each object the number it generates, plus the sum of the all the numbers - remember the link_message event will be triggered by both the other objects.

There will be twiddles I'm sure, but that should do what you're looking for if you code it up. I can't see any obvious errors in your code although I'm not a really good reader of other people's code I'm afraid.
Seagel Neville
Far East User
Join date: 2 Jan 2005
Posts: 1,476
06-04-2005 14:53
Hi Eloise,

Thank you, I got what you said.

There are there objects, A, B, and C.
I always touch A.

A: Touched, it generate number "3".
Send number "3" and the passward which tells to generate number

B: Received "3" and the password, and it generate number "2".
Making total number, "5".
Send number "2".

C: Received "3" and the password, and it generate number "1".
Making total number, "4".
Send number "1".

A: Received "2" from B, it making total number "5".
Received "1" from C, it making tatal number "6".

B: Received "1" from C, it making total number "6".

C: Received "2" from B, it making total number "6".

Yay, sure.
But when can I have them told us their total number?
If I put llOwnerSay in the link message event, they would tell number whenever they received it. Can I bring the data to another event? It is just the total number that I want.
I don't know how to have them done it. Please?
_____________________
:) Seagel Neville :)
Eloise Pasteur
Curious Individual
Join date: 14 Jul 2004
Posts: 1,952
06-04-2005 17:48
Why do you have three objects to generate the three numbers?

Anyway...

if in your link message event in object A you put a counter, if it's received two messages it resets and says the total...

that should do it.
Seagel Neville
Far East User
Join date: 2 Jan 2005
Posts: 1,476
06-05-2005 05:58
To tell the truth, I don't knnow how much objects would appear in front of me, and it would generate number upto 256. I wanted to just simplify the talk.

OK, I will count number of objects and try to add numbers those times. Thank you.
_____________________
:) Seagel Neville :)
Jeffrey Gomez
Cubed™
Join date: 11 Jun 2004
Posts: 3,522
06-05-2005 06:23
This isn't so hard. I'd perform the task something like this:

CODE

integer is_talking = FALSE; // Boolean; throttles extra messages
float time_between = 3.0; // Cooldown time between tasks
float my_number = 0.0; // My random number
float MIN_RAND = 3.0; // Minimum for random number
float MAX_RAND = 10.0; // Maximum for random number

default
{
touch_start(integer total_number)
{
llMessageLinked(LINK_SET,0,"Start up!","");
}
link_message(integer sender, integer num, string str, key id)
{
if(str == "Start up!" && is_talking == FALSE)
{ // We're not trying to be a "cool kid" -^
my_number = 0.0; // Reset the number
is_talking = TRUE; // Throttles any crosstalk for this command
llSleep(0.5); // Quick sleep timer to prevent number crossover
llSetTimerEvent(time_between); // Fire up the timer
llMessageLinked(LINK_SET,0,"My Number",(string)(llFrand(MAX_RAND - MIN_RAND) + MIN_RAND));
}
else if(str == "My Number")
{
my_number += (float)((string)id); // Add the number
}
}
timer()
{
is_talking = FALSE; // Refresh the cooldown timer
llSetTimerEvent(0.0); // Shut the timer
if(llGetLinkNumber() == 1 || llGetLinkNumber() == 0) // If first in a set...
llOwnerSay("My number is: " + (string)my_number); // Say the stuff
}
}


As a note, it's actually faster to do it with multithreading than it is to loop one script 256 plus times.
_____________________
---
Seagel Neville
Far East User
Join date: 2 Jan 2005
Posts: 1,476
06-05-2005 08:39
Hello Jeffrey,

Thanks a lot. I was stuck at all.

You seems to use their objects' key id number. Is that better to use than integer num?
But the most what I wanted to know was how to exit the link_message event and get the total number without the process.
hmm.. timer event! I couldn't even imagine such a thing. You said it wasn't so hard though. :D And it seems to take time for a while for me to understand your script.

Anyway, thank you. I try to learn. :)
_____________________
:) Seagel Neville :)
Jeffrey Gomez
Cubed™
Join date: 11 Jun 2004
Posts: 3,522
06-05-2005 17:37
From: Seagel Neville
You seems to use their objects' key id number. Is that better to use than integer num?

The key can be used to pass more than just an integer, so it's helpful as an extra data field. In this example, I use it to pass a float.

And no problem.
_____________________
---