Author Topic: While Loop Error  (Read 606 times)

Legacy_Buddywarrior

  • Hero Member
  • *****
  • Posts: 512
  • Karma: +0/-0
While Loop Error
« on: February 11, 2011, 07:15:59 pm »


               I'm getting an error, and I'm not sure why. Could somebody explain why i'm getting this error?
"ERROR: PARSING VARIABLE LIST " 



int i = 1;while(i<6){//string CSPAWN1  = GetLocalString(oTarget, "CSPAWN1"); //Make this work using loop.string CSPAWN+(IntToString(i)) = GetLocalString(oTarget, "CSPAWN"+(IntToString(i)));i++;}

When I run it as: 
string CSPAWN = GetLocalString(oTarget, "CSPAWN"+(IntToString(i)));
It compiles, but I don't see why the string CSPAWN+(IntToString(i)) logic is wrong.

Thanks for any clearification.
               
               

               
            

Legacy_Baragg

  • Sr. Member
  • ****
  • Posts: 496
  • Karma: +0/-0
While Loop Error
« Reply #1 on: February 11, 2011, 07:50:52 pm »


               What are you trying to do?

               
               

               
            

Legacy_Baragg

  • Sr. Member
  • ****
  • Posts: 496
  • Karma: +0/-0
While Loop Error
« Reply #2 on: February 11, 2011, 07:54:07 pm »


               It seems your trying to use a new variable CSPAWN+IntToString(i) without first defining it.
               
               

               
            

Legacy_Buddywarrior

  • Hero Member
  • *****
  • Posts: 512
  • Karma: +0/-0
While Loop Error
« Reply #3 on: February 11, 2011, 08:09:56 pm »


                first, I apologize for the
printing odd. It wasn't that way when I submitted it, with their was a preview.

I'm just trying to learn to use a while look to scan threw all the local variables named CSPAWN1 - CSPAWN6. 

I was typing it all out an figured I could use a loop to do the work for me.

I thought the string CSPAWN+(IntToString(i)) = GetLocalString(oTarget, "CSPAWN"+(IntToString(i)));was what is defining CSPAWN

Here's the code again, maybe it will print out correctly this time.

[code]int i = 1;while(i<6){string CSPAWN+(IntToString(i)) = GetLocalString(oTarget, "CSPAWN"+(IntToString(i)));i++;}

               
               

               
            

Legacy_Buddywarrior

  • Hero Member
  • *****
  • Posts: 512
  • Karma: +0/-0
While Loop Error
« Reply #4 on: February 11, 2011, 08:14:21 pm »


               bah that looks ugly. (both in IE and Chrome). Hope it's just me.
               
               

               
            

Legacy_GhostOfGod

  • Hero Member
  • *****
  • Posts: 1490
  • Karma: +0/-0
While Loop Error
« Reply #5 on: February 11, 2011, 09:49:54 pm »


               The thing is when you declare a variable, and in this case a string variable:
You first say its going to be a string:

string

Then you give this string a name, and in this case you are trying to put a function in with the naming part which you cant do. If you want to name your string CSPAWN you can:

string CSPAWN

then you put either leave it at that and put a semicolon and it will be declared or you can go ahead and define it now with the =:

string CSPAWN;
string CSPAWN = whatever;

And if we just declare it first:

string CSPAWN;

Then you define it later:

CSPAWN = whatever;

You just can use any function on the naming side(left of =) cause all your doing is giving it a name(declaring it).
Your loops might look something more like so:

void main()
{
        object oTarget = OBJECT_SELF;//or whatever target is
        int iInt = 1;
        string sCSPAWN = GetLocalString(oTarget, "CSPAWN" + IntToString(iInt));

        while (iInt <= 6)
        {
                //do whatever
                iInt++;
                sCSPAWN = GetLocalString(oTarget, "CSPAWN" + IntToString(iInt));
        }
}

But it would help to know specifically what you are trying to do to see if this is the right approach.
Hope it helps.
               
               

               


                     Modifié par GhostOfGod, 11 février 2011 - 09:52 .
                     
                  


            

Legacy_Buddywarrior

  • Hero Member
  • *****
  • Posts: 512
  • Karma: +0/-0
While Loop Error
« Reply #6 on: February 11, 2011, 10:57:25 pm »


               

GhostOfGod wrote...

But it would help to know specifically what you are trying to do to see if this is the right approach.
Hope it helps.


Thanks for the info. I've added variables of a creatures ResRef (or tag, I forget w/o being at the game) to an Area Trigger, so when oPC walks into the area it spawns a creature at a waypoint. It has been working nicely, but I wanted to shorten the script (and learn how to do it to be honest) by using the loop instead of having a ton of lines.

Thanks again.'<img'>
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
While Loop Error
« Reply #7 on: February 11, 2011, 11:23:08 pm »


               The Best thing to do would be to post the original script.  We could then walk you through how to rewrite it.
               
               

               
            

Legacy_Buddywarrior

  • Hero Member
  • *****
  • Posts: 512
  • Karma: +0/-0
While Loop Error
« Reply #8 on: February 12, 2011, 12:36:01 am »


               

Lightfoot8 wrote...

The Best thing to do would be to post the original script. We could then walk you through how to rewrite it.

Alright.. You asked for it.


void main()
{
object oPC = GetEnteringObject();
    if (!GetIsPC(oPC)) return;

object oTarget;
object oSpawn;
location lTarget;
string WAYPOINT1  = GetLocalString(OBJECT_SELF, "WAYPOINT1");
 
oTarget = GetWaypointByTag(WAYPOINT1);
lTarget = GetLocation(oTarget);
string CSPAWN1 = GetLocalString(oTarget,"CSPAWN1");//the variable on the waypoint of what creature it is to spawn.

if (GetLocalInt(oTarget, "DO_ONCE")== 0) //Let's not have it spawn twice.
   {
    oSpawn = CreateObject(OBJECT_TYPE_CREATURE, CSPAWN1, lTarget); //creature sets it back to 0.
    SetLocalInt(oTarget,"DO_ONCE",1);
       return;
    }
}

               
               

               
            

Legacy__Knightmare_

  • Full Member
  • ***
  • Posts: 191
  • Karma: +0/-0
While Loop Error
« Reply #9 on: February 12, 2011, 03:03:54 am »


               EDIT: Sorry wrong post
               
               

               


                     Modifié par _Knightmare_, 12 février 2011 - 03:12 .
                     
                  


            

Legacy_Baragg

  • Sr. Member
  • ****
  • Posts: 496
  • Karma: +0/-0
While Loop Error
« Reply #10 on: February 12, 2011, 04:28:05 am »


               Here try this I commented on it a bit.

[nwscript]
void main()
{
object oPC = GetEnteringObject();//establish oPC as the entering object

   if (!GetIsPC(oPC)) return;//if oPC is not a PC end here

object oTarget = GetWaypointByTag("WAYPOINT1");//establish oTarget as a waypoint with "WAYPOINT1" as its tag
location lTarget = GetLocation(oTarget);//get the location of the oTarget
string CSPAWN1 = GetLocalString(oTarget,"CSPAWN1");
//the string variable set on the waypoint of  a creature resref to spawn.
int nNumber = 6;//number of times we want to the loop to run
object oSpawn;//establish this object variable for later use in the loop


if (GetLocalInt(oTarget, "DO_ONCE")== 0) return;//if already been done stope here


while(nNumber != 0)//only loop if nNumber DOES NOT equal 0
{
   oSpawn = CreateObject(OBJECT_TYPE_CREATURE, CSPAWN1, lTarget); //spawn critter
   nNumber = nNumber-1;//reduce count by 1
}

//loop should be done so set DO_ONCE to 1 now
SetLocalInt(oTarget, "DO_ONCE", 1);

}[/nwscript]
               
               

               


                     Modifié par Baragg, 12 février 2011 - 04:29 .
                     
                  


            

Legacy_Baragg

  • Sr. Member
  • ****
  • Posts: 496
  • Karma: +0/-0
While Loop Error
« Reply #11 on: February 12, 2011, 04:30:56 am »


               Loops took me a while to get a hold onto. You have different types do, while, and for loops. While loops are good for most things, keep tyring you will get it eventually.

Edit(to add Lexicon link):

Here this will greatly help with scripting:  http://www.nwnlexicon.com/
               
               

               


                     Modifié par Baragg, 12 février 2011 - 04:33 .
                     
                  


            

Legacy__Knightmare_

  • Full Member
  • ***
  • Posts: 191
  • Karma: +0/-0
While Loop Error
« Reply #12 on: February 12, 2011, 07:50:57 pm »


               My tutorial linked in sig below has sections on using both "while" and "for" loops as well. Written with NWN2 in mind, but the vast majority of the tutorial is valid for NWN1 as well.