Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Global variables not updating

Gregory McLeod
Registered User
Join date: 21 Oct 2006
Posts: 278
03-16-2007 09:13
I have a script which uses variables to hold the players names. these are not being updated.
CODE

// Global Variables
integer CHANNEL = 42; // dialog channel
list MENU_MAIN = ["Info", "Play", "Reset"]; // the main menu
list MENU_OPTIONS = ["4", "5", "6", "1", "2", "3","...Back"]; // a submenu
string ID; // id of user
integer SCRIPT_PIN = 29071938; // PIN for piece script update
string SCRIPT_TO_UPDATE = "Piece_Script"; // Script to be updated
string Player0 = "none";
string Player1 = "none";
string Player2 = "none";
string Player3 = "none";
string Player4 = "none";
string Player5 = "none";

// Initialise function
Initialise()
{
// llOwnerSay("Initialising"); // replace with llSay(0,"Initialising")
llListen(CHANNEL, "", NULL_KEY, "");
llMessageLinked(133,0,"Reset",NULL_KEY);
llOwnerSay("Initialised"); // replace with llSay(0,"Initialised")
}

// Update function
UpdatePrims()
{
integer num_prims = 122; // number of piece prims
integer i; // for variable
for (i = 2; i <= num_prims; i++)
{
key prim_key = llGetLinkKey (i); // get key of prim
llOwnerSay("Updating prim #" + (string)(i)); // tell me which prim is updating
// load the script to the current prim
llRemoteLoadScriptPin (prim_key, SCRIPT_TO_UPDATE, SCRIPT_PIN, TRUE, 0);
}
// tell me how many prims were updated
llOwnerSay("Updated " + (string)(num_prims - 1) + " prims");
}

// Play1 function
Play1()
{
// Player0 = "ChinChek";
// Player3 = llDetectedName(0);
// llOwnerSay("Player0: " + Player0);
// llOwnerSay("Player3: " + Player3);
}

// Play2 function
Play2()
{
llOwnerSay("Choose Colors by clicking one of the colored triangles.");
llOwnerSay("First to click a color will be announced");
}

// Play3 function
Play3()
{
llOwnerSay("play3 selected");
}

// Play4 function
Play4()
{
llOwnerSay("play4 selected");
}

// Play5 function
Play5()
{
llOwnerSay("play5 selected");
}

// Play6 function
Play6()
{
llOwnerSay("play6 selected");
}


// default state
default
{
state_entry()
{
Initialise();
}

on_rez(integer start_param)
{
Initialise();
}

touch_start(integer total_number)
{
ID = llDetectedKey(0);
// present dialog on click
llDialog(ID, "What do you want to do?", MENU_MAIN, CHANNEL);
}

listen(integer CHANNEL, string name, key id, string message)
{
// verify dialog choice
if (llListFindList(MENU_MAIN + MENU_OPTIONS, [message]) != -1)
{
if (message == "Info")
// give notecard info
llGiveInventory(ID, llGetInventoryName(INVENTORY_NOTECARD, 0));
if (message == "Play")
// present submenu on request
llDialog(ID, "How many players?", MENU_OPTIONS, CHANNEL);
if (message == "Reset")
// reset board
llMessageLinked(133,0,"Reset",NULL_KEY);
if (message == "...Back")
// present main menu on request to go back
llDialog(ID, "What do you want to do?", MENU_MAIN, CHANNEL);
if (message == "1")
{
// Set up board for 1 player
llMessageLinked(138,0,"Setup1",NULL_KEY);
Play1();
}
if (message == "2")
{
// Set up board for 2 players
llMessageLinked(137,0,"Setup2",NULL_KEY);
Play2();
llSetTimerEvent(5.0);
}
if (message == "3")
{
// Set up board for 3 players
llMessageLinked(136,0,"Setup3",NULL_KEY);
Play3();
}
if (message == "4")
{
// Set up board for 4 players
llMessageLinked(135,0,"Setup4",NULL_KEY);
Play4();
}
if (message == "5")
{
// Set up board for 5 players
llMessageLinked(134,0,"Setup5",NULL_KEY);
Play5();
}
if (message == "6")
{
// Set up board for 6 players
llMessageLinked(133,0,"Setup6",NULL_KEY);
Play6();
}
}
else
// check if the message corresponds to a predefined string.
if (message == "Update")
{
llOwnerSay("Update command received");
UpdatePrims();
}
// check if the message corresponds to a predefined string.
if (message == "Memory")
{
llOwnerSay((string)llGetLinkNumber() + " has " + (string)llGetFreeMemory());
}
if (message == "Fill")
{
llOwnerSay("Filling");
integer i;
for (i = 2; i <= 122; i++)
{
llMessageLinked(i,6,"Colored",NULL_KEY);
}
}
if (message == "Global")
{
llOwnerSay("Player0: " + Player0);
llOwnerSay("Player1: " + Player1);
llOwnerSay("Player2: " + Player2);
llOwnerSay("Player3: " + Player3);
llOwnerSay("Player4: " + Player4);
llOwnerSay("Player5: " + Player5);
}
}

link_message(integer sender_num, integer num, string str, key id)
{
list ColorSelectors = ["127","128","129","130","131","132"];
if (llListFindList(ColorSelectors, (list)sender_num) != -1)
{
if (num == 0)
{
llOwnerSay(str + " has selected Red");
Player0 = str;
}
if (num == 1)
{
llOwnerSay(str + " has selected Yellow");
Player1 = str;
}
if (num == 2)
{
llOwnerSay(str + " has selected Green");
Player2 = str;
}
if (num == 3)
{
llOwnerSay(str + " has selected Cyan");
Player3 = str;
}
if (num == 4)
{
llOwnerSay(str + " has selected Blue");
Player4 = str;
}
if (num == 5)
{
llOwnerSay(str + " has selected Magenta");
Player5 = str;
}
llOwnerSay(Player0 + Player1 + Player2 + Player3 + Player4 + Player5);
}
list NextSelectors = ["127","128","129","130"];
if (llListFindList(ColorSelectors, (list)sender_num) != -1)
{
llOwnerSay("Next clicked");
}
}

timer()
{
llOwnerSay(Player0 + Player3);
if(Player0 == "none" || Player3 == "none")
{
llOwnerSay("Waiting for players to select colors.");
// }
// else
// {
llSetTimerEvent(0.0);
}
}
// llMessageLinked(143,0,"abc", NULL_KEY);

}


In the script above Player0 Player1 etc are updated when a player selects a color.
It comes froma link_message.

the global variables Player0 Player1 etc are not updating as the llOwnerSay does not show them updated. The listen event does show them when /42Global is entered.

What am I missing?
Darien Caldwell
Registered User
Join date: 12 Oct 2006
Posts: 3,127
03-16-2007 10:33
It appears your link message handler expects the variable sender_num to be a usable value, but this is actually the number of the prim sending the message. I think you want

CODE

if (llListFindList(ColorSelectors, [num]) != -1)


instead of:

CODE

if (llListFindList(ColorSelectors, (list)sender_num) != -1)


But I could be wrong. Are you intending to send your link messages to specific prims, numbered 127-139 or is that a mistake?
Gregory McLeod
Registered User
Join date: 21 Oct 2006
Posts: 278
03-16-2007 11:14
No. I think that part of the code is correct. prims linked as numbers 127 - 132 are buttons which the user clicks on they have touch_start() events which encode their sender_num which is used by this script to validate the value against a list of color selectors. They thus provide the identity of the colored button clicked on.

The list is used to allow the link_message event to handle further inptus from a different set of prims. The whole consists of 147 prims linked.

What the problem is is the global variables are not being updated by the code segment
CODE

llOwnerSay(str + " has selected Red");
Player0 = str;


why not?
Darien Caldwell
Registered User
Join date: 12 Oct 2006
Posts: 3,127
03-16-2007 11:55
I couldn't answer that without seeing the code you have in the buttons. The code expects the button to send the player name in the passed parameter 'str'. if it's doing that correctly, it should work. However, there is a chance the menu may trigger *before* the link message has a chance to be processed. The link message won't be processed until the listen handler is exited.
Gregory McLeod
Registered User
Join date: 21 Oct 2006
Posts: 278
03-16-2007 15:16
Well that gives me something to go on. The timing of the events may be preventing the processing of the updates is what I think you are saying. The button code does indeed pass the name of the player as can be seen from the llOwnerSay statements before the variables are updated which are working correctly. When a player touches the color selector button their name is passed with the prim link number so that the main code can work out who clicked which color. It is complicated by the fact that the players may all want to click the same button and there has to be a timeout which may also be interfering with the events being passed.

I thought that the scripts in the many prims would all be processing independently from one another and I may need to synchronise them in some way.

thanks for the thought provoking comments.
Erwin Goldblatt
Registered User
Join date: 23 Dec 2006
Posts: 41
03-17-2007 03:56
I dont know if it helps but I myself had exactly this issue with timing a couple weeks ago.

A global variable simply would not update as the request to view it was coming in before somehting else had finished.

The solution in my case was (after advice) to drop the update into a clean state where I knew the update had finished and then proceed.

Maybe not ideal for your solution but a heads up to anyone else who was in my shoes.

Erwin
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
03-17-2007 04:29
I'm not sure that your list checking code will work as you expect it to.
You are checking an integer cast to a list against a string entry within a list.This will not give the expected result. Try this instead

CODE

list ColorSelectors = ["127","128","129","130","131","132"];
if (llListFindList(ColorSelectors, [ (string)sender_num ] ) != -1)



And Please Please Please learn how to use else if!!!!!!
Gregory McLeod
Registered User
Join date: 21 Oct 2006
Posts: 278
03-17-2007 12:04
You are contrasting
CODE

list ColorSelectors = ["127","128","129","130","131","132"];
if (llListFindList(ColorSelectors, (list)sender_num) != -1)

against
CODE

list ColorSelectors = ["127","128","129","130","131","132"];
if (llListFindList(ColorSelectors, [ (string)sender_num ] ) != -1)

but if it were failing none of the code
CODE

{
if (num == 0)
{
llOwnerSay(str + " has selected Red");
Player0 = str;
}
if (num == 1)
{
llOwnerSay(str + " has selected Yellow");
Player1 = str;
}
if (num == 2)
{
llOwnerSay(str + " has selected Green");
Player2 = str;
}

}

would work which it patently is, since the llOwnerSay is working.

The original problem I was having was why the global variables Player0 Player1 etc were not updating and I would like an answer to that problem, if you have one.

I agree that the coding of the ifs after the first should be else ifs,
but in mitigation this is a developing piece of code and still has a
lot of development to go. (poor excuse).
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
03-17-2007 13:51
From: Gregory McLeod
You are contrasting
CODE

list ColorSelectors = ["127","128","129","130","131","132"];
if (llListFindList(ColorSelectors, (list)sender_num) != -1)

against
CODE

list ColorSelectors = ["127","128","129","130","131","132"];
if (llListFindList(ColorSelectors, [ (string)sender_num ] ) != -1)

but if it were failing none of the code
CODE

{
if (num == 0)
{
llOwnerSay(str + " has selected Red");
Player0 = str;
}
if (num == 1)
{
llOwnerSay(str + " has selected Yellow");
Player1 = str;
}
if (num == 2)
{
llOwnerSay(str + " has selected Green");
Player2 = str;
}

}

would work which it patently is, since the llOwnerSay is working.

The original problem I was having was why the global variables Player0 Player1 etc were not updating and I would like an answer to that problem, if you have one.

I agree that the coding of the ifs after the first should be else ifs,
but in mitigation this is a developing piece of code and still has a
lot of development to go. (poor excuse).



From my own experience the code you posted doesnt work.
The llListFindList call always returns -1 since the items are not it the list in the same format.

To prove it try the following hacked together test
CODE

default
{
state_entry()
{
llSay(0, "Hello, Avatar!");
}

touch_start(integer total_number)
{
list col = [ "123","1234","12345" ];
integer fred = 1234;
integer i = llListFindList(col,(list)fred);
llOwnerSay("result is " + (string) i);
fred = 12345;
i = llListFindList(col,[ (string)fred ]);
llOwnerSay("result is " + (string) i);
}
}
Gregory McLeod
Registered User
Join date: 21 Oct 2006
Posts: 278
03-18-2007 03:18
OK I bow to your greater experience and will change the code accordingly.

In the mean time have you any comments on why the updating of the Global variables is NOT happening?