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.
TYPE Item
ItemName as string : Amount as integer
ENDTYPE
DIM PlayerInventory(16) as item
PlayerInventory(0).ItemName = "knife"
PlayerInventory(0).Amount = 1
PlayerInventory(1).ItemName = "Ammo"
PlayerInventory(1).Amount = 100
PlayerInventory(2).ItemName = "Potion"
PlayerInventory(2).Amount = 14
PlayerInventory(3).ItemName = "Revive"
PlayerInventory(3).Amount = 2
PlayerInventory(4).ItemName = "Full Heal"
PlayerInventory(4).Amount = 1
PlayerInventory(5).ItemName = "Hi-Potion"
PlayerInventory(5).Amount = 4
PlayerInventory(6).ItemName = "Antidote"
PlayerInventory(6).Amount = 6
PlayerInventory(7).ItemName = "Rope"
PlayerInventory(7).Amount = 2
PlayerInventory(8).ItemName = "Flint"
PlayerInventory(8).Amount = 19
PlayerInventory(9).ItemName = "Firewood"
PlayerInventory(9).Amount = 11
PlayerInventory(10).ItemName = "Hammer"
PlayerInventory(10).Amount = 1
PlayerInventory(11).ItemName = "axe"
PlayerInventory(11).Amount = 1
PlayerInventory(12).ItemName = "Sword"
PlayerInventory(12).Amount = 1
PlayerInventory(13).ItemName = "pistol"
PlayerInventory(13).Amount = 1
PlayerInventory(14).ItemName = "Small Shield"
PlayerInventory(14).Amount = 1
PlayerInventory(15).ItemName = "Tent"
PlayerInventory(15).Amount = 3
PlayerInventory(16).ItemName = "Apple"
PlayerInventory(16).Amount = 7
DO
If Keystate(19)
FOR a = 0 to array count(PlayerInventory() ) - 1
If PlayerInventory(a).ItemName = "Ammo"
`SOME STUFF HAPPENS IN HERE. WHAT THAT IS, ISNT IMPORTANT
`WHATS IMPORTANT IS THAT I DO THIS STUFF, THEN WANT TO EXIT THE LOOP WITHOUT
`GOING THROUGH THE REST OF THE ITEMS IN THE ARRAY
Goto _JumpPoint
Endif
NEXT
_JumpPoint:
endif
Sync
LOOP
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.