UGX-Mods

Call of Duty: Black Ops 3 => Help Desk => Scripting => Topic started by: Doodles_Inc on September 16, 2017, 08:31:29 pm

Title: Stop a loop?
Post by: Doodles_Inc on September 16, 2017, 08:31:29 pm
You might think that this is a joke, but, I really suck at scripting since I started it this week, and I need help so, after the loop is completed, he stops, thanks in advance!
If you're wondering how my code is looking... here it is: https://pastebin.com/dJ45jkGa

Double Post Merge: September 16, 2017, 10:54:21 pm
Made it, you can see it in my pastebin.
Title: Re: Stop a loop?
Post by: Wolfilms on September 17, 2017, 03:52:52 pm
It looks like you are waiting for a player to enter 2 zones at once?
Title: Re: Stop a loop?
Post by: Doodles_Inc on September 17, 2017, 10:58:00 pm
It looks like you are waiting for a player to enter 2 zones at once?
Not at once, I managed to finish the script, I was only waiting for someone to answer so I could put this thread as solved.
Title: Re: Stop a loop?
Post by: Wolfilms on September 18, 2017, 12:08:45 am
oh ok
Title: Re: Stop a loop?
Post by: Archaicvirus on November 30, 2017, 01:23:41 am
I know you solved the issue, but I don't think you fully understand how while() loops work. Figured I'd explain a little to help you out.

Everything inside {curly braces} will loop forever until while(true) is evaluated as false - unless you call a break. It will work the way you're doing it (saw the pastebin, lol btw) however it is completely unnecessary if you call a wait() statement.

Basically, loops in general are intended to repeat or iterate a block of code until some condition is met. Logically if you're waiting() for a zone entrance, what code do you need to repeat? None. When you call waittill("zone_enter"), the script halts and does not process the next line until that notification is recieved, thus making a loop irrelevant. Then the next waittill("zone_enter") gets called and halts again until the player enters. So just get rid of the loop altogether, and write your code as normal. Ill give you a few examples for the sake of it. I'm no expert coder, but I can teach what I know.

Code Snippet
Plaintext
//This example counts 10 player slide actions, prints the total to the screen, then breaks out of the loop & ends the function.
function example_loop()
{
slides = 0;
max_slides = 10;
players = GetPlayers();
player = players[0];

while(1)
{
if(player isSliding())
{
slides++;
IPrintLnBold("[" + slides + "] Slides have been detected");
if(slides >= max_slides)
{
break;
}
}
wait(1);
}
}

//This ex. uses a for() loop, counts & prints the array of zombies alive. You don't need a break statement as 'i' counts up with zombies.size.
//Once 'i' is greater than zombies.size, then middle statement in the loop becomes false, breaking the loop.
function example_loop2()
{
zombies = GetAISpeciesArray("axis", "all");

for(i = 0; i < zombies.size; i++)
{
IPrintLnBold("[" + i + 1 + "] zombies have been detected");
}

}

//Or you could do
function example_loop3()
{
i = 0;
foreach(zombie in GetAISpeciesArray("axis", "all"))
{
IPrintLnBold("[" + i + 1 + "] zombies have been detected");
i++;
}
}