How do I check if an array is full?

Get help using Construct 2

Post » Sun Mar 05, 2017 9:45 am

Hi, I have a pretty simple question (at least I hope it's simple).

Image

I'd like this event to only occur if the inventory (Array_Inventory) is not full. The problem is that I noticed there's no action to check if an array has a value in every slot. How would I go about doing such a thing?

EDIT: I've figured out how to check if the inventory is full using a variable and the "IndexOf" expression, but now the problem is that you can't collect multiples of the same block if there's a block in every slot. It's so close, but I have no idea how to fix that!

https://www.dropbox.com/s/d6nuv9fzqyd61 ... .capx?dl=0

Here is the cap file, line 42 is the one that deals with my question.
B
18
S
9
Posts: 103
Reputation: 1,817

Post » Mon Mar 06, 2017 12:49 am

Personally I would not use the array size as the inventory size, Instead I would add a variable to the array object and call it "ItemLimit" or similar, next I would set the default inventory array width to 0.

Every time an item is added or removed I would push or pop an array X element. That way you would know for sure that every X element is an item, so if array.width would equal "ItemLimit" you would know it is full. And if it is full, you would prevent any item add events from firing.
B
15
S
6
Posts: 85
Reputation: 1,195

Post » Mon Mar 06, 2017 5:33 am

Would that solve the problem, though? It seems like that would leave me with the same issue but done via a different method. How would I make it so that happens unless the object being picked up is of a type the player already has?

Image

I managed to do this with the system I currently have, but I don't know how to make it check if the object being picked up has the same 'type' variable as any in the inventory (line 45), and then just not do anything if that's not the case.
B
18
S
9
Posts: 103
Reputation: 1,817

Post » Mon Mar 06, 2017 6:42 pm

Ah ok, I did not notice the edit, sorry. You are using array.indexof(0) to find the empty right? Also a valid way to do it, never thought of it. Anyway I have some suggestions as to the solution. I will give you two solutions actually, the first one is applicable in your case since you store type at index y=0. The second is more general if you need to check things at other y indexes.

Specific for type stored at y=0:

Remember that snazzy indexof you are using? Use array.indexof(block.type). It will return an index of 0 or greater if the block of that type is already in the array. It will return -1 if the block type has not yet been added to the inventory at all. And if the block type is in the array, you can use the same expression to tell the function at which index to add the new block amount.

General:

What I do is the following. Create a local variable "IsInInventory" or something similar in the event. Default value 0. Create another for the type of item you add if needed.

Event conditions
Loop for every X element of the inventory.
Inventory value at (Self.CurX,0) = type

Event Actions
set value "IsInInventory" to 1

Next you just make an event with condition IsInInventory=1 and do whatever you need to do.

BTW I like your project, pretty cool.
B
15
S
6
Posts: 85
Reputation: 1,195

Post » Mon Mar 06, 2017 9:49 pm

If you intend to keep the array at the current size. And, if full means that every slot has at least 1 item. Then the array is not full when ...

arr_Inventory.at(5,1) is equal to zero

I suppose you clear 'items with a zero amount' from the array.
Else. If a zero amount of an item is allowed, you better go for a different 'nothing is there' annotation.
I would go for the empty string "". I would fill the whole array on start up with "". And if i make an empty slot later, i would fill that index with "". This way 'zero' does not mean empty, but an empty amount. Now the array is not full when ....

arr_Inventory.at(5,1) is equal to ""
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Mon Mar 06, 2017 9:55 pm

Pulstar wrote:Ah ok, I did not notice the edit, sorry. You are using array.indexof(0) to find the empty right? Also a valid way to do it, never thought of it. Anyway I have some suggestions as to the solution. I will give you two solutions actually, the first one is applicable in your case since you store type at index y=0. The second is more general if you need to check things at other y indexes.


Thank you for the continued help, I appreciate it. That said, I think I still have no idea what I'm doing. I definitely get what you're saying, but I've got no clue how to put it into practice. Your explanation is fine, I guess the problem is I'm having trouble wrapping my head around putting it into Construct. Maybe running through it step by step would help?

https://www.dropbox.com/s/d6nuv9fzqyd61 ... .capx?dl=0

Here's the updated capx file in case you want to just show me what the finished product would look like (a la your favorite daytime TV cooking show) instead because that might be easier. I wouldn't blame you :P.
B
18
S
9
Posts: 103
Reputation: 1,817

Post » Tue Mar 07, 2017 7:40 pm

@CjacobsSA

Try the below demo I just made. Everything should be commented (besides the very obvious on flash stop make warning text invisible event).

Basically this is an inventory system working kind of like in minecraft where you have a max stack size per slot. Although unlike in minecraft here the stack size is the same for all objects, set by the global variable MaxStackSize. In minecraft tools, buckets etc. have a stack size of 1 vs 64 for most if not all blocks IIRC.

I ended up using a while loop as it is the most flexible and efficient IMO for doing such searches through an array. Note that the demo is fairly rudimentary, and it will not for instance, prioritize putting a block into a not yet full slot with the same type over putting it in an empty slot (if one has a lower index). As there is no way in the demo to remove an item from a slot, this case will never happen.

https://www.dropbox.com/s/3k9v2fdlqhn3b ... .capx?dl=1
B
15
S
6
Posts: 85
Reputation: 1,195

Post » Tue Mar 07, 2017 8:07 pm

Wow, that's awesome! You really went above and beyond. Thanks, this is great!
B
18
S
9
Posts: 103
Reputation: 1,817


Return to How do I....?

Who is online

Users browsing this forum: biasaur, Exabot [Bot], plinkie, Unconnected and 10 guests