Jump to content

Photo

Is for looping working excellently? And other for loop questions


  • Please log in to reply
2 replies to this topic

#1 LikeLike888

LikeLike888

    Spicy food lover!!

  • Members
  • Real Name:Jason
  • Location:North America

Posted 09 August 2018 - 03:21 AM

#1: Do I need a Waitframe(); in a for loop?


#2: In global script if a for loop is not in a while(true) loop does the for loop continue on?



#3: In an ffc script does a for loop go on forever while Link is on screen with the ffc if such a for loop is not in a while(true) loop?



#4: All up above questions but replace for with do until.



#5: Top 3 up above questions but replace for with the word with.

#2 Emily

Emily

    Scripter / Dev

  • ZC Developers

Posted 09 August 2018 - 07:25 AM

You need Waitframe() in any loop that is infinite; this for the most part means only in while(true) loops. You also need Waitframe() wherever you need to... wait a frame.

 

A for() loop runs until a condition is met. So for instance, for(int i = 0;i<10;i++) will run whatever is in the body of the loop 10 times, with the value of i going up by one each time. Once the condition (in this case, i<10) is no longer met, the loop stops and the code after the loop begins. Do beware creating an infinite loop accidentally; make sure that the condition can be made false by something in the loop, or if it is supposed to be made false by something the player does, be sure to include Waitframe().

 

If anything is BELOW the while(true) loop in a global script, it will not ever run. If it is ABOVE a while(true) loop, it will run when the game starts until the condition is no longer true, and the while(true) loop WILL NOT BEGIN RUNNING until AFTER the for loop is done.

 

I'd recommend checking out some online coding tutorials (java, c++, etc) because loops like this function much the same in those languages as they do in ZScript!



#3 Saffith

Saffith

    IPv7 user

  • ZC Developers

Posted 09 August 2018 - 10:54 AM

Whether a loop needs a Waitframe() or not depends on whether it's mean to run instantly or over a period of time. So, for instance, a while loop that runs for as long as a button is held needs a Waitframe(). A for loop that runs for five seconds needs a Waitframe(). A while loop that increments a pointer to find the end of a string doesn't need one. A for loop that deletes every eweapon on the screen doesn't need one.

Any type of loop can be infinite or not. It simply depends on whether the condition will ever become false.

do-while loops are almost the same as while loops; the difference is that a do-while loop always runs at least once, whether its condition is true or not. They're not used very often in ZScript. Unless you have a specific reason to prefer do-while, use a while loop instead.


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users