Aller au contenu

Photo

While Loop Error


  • Veuillez vous connecter pour répondre
12 réponses à ce sujet

#1
Buddywarrior

Buddywarrior
  • Members
  • 256 messages
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.

#2
Baragg

Baragg
  • Members
  • 271 messages
What are you trying to do?


#3
Baragg

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

#4
Buddywarrior

Buddywarrior
  • Members
  • 256 messages
 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++;}


#5
Buddywarrior

Buddywarrior
  • Members
  • 256 messages
bah that looks ugly. (both in IE and Chrome). Hope it's just me.

#6
GhostOfGod

GhostOfGod
  • Members
  • 863 messages
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 .


#7
Buddywarrior

Buddywarrior
  • Members
  • 256 messages

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.:)

#8
Lightfoot8

Lightfoot8
  • Members
  • 2 535 messages
The Best thing to do would be to post the original script. We could then walk you through how to rewrite it.

#9
Buddywarrior

Buddywarrior
  • Members
  • 256 messages

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;
    }
}


#10
_Knightmare_

_Knightmare_
  • Members
  • 643 messages
EDIT: Sorry wrong post

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


#11
Baragg

Baragg
  • Members
  • 271 messages
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 .


#12
Baragg

Baragg
  • Members
  • 271 messages
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 .


#13
_Knightmare_

_Knightmare_
  • Members
  • 643 messages
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.