Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Avoiding repeating if/else on link_message

Jack Moseley
Registered User
Join date: 24 Aug 2004
Posts: 39
10-05-2004 12:43
I have a script with multiple states that all need to listen for link_messages. These link messages instruct the script to switch states. I'd like to avoid duplicating my if/else tree for different link_message commands in each state. Is there any way to accomplish this using a gobal function?
Moleculor Satyr
Fireflies!
Join date: 5 Jan 2004
Posts: 2,650
10-05-2004 13:00
From: Jack Moseley
I have a script with multiple states that all need to listen for link_messages. These link messages instruct the script to switch states. I'd like to avoid duplicating my if/else tree for different link_message commands in each state. Is there any way to accomplish this using a gobal function?


CODE
integer a;
integer b;
integer blah;
string UselessExampleVariable = "Hiya! I suck!";

GlobalFunction1(integer WooHoo)
{
llSay(0,(string)WooHoo);
}
integer GlobalFunction2(string message)
{
if(TRUE)
{
state one;
}
else
{
state two;
}
}

state default
{
CODE HERE
}
state one
....


I have NO clue if state changes works like that, but that's how you do global functions.

(And that code is untested. I just wrote it up here in the editor.)
_____________________
</sarcasm>
Ace Cassidy
Resident Bohemian
Join date: 5 Apr 2004
Posts: 1,228
10-05-2004 13:54
From: Moleculor Satyr
CODE
integer a;
integer b;
integer blah;
string UselessExampleVariable = "Hiya! I suck!";

GlobalFunction1(integer WooHoo)
{
llSay(0,(string)WooHoo);
}
integer GlobalFunction2(string message)
{
if(TRUE)
{
state one;
}
else
{
state two;
}
}

state default
{
CODE HERE
}
state one
....


I have NO clue if state changes works like that, but that's how you do global functions.

(And that code is untested. I just wrote it up here in the editor.)



State changes within global functions are a no-no. There is a hack written up on the Wiki, but I would not suggest using it, since future versions of the script engine might break such scripts.

The reason for not allowing state changes in a global function is probably related to stack unwinding.

- Ace
_____________________
"Free your mind, and your ass will follow" - George Clinton
Jack Moseley
Registered User
Join date: 24 Aug 2004
Posts: 39
10-05-2004 15:05
Ya, that was my problem... can't do a state change within a global function... Is there any way short of simply duplicating the if/else in every state?
Kurt Zidane
Just Human
Join date: 1 Apr 2004
Posts: 636
10-05-2004 17:25
do you mean like this?

CODE
commandNexus(message)
{
if ( message == "stop" )
{
state default;
}
else
{
state active;
}
}

default
{
state_entry()
{

}

link_message(integer sender_num, integer num, string message, key id)
{
commandNexus(message);
}
}

active
{
state_entry()
{

}

link_message(integer sender_num, integer num, string message, key id)
{
commandNexus(message);
}
}
Jack Moseley
Registered User
Join date: 24 Aug 2004
Posts: 39
10-05-2004 17:59
You can't change state from within a global function...
Ama Omega
Lost Wanderer
Join date: 11 Dec 2002
Posts: 1,770
10-05-2004 20:24
I havn't tested it and it may add too much delay, but you could store the state to jump to in a global variable, then jump to a transition state which had all the ifs n elses and jumped.

CODE
string jumpto;

default
{
listen(integer chan, string name, key id, string mes)
{
jumpto = mes;
state transition;
}
}

state transition
{
state_entry()
{
if (jumpto = "one") state one;
else if (jumpto = "two") state two;
}
}
// etc...
_____________________
--
010000010110110101100001001000000100111101101101011001010110011101100001
--
Azelda Garcia
Azelda Garcia
Join date: 3 Nov 2003
Posts: 819
10-06-2004 05:14
Errrr... cant you just use a variable for the state name?

CODE

state randomstate
{
...
linkmessage( ... )
{
list arguments = llParseStringKeepNulls( sMessage, ["-=-"], [] );
string Command = llList2String( arguments, 0 );

if( Command == "CHANGESTATE" )
{
state llList2String( arguments, 1 );
}
}
}
_____________________
Azelda Garcia
Azelda Garcia
Join date: 3 Nov 2003
Posts: 819
10-06-2004 05:16
Edit: by the way, note that my post does not imply endorsement of using states.
_____________________
Rysidian Rubio
Ruby Red Head
Join date: 14 Jan 2004
Posts: 263
10-07-2004 19:02
From: Azelda Garcia
Errrr... cant you just use a variable for the state name?

Nope, you can't. Would be VERY good if you could tho. would save alot of IF/ELSEs for some games I have which have around 10 states in some scripts.
Eckhart Dillon
Registered User
Join date: 4 Jan 2004
Posts: 22
You can change states in global function.
10-10-2004 02:37
From: Jack Moseley
Ya, that was my problem... can't do a state change within a global function... Is there any way short of simply duplicating the if/else in every state?


For some reason if you want to change states in a global function the state needs to be in an if statement.

if(number = 1)
{
state fly;
}
else if(TRUE)
{
state sink;
}

Just use if(TRUE).
Its really strange.