problem with array for each loop

Get help using Construct 2

Post » Tue Jun 23, 2015 1:32 am

Hey there evryone.
Im having a little problem with my code here.
This is what i have:

Image

I have a simple 6 slot inventory system going on. This piece of code checks if the inventory is not full (has at least 1 slot free) Basically if any value in X is 0, it means that slot is empty. The thing is, that it works in a weird way.

I add items to the inventory until all 6 slots are used. The code runs and sets "inv_full" to true. Everything great over there. The problem is that if I delete any slot, other than the last one, It doesnt set the "inv_full" back to false. As soon as I delete the last slot (last X value in the array) than it sets "inv_full" to false.

Image

Ideas???
Image
B
22
S
5
G
2
Posts: 65
Reputation: 1,724

Post » Tue Jun 23, 2015 1:45 am

You are always checking every slot, ending with the last one, so it will only ever set whatever the last slot's state is. You need to at least stop the loop if the =0 condition is met. (Start with true though).
ImageImageImage
B
71
S
22
G
248
Posts: 3,759
Reputation: 138,069

Post » Tue Jun 23, 2015 1:53 am

And how could I stop the loop?
Image
B
22
S
5
G
2
Posts: 65
Reputation: 1,724

Post » Tue Jun 23, 2015 2:02 am

Doh, the Array object doesn't support Stop directly (there is a System.Stop loop action), but an even simpler solution is to the Array.IndexOf(0). Let Array do the work.
https://www.scirra.com/manual/108/array
ImageImageImage
B
71
S
22
G
248
Posts: 3,759
Reputation: 138,069

Post » Tue Jun 23, 2015 2:18 am

awesome, the indexof expression worked. here is how I fixed it:

Image

thanks for the help blackhornet!
Image
B
22
S
5
G
2
Posts: 65
Reputation: 1,724

Post » Tue Jun 23, 2015 2:21 am

You don't need the for loop for this!
if inventory.IndexOf(0) != -1 -> inv_full = false
Else -> inv_full = true
ImageImageImage
B
71
S
22
G
248
Posts: 3,759
Reputation: 138,069

Post » Tue Jun 23, 2015 2:26 am

oooooh. good to know lol! removed the for loop! thanks!
Image
B
22
S
5
G
2
Posts: 65
Reputation: 1,724


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 24 guests