Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

elseif issues?

Sollie Villota
Registered User
Join date: 22 Jun 2007
Posts: 46
01-03-2008 00:53
I appear to be stumped on why I can't get my ifs and elseifs working...Here's what I am trying to do:

I am building my own vendor for my own use, plus i wanted the challenge, until now... The issue is odd and doesn't make sense at all to me. Here's the code:

CODE:


state maleitems
{
link_message(integer sender_num, integer num, string str, key id)
{
if (num == NEXTARROW)
{
nextarrowmale();
}
}


:ENDCODE

The NEXTARROW thing is the linked script that sends the message "NEXTARROW" on the channel 1, which is what (num == NEXTARROW) is. The problem though is the "nextarrowmale();" statement. Here's what it does:

CODE:
nextarrowmale()
{
if (MALENUM = 0)
{
llSay(0, "DEBUG: " + (string)MALENUM);
MALENUM = 1;
CURRENTTEXTURE = llList2String(MALETEXTURE, 1);
CURRENTINFO = llList2String(MALEINFO, 1);
CURRENTPRICE = (integer)llList2String(MALEPRICE, 1);
//CURRENTITEM = llList2String(MALEITEM, 1);
//CURRENTNOTECARD = llList2String(MALENOTECARD, 1);
return;
}
if (MALENUM = 1)
{
llSay(0, "DEBUG: " + (string)MALENUM);
MALENUM = 2;
CURRENTTEXTURE = llList2String(MALETEXTURE, 2);
CURRENTINFO = llList2String(MALEINFO, 2);
CURRENTPRICE = (integer)llList2String(MALEPRICE, 2);
//CURRENTITEM = llList2String(MALEITEM, 2);
//CURRENTNOTECARD = llList2String(MALENOTECARD, 2);
return;
}
if (MALENUM = 2)
{
llSay(0, "DEBUG: " + (string)MALENUM);
MALENUM = 3;
CURRENTTEXTURE = llList2String(MALETEXTURE, 3);
CURRENTINFO = llList2String(MALEINFO, 3);
CURRENTPRICE = (integer)llList2String(MALEPRICE, 3);
//CURRENTITEM = llList2String(MALEITEM, 3);
//CURRENTNOTECARD = llList2String(MALENOTECARD, 3);
return;
}
if (MALENUM = 3)
{
llSay(0, "DEBUG: " + (string)MALENUM);
MALENUM = 0;
CURRENTTEXTURE = llList2String(MALETEXTURE, 0);
CURRENTINFO = llList2String(MALEINFO, 0);
CURRENTPRICE = (integer)llList2String(MALEPRICE, 0);
//CURRENTITEM = llList2String(MALEITEM, 0);
//CURRENTNOTECARD = llList2String(MALENOTECARD, 0);
return;
}
}
:ENDCODE

When its just four differnet 'if's like i have here it will end up doing ALL four of the things. When I change it to "else if's" it will only do one of the things no matter what, even if I keep clicking it.

To sum up my question: what am I doing wrong? It makes no sense to me.
Viktoria Dovgal
Join date: 29 Jul 2007
Posts: 3,593
01-03-2008 01:12
Make sure that you use == for equality tests, a single = is for assignment.
Nika Talaj
now you see her ...
Join date: 2 Jan 2007
Posts: 5,449
01-03-2008 01:17
I'm not sure what the problem is. When they are all "if"s as above, MALENUM gets set within every if clause so as to make the next if clause true, so yes, executing all 4 would be the expected behavior (tho clearly not what you want, lol!).

When you make them "elseifs", then only the one for the current value of MALENUM would execute, and presumably each time you click it a different elseif clause is executed, rotating as you increment MALENUM. Isn't that what you want to have happen? Or do you mean that's not what you see happening :confused:
Sollie Villota
Registered User
Join date: 22 Jun 2007
Posts: 46
01-03-2008 01:20
Yeah I just realized I didn't do that, so I fixed it and guess what? Still the same problem, but now its not EVEN WORKING. Doesn't make sense at all, unless I shoudln't be doing it the way I am doing it? Maybe I should just put the if statements straight in the original link message thing? I shall try that!
Nika Talaj
now you see her ...
Join date: 2 Jan 2007
Posts: 5,449
01-03-2008 01:22
?? with the statements all "if"s as above, you set the value of MALENUM within each clause so as to make the next if true, so indeed all 4 should execute, if you get to the code at all. I suspect your issue may be in the event or linkmessage setup, which we don't see here ...
Sollie Villota
Registered User
Join date: 22 Jun 2007
Posts: 46
01-03-2008 01:24
Ahh, I fixed it. I really don't understand what I did, but i just moved some things around and "POOF" it works. Thanks.
Squirrel Wood
Nuteater. Beware!
Join date: 14 Jun 2006
Posts: 471
01-03-2008 01:48
Instead of

if (MALENUM = 3)

use:

if (3 = MALENUM)

and upon compiling you will be given a syntax error.

Then correct your code to

if (3 == MALENUM)

and there will be much rejoicing!
Meade Paravane
Hedgehog
Join date: 21 Nov 2006
Posts: 4,845
01-03-2008 07:47
Can you post the new code? I'm really curious why the return inside each if didn't take..

For your next verison, I suggest something like this might be easier to manage..
CODE

nextarrowmale()
{
llSay(0, "DEBUG: " + (string)MALENUM);

if (MALENUM == 3)
{
MALENUM = 0;
}
else
{
++MALENUM;
}

CURRENTTEXTURE = llList2String(MALETEXTURE, MALENUM);
CURRENTINFO = llList2String(MALEINFO, MALENUM);
CURRENTPRICE = (integer)llList2String(MALEPRICE, MALENUM);
//CURRENTITEM = llList2String(MALEITEM, MALENUM);
//CURRENTNOTECARD = llList2String(MALENOTECARD, MALENUM);
}
_____________________
Tired of shouting clubs and lucky chairs? Vote for llParcelSay!!!
- Go here: http://jira.secondlife.com/browse/SVC-1224
- If you see "if you were logged in.." on the left, click it and log in
- Click the "Vote for it" link on the left
Wyatt Burton
Registered User
Join date: 11 Jan 2007
Posts: 49
01-03-2008 19:14
Since we can't see all the code properly we can only guess what is going on. A guess from me is that your nextarrowmale() function is getting called repeatedly after the return.

Also in case you didnt know it
if (x=0) {...}
will always be false
but if (x= any non zero){...}
will always return true.

Of course this was unintentional and you meant to use the == insetad of the assignement (=).

P.S. This is not the best way to write this code You can do it without using ifs. Just create list arays of your textures, prices, blah blah ad use MALENUM to extract the value from the lists. It will shirnk your code and make it more readable.