There are many but I don't understand why this script is spawning many more than the intended 10 orcs.
The basic idea is that the script will spawn 10 (GlobalInt "eye_orc_guards" == 10) orcs at 4 different waypoint locations, randomly determined. It seems to work fine for that but it doesn't stop at 10 and would probably go on until my PC crashed if I didn't exit the game first.
Can someone set me straight please?
// Determines how many orc guards to spawn and where
void main()
{
// Waypoints for spawns
object oWP1 = GetObjectByTag("wp_01_orcs");
location lWP1 = GetLocation(oWP1);
object oWP2 = GetObjectByTag("wp_02_orcs");
location lWP2 = GetLocation(oWP2);
object oWP3 = GetObjectByTag("wp_03_orcs");
location lWP3 = GetLocation(oWP3);
object oWP4 = GetObjectByTag("wp_04_orcs");
location lWP4 = GetLocation(oWP4);
// Number of orcs to spawn
int iOrcs = GetGlobalInt("eye_orc_guards");
// For each spawned orc
int iOrc = 0;
while (iOrc < iOrcs)
{
// Create orcs
int iRandom = d4();
switch (iRandom)
{
case 1: CreateObject(OBJECT_TYPE_CREATURE,"c_orc_guard",lWP1); break;
case 2: CreateObject(OBJECT_TYPE_CREATURE,"c_orc_guard",lWP2); break;
case 3: CreateObject(OBJECT_TYPE_CREATURE,"c_orc_guard",lWP3); break;
case 4: CreateObject(OBJECT_TYPE_CREATURE,"c_orc_guard",lWP4); break;
}
iOrc ++;
}
}
Gaps in my understanding
Débuté par
El Condoro
, mars 01 2011 08:57
#1
Posté 01 mars 2011 - 08:57
#2
Posté 01 mars 2011 - 09:26
I don't see anything super-obvious, but you should have you script give feedback on what value it thinks iOrcs has (I'd also recommend not giving your variables super-similar names, but that's just me).
Another point of interest, but which shouldn't make or break your script: when you want a loop to run a certain number of times, as opposed to running while it finds a certain condition to be true, you've got the perfect situation to use a for loop.
Another point of interest, but which shouldn't make or break your script: when you want a loop to run a certain number of times, as opposed to running while it finds a certain condition to be true, you've got the perfect situation to use a for loop.
#3
Posté 01 mars 2011 - 10:00
Ah, the for loop - of course. I'll give that a go.
Some testing showed that if I used iOrc = iOrc + 1 it worked. iOrc = iOrc++ would probably work, too, but I haven't tried it. The for loop makes more sense. Thanks.
Some testing showed that if I used iOrc = iOrc + 1 it worked. iOrc = iOrc++ would probably work, too, but I haven't tried it. The for loop makes more sense. Thanks.
#4
Posté 01 mars 2011 - 11:23
iOrc = iOrc + 1;
iOrc += 1;
iOrc++;
and even
++iOrc;
are all supposed to be synonymous.
iOrc += 1;
iOrc++;
and even
++iOrc;
are all supposed to be synonymous.
#5
Posté 01 mars 2011 - 11:44
My understanding of ++ is that the order in which the increment is done varies. This is from the NWN Lexicon:
void main()
{
int nTest = 1;
SpeakString(IntToString(nTest)); // display 1
SpeakString(IntToString(++nTest)); // display 2
SpeakString(IntToString(nTest)); // display 2
SpeakString(IntToString(--nTest)); // display 1
SpeakString(IntToString(nTest)); // display 1
SpeakString(IntToString(nTest++)); // display 1
SpeakString(IntToString(nTest)); // display 2
SpeakString(IntToString(nTest--)); // display 2
SpeakString(IntToString(nTest)); // display 1
}
void main()
{
int nTest = 1;
SpeakString(IntToString(nTest)); // display 1
SpeakString(IntToString(++nTest)); // display 2
SpeakString(IntToString(nTest)); // display 2
SpeakString(IntToString(--nTest)); // display 1
SpeakString(IntToString(nTest)); // display 1
SpeakString(IntToString(nTest++)); // display 1
SpeakString(IntToString(nTest)); // display 2
SpeakString(IntToString(nTest--)); // display 2
SpeakString(IntToString(nTest)); // display 1
}
#6
Posté 01 mars 2011 - 03:15
You almost always do the ++ on it's own line.
++ ALWAYS increments by one, but it varies if it the variable is counted as changed on the current line or afterwards ( you can pass it into a function as it is and increment it for next time. ) Putting it before does it for the current expression or function, while after does it only AFTER it gets to the next line inside the scope you are in.
This is frankly confusing, if not to you, but to anyone else who reads your code. But you need to know it if dealing with others code who might use it.
++ ALWAYS increments by one, but it varies if it the variable is counted as changed on the current line or afterwards ( you can pass it into a function as it is and increment it for next time. ) Putting it before does it for the current expression or function, while after does it only AFTER it gets to the next line inside the scope you are in.
x=1; foo(x++); now x = 2 and foo receives 1 x=1; foo(++x); now x = 2 and foo receives 2 note most understandable and clear way is as follows: x=1 x++; foo(x); --there is no confusion, you know x = 2 and it's no more real work.
This is frankly confusing, if not to you, but to anyone else who reads your code. But you need to know it if dealing with others code who might use it.
#7
Posté 01 mars 2011 - 09:08
Yes indeed, the only difference between these is the order of operations. That's why, to avoid confusion, x++ is usually on its own line, so it ends up as just a short-hand way of saying x += 1 (which in turn is a short-hand for x = x + 1).
Incidentally, iOrc = iOrc++; would probably work, but it's like writing iOrc++ and then iOrc = iOrc;
Incidentally, iOrc = iOrc++; would probably work, but it's like writing iOrc++ and then iOrc = iOrc;
#8
Posté 01 mars 2011 - 09:26
C is a language chock full of confusing syntax, written for the benefit of a compiler, not a human.





Retour en haut






