SA-MP Forums Archive
Reverse loop fails. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Reverse loop fails. (/showthread.php?tid=176103)



Reverse loop fails. - MidnightHippie - 12.09.2010

Whenever I try to use this decremental loop to spawn NPCs.. It only spawns one NPC with ID 499.

Code:
for(new i = 499; i >= 485;i--)
{
    CreateNPC(i,"Suspect");
}
What on earth is wrong with this loop? :S


Re: Reverse loop fails. - CuervO - 12.09.2010

Problem solved with me on MSN. Feel free to lock or remove


Re: Reverse loop fails. - Simon - 12.09.2010

It's probably the fact they all have the same name, try doing this:

pawn Code:
new
    szNPCName[MAX_PLAYER_NAME],
    szIntBuf[4];

for(new i = 499; i >= 485; i--)
{
    // Set the start of string to "Suspect" for each
    // new NPC.
    szNPCName = "Suspect";

    // Convert i to a string, so we can add
    // it to the name of NPC.
    valstr(szIntBuf, i);

    // Add the number (in string form) to the
    // name "Suspect", to get result of "SuspectX"
    strcat(szNPCName, szIntBuf);

    // Create the NPC with the name that has
    // been generated.
    CreateNPC(i, szNPCName);
}
edit: Grr, I miss the "New post" warning from SMF. What was your solution?


Re: Reverse loop fails. - CuervO - 12.09.2010

pawn Code:
for(new i=0;i<99;i++)
{
    new n=i+400;
    CreateNpc(n, "Suspect");
    //rest of code which is not meant to be released
}