Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Possible to exit a FOR-NEXT loop in the middle or after finding what you need?

Omega gamer 89
Years of Service
User Offline
Joined: 10th Sep 2007
Location: Pittsburgh, PA
Posted: 1st Jun 2021 19:32 Edited at: 1st Jun 2021 19:43
Ok, so I have an array called PLAYERINVENTORY() that stores a list of everything the player has. I used a custom type so that each entry has additional fields such as item name, item type, amount, etc.

Right now Im working on a section where I need to search through each entry in the PLAYERINVENTORY array to see if any of them match a certain item, and if one does, then you do a certain thing. THAT part I have worked out. The part Im wondering is, whats the best way to break out of that FOR-NEXT loop once I find the entry I need, so I dont pointlessly loop through all the remaining entries in the array?
In other words, lets say the array has 100 entries. Normally, the FOR-NEXT loop would go through each one. But, if I find the item I need in, say, the 10th entry, theres no need to still go through the remaining 90 entries after that. its just wasted time and could potentially slow the game down with a long enough list.

Whats the best way to break out of that FOR-NEXT loop? So far the only thing Ive come up with is having a GOTO command after the code finds the correct item in the array and does what it needs to do, but Im hesitant to use GOTO because I hear they tend to get messy. Any ideas? Psuedo code included below. Id rather not post the actual code because its pretty long and has a lot of media it depends on and other little things that make it difficult to share like that.

So if you look at this code you can see that, if not for the GOTO command, FOR-NEXT loop would repeat 14 more times AFTER we've already achieved what we were trying to do. This isnt a huge problem with 16 items in the array, but once it gets to hundreds or thousands of items... Well, Id rather avoid repeating the loop hundreds of times AFTER Ive already found the entry Im looking for.
The GOTO command is the bast way Ive been able to come up with so far to break from the FOR-NEXT loop early, but I just feel like there must be a better way of doing it. I considered using the EXIT command, but the help files says that EXIT only works for loops with uncertain exit conditions, which (to my understanding) does not include FOR-NEXT, since that has a predetermined number of loops itll run through.
Years of Service
User Offline
Joined: 23rd Mar 2005
Location: Hertfordshire, England
Posted: 2nd Jun 2021 04:51
AGK Developer
Years of Service
User Offline
Joined: 21st Jan 2009
Location: Sarasota, FL
Posted: 2nd Jun 2021 13:55

If [condition] then a = array count(PlayerInventory() )
LynxJSA's web games/quizzes - LynxJSA's Android apps
AGK Resource Directory
"Stick to a single main loop (DO...LOOP) and loop through it every frame.
Do everything inside functions.
Use finite state machines to control your game.
Use lots and lots of source files.
Use virtual resolution instead of the default percentage system." - Digital Awakening
Omega gamer 89
Years of Service
User Offline
Joined: 10th Sep 2007
Location: Pittsburgh, PA
Posted: 2nd Jun 2021 16:39
Quote: "Break"

I had considered that, but the help files say that that command exits the program completely when running in debug mode. It doesnt do what Im trying to do here.

Quote: "If [condition] then a = array count(PlayerInventory() ) "

And now I feel stupid. XD
Thank you. Thats one of those things that, once you hear it, its like "Wow, why on earth didnt I think of that right away?" Thatll work perfectly. Thank you.
Years of Service
User Offline
Joined: 20th Dec 2015
Posted: 2nd Jun 2021 17:48
judging from this quick test, exit seems to work fine with for loops.

Bored of the Rings
User Banned
Posted: 2nd Jun 2021 18:22
Break is for debug only, exit works fine with a loop once a condition is met.
Professional Programmer, languages: SAS, C++, SQL, PL-SQL, DBPro, Purebasic, JavaScript, others
Years of Service
User Offline
Joined: 16th May 2009
Location: UK
Posted: 13th Jun 2021 23:36
Just a FYI, there is some kind of compiler parsing bug with exit in that if you have ANY whitespace after it, it will give a compilation error. Unless you also have a comment using ` after the whitespace. I'm still using Synergy so it may be related to the IDE rather than the compiler.

Also, exit can be used to umm.. exit any kind of loop, not just a

Login to post a reply

Server time is: 2023-11-28 11:14:56
Your offset time is: 2023-11-28 11:14:56