Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Global Variable

Rasputin Polonsky
Registered User
Join date: 13 Aug 2005
Posts: 9
08-14-2005 08:32
On my item (a weapon) i got multiple scripts.
Is there a way i can have a global variable for all these scripts. (The one holding the Ammo-info)
and if yes how whould i pass it around to the scripts

- Rasputin 'AssKicR' Polonsky
Judah Jimador
Registered User
Join date: 13 Mar 2005
Posts: 230
08-14-2005 09:44
Hi Rasputin,

You'll probably want to reserve a couple of linked messages to set and get the global value between one of the scripts and all the others.

If you have more than a couple of globals, it might be a good idea to set up a separate script which does nothing but act as a repository for them.

-- jj
Rasputin Polonsky
Registered User
Join date: 13 Aug 2005
Posts: 9
08-14-2005 11:10
tnx
Rasputin Polonsky
Registered User
Join date: 13 Aug 2005
Posts: 9
08-14-2005 16:49
eh.. btw

Whould you mind giving an example
Blain Candour
Registered User
Join date: 17 Jun 2005
Posts: 83
08-14-2005 20:27
Sure.



this sends the variable to the other linkparts:
CODE

string global_variable;
integer variable_channel = 22222;

default
{
touch_start(integer total_number)
{
global_variable = "yo baby's momma!";
llMessageLinked(LINK_SET, variable_channel , global_variable, NULL_KEY);
}
}



and this listens for it:
CODE

string global_variable;
integer variable_channel = 22222;

default
{
link_message(integer sender_num, integer num, string str, key id)
{
if (num = 22222)
{
global_variable = str;
llOwnerSay(global_variable);
}
}
}




Didn't test compile that in game but it SHOULD pass the string from one script to another and store it as a variable. Then say it back to the owener. In this case the phrase is "yo baby's momma".
Rasputin Polonsky
Registered User
Join date: 13 Aug 2005
Posts: 9
08-14-2005 22:38
the problem is that i need to send both string and int

like

ammo 1000

ammo 342

ammo 0
Blain Candour
Registered User
Join date: 17 Jun 2005
Posts: 83
08-14-2005 23:23
So then use more than 1 variable channel and send 2 messages one variable per channel.

convert the integer to a string to send the message and then back to an integer before storing it.

Or when you get a bit more advanced with it combine and parse the strings as a list or wiht substrings. Really easier just to send 2 messages if you aren't big into LSL yet though.
Eloise Pasteur
Curious Individual
Join date: 14 Jul 2004
Posts: 1,952
08-15-2005 03:08
Actually if you modify that code so that you use the key field (which will also take strings) to carry the checker, rather than the integer field you can pass the number of bullets left straight in the integer field.

Something like this:

CODE

string global_variable;
string checker = "22222";
integer ammo=340;

default
{
touch_start(integer total_number)
{
global_variable = "Ammo =";
llMessageLinked(LINK_SET, ammo , global_variable, checker);
}
}



and this listens for it:
CODE

string checker = "22222";
integer ammo;

default
{
link_message(integer sender_num, integer num, string str, key id)
{
if (id == "22222")
{
llOwnerSay(global_variable+" "+(string)num);
ammo=num;
}
}
}


This should compile and say "Ammo = 340" from the child, but I've not tested it. You can then use this to pass on variables to other events as required. In this case ammo is a global that get changed every time it receives a link message once you get that code sorted.
Blain Candour
Registered User
Join date: 17 Jun 2005
Posts: 83
08-16-2005 02:42
That works as well. I very rarely use linktells to pass just 1 type of thing so I have never used the key as the identifier instead of the integer. It is definately cleaner for integer only passes though. Thanks for the tip.
Eloise Pasteur
Curious Individual
Join date: 14 Jul 2004
Posts: 1,952
08-16-2005 06:11
You're welcome, and of course it depends on what you need to pass. In theory two strings (string and key) and an integer can go, and with typecasting and some maths jiggery pokery you can make that 3 pieces of information.

But if, like this example, you *need* to pass info that's an integer use something else for the identifier if you need it. Actually in a gun script you might find the link_num a useful identifier too, it's only if you're multi-threading within a single prim that's not so useful.

Like all these things it's a question of adapting to your circumstances and needs.