Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

problem with a function

Archetypus Deed
(title goes here)
Join date: 13 Sep 2009
Posts: 13
09-24-2009 21:52
Here is the function that I am trying to use:

vector getOffset(string s)
{
//vector offsetV = <0.0,0.0,0.0>;
if (s="A";)
{
offsetV = < .25, .50, 0.0>;
}
else if (s="B";)
{
offsetV = < 0.0, 0.0, 0.0>;
}
else if (s="C";)
{
offsetV = < 0.25, 0.0, 0.0>;
}
else if (s="D";)
{
offsetV = < 0.5, 0.0, 0.0>;
}
else if (s="E";)
{
offsetV = < -0.25, 0.0, 0.0>;
}
else if (s="F";)
{
offsetV = < 0.0, .25, 0.0>;
}
else if (s="G";)
{
offsetV = < .25, .25, 0.0>;
}
else if (s="H";)
{
offsetV = < 0.5, .25, 0.0>;
}
else if (s="I";)
{
offsetV = < -.25, .25, 0.0>;
}
else if (s="J";)
{
offsetV = < 0.0, 0.5, 0.0>;
}
else
{
offsetV = <0.5, 0.5, 0.0>;
}
return offsetV;
}

The problem is that no matter what the input, the outcome is still as though the function was given "A" to evaluate. The first line that is commented out, was to see if the function was working at all.

I'm betting its something obvious, but what am I missing here?
ab Vanmoer
Registered User
Join date: 28 Nov 2006
Posts: 131
09-24-2009 22:11
A comparison operator is signified by == not =
eg
CORRECT if (s=="A";)
WRONG if (s="A";)

The statement if(s="A";) first assigns "A" to s, it is then evaluated for truth so will always return true.
Archetypus Deed
(title goes here)
Join date: 13 Sep 2009
Posts: 13
09-24-2009 22:36
OMG I feel kinda silly now. Thanks.

(I think I should get more sleep...lol)
ab Vanmoer
Registered User
Join date: 28 Nov 2006
Posts: 131
09-24-2009 23:20
From: Archetypus Deed
OMG I feel kinda silly now. Thanks.
(I think I should get more sleep...lol)

A common enough mistake - we have all been there :)
Kaluura Boa
Polygon Project
Join date: 27 Mar 2007
Posts: 194
09-25-2009 00:31
And instead of a bunch of if/else if, you may want to simplify.

vector getOffset(string s)
{
string trigger = "ABCDEFGHIJ";
list value = [< .25, .50, 0.0>, ZERO_VECTOR, < 0.25, 0.0, 0.0>,
< 0.5, 0.0, 0.0>, < -0.25, 0.0, 0.0>, < 0.0, .25, 0.0>,
< .25, .25, 0.0>, < 0.5, .25, 0.0>, < -.25, .25, 0.0>,
< 0.0, 0.5, 0.0>];

integer i = llSubStringIndex(trigger, s); // Number of the letter in string 'trigger'
if (i == -1) { return <0.5, 0.5, 0.0>; } // Not found (Your final else)
else { return llList2Vector(value, i); } // Otherwise return the matching value
}

If your string 's' is not always just a letter, you can still remain very simple:

vector getOffset(string s)
{
list trigger = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"];
list value = [< .25, .50, 0.0>, ZERO_VECTOR, < 0.25, 0.0, 0.0>,
< 0.5, 0.0, 0.0>, < -0.25, 0.0, 0.0>, < 0.0, .25, 0.0>,
< .25, .25, 0.0>, < 0.5, .25, 0.0>, < -.25, .25, 0.0>,
< 0.0, 0.5, 0.0>];

integer i = llListFindList(trigger, );
if (i == -1) { return <0.5, 0.5, 0.0>; }
else { return llList2Vector(value, i); }
}

My QA department disapproves the intermediary variables but it should be easier to understand. ;)