functions with two parameters
|
|
Gregory McLeod
Registered User
Join date: 21 Oct 2006
Posts: 278
|
04-17-2007 03:24
I am being particularly thick this morning but I can't see how to return two parameters from a function. What I need is to have a function execute and return two parameters or arguments.
|
|
Twisted Pharaoh
if ("hello") {"hey hey";}
Join date: 24 Mar 2007
Posts: 315
|
04-17-2007 03:31
Many ways of doing it, depending on what exactly you need, the first which pops to my mind is to use global variables :
integer Result_val ; string Result_string ;
get_two_results() { Result_val = 3 ; Result_string = "test" ; }
call_two_results() { integer myval ; string mystring ;
get_two_results() ; myval = Result_val ; mystring = Result_string ;
}
Or you could return a list, or encode the results in a string, and so on...
|
|
Gregory McLeod
Registered User
Join date: 21 Oct 2006
Posts: 278
|
04-17-2007 03:43
Thanks for the thought. I think however what I am looking for is a more general function method. For example a function which accepts two arguments and manipulates them both. a simple case might be a push function which puts a new value onto a stack.
push(string target, string newvalue) results in a modification of target by adding newvalue to the front.
or pull(string source, string firstvalue) results in source being stripped of the first element and gives the stripped portion to result.
If it has to be global variables the functions cannot be generalised to operate on more than one source or target.
Do I make my problem clearer?
|
|
Twisted Pharaoh
if ("hello") {"hey hey";}
Join date: 24 Mar 2007
Posts: 315
|
04-17-2007 04:00
For push it's obvious but you're right for pull you are in trouble if you want both the truncated list and the value at the top to be returned. But you can still use global variables as long as you retrieve them right away after the call. list just_a_list ; list yet_another_list ;
list temp_list ;
list push(list target, string value) { target += value ; return target ; }
string pull(list source) { string value ; value = llList2String(source, 0) ; temp_list = llDeleteSubList(source, 0, 0) ; return value ; }
example_call() { string mystring ;
just_a_list= push(just_a_list, "this") ; yet_another_list= push(yet_another_list, "is a test") ; mystring = pull(just_a_list) ; just_a_list = temp_list ; // Don't forget to do this after each call to pull() mystring = pull(yet_another_list) ; yet_another_list = temp_list ; // Don't forget to do this after each call to pull() }
I know it looks like a bad hack but it works xD.
|
|
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
|
04-17-2007 04:09
LSL uses pass by value not pass by reference for its model. Because of this you cannot manipulate any of your passed in parameters and see the results out side of the function call unless, as Twisted states, you use global variables. The only obvious exception is the returned value.
|
|
Squirrel Wood
Nuteater. Beware!
Join date: 14 Jun 2006
Posts: 471
|
04-17-2007 05:20
You can return more than one variable if you return them as a string. Simply pack them into one and then parse said string accordingly.
string strRet = "Yada|" + (string) intVal1 + "|" + (string)fltVal2 + "|" + (string)vctVector return strRet
and then do...
list lstRetVal = llParseString2List(YourFunction,["|"],[""]);
|
|
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
|
04-17-2007 05:48
From: Squirrel Wood You can return more than one variable if you return them as a string. Simply pack them into one and then parse said string accordingly.
string strRet = "Yada|" + (string) intVal1 + "|" + (string)fltVal2 + "|" + (string)vctVector return strRet
and then do...
list lstRetVal = llParseString2List(YourFunction,["|"],[""]); True but the additional time over head packing / unpacking means that unless your very tight for memory I'd rather use a global.
|
|
Gregory McLeod
Registered User
Join date: 21 Oct 2006
Posts: 278
|
04-17-2007 06:42
Thanks to all, when I had woken up I realised the pass by value not pass by reference is the root of my problem. In my specific case I think the global variable is my solution although it is to save numeric values 0 < value < 999. So I think I will further compromise by using 3 digit numeric strings with offsets calculated in multiples of 3 to save processing overload of parsing, and typecast the result.
Thanks again.
|
|
Twisted Pharaoh
if ("hello") {"hey hey";}
Join date: 24 Mar 2007
Posts: 315
|
04-17-2007 07:06
You can do this with integers: integer encode(integer a, integer b) { return (a*1000)+ b ; }
integer decode_a(integer e) { return (e / 1000) ; }
integer decode_b(integer e) { return (e%1000) ; }
default { state_entry() { }
touch_start(integer total_number) { integer test ; test = encode(432, 865) ; llSay(0, "a="+(string)decode_a(test)+ " b="+(string)decode_b(test)); } }
|