PickedInstancesCount() Not Calculating 0

How do I…

I am trying to use the PickedInstancesCount() function to calculate the number of resources that have been gathered by the player.

What is the expected result

Currently, the count of resources is conditioned on a Boolean variable ‘Gathered’ which is attached the Resource object. The Boolean variable is triggered when a collision is detected between the Block object and Resource object. When detected, the object instance has the ‘Gathered’ variable set to True. With no collision this is set to false. In a subsequent line, the total number of gathered objects is picked and a global variable ‘Resource-count’ is updated with the total number. Theoretically this should count all resource objects in the collection area.

What is the actual result

Everything is working flawlessly…! Except for some reason the PickedInstancesCount() will not calculate if the number of objects picked is 0 after the first object as been counted. It works exactly as intended until all resources are removed. I have a count in debug text fields.

Game start - No objects have been counted:

Number of objects in the bin is 3:

All objects have been removed:

Any thoughts on what I am doing wrong? I am certainly open to achieving the same result with a different approach. Thank you!

How are you removing the resources? If that is the part that’s not working then post a screenshot of those events.

Thanks Keith. The resources are not being removed from the scene. I’m trying to create an inventory system in which resources are still physically represented in the world. The removal of the resources is in variable only. IE - If no blocks are in collision with a resource, then all ‘Gathered’ variables are false. This is then counted with the condition below. I think I might have figured out a work around that I’ll post in just a moment. Although it is super odd. Almost feels like a Gdevelop bug in the 'PickedInstancesCount().

I put together a workaround to the PickedInstancesCount(). Here’s what the Events look like now:

The initial events are much the same. If a resource is in collision, mark the Resources instance ‘Gathered’ variable to True. If a resource is NOT in collision, mark the Resources instance variable ‘Gathered’ to false.

An event then picks all of the Resources instances with the ‘Gathered’ Boolean variable as True, and changes the global variable of Resources_Count to all of the picked resources.

Here’s where things get a little hacky. In the following event I do the exact opposite meaning that all of the False values are counted as well. In the gif below you will see the value of Resources_Count and Resources_Uncounted toggle back and forth… but never hit 0.

Screen-Recording-2023-09-23-at-2 (1)

Adding one more event gives the intended functionality. The final line looks at the Resources_Uncounted global variable and compares it SceneInstancesCount(Resources). If these values are the same it updates the Resource_Count global variable to 0.

I would think seeing as Resources_Uncounted is counting all resources that are True, which would be none, these last few lines would not be needed.

A little odd, but I think this works for now.

1 Like

I would have to play around with it but I like what you have. I think the issue with the inverted collision is that if there are zero resources then there’s nothing to collision test. A non inverted collision condition inside of a [not] condition might work. I’m not positive, I’d like to test it but usually it’s more reliable. It inverts the result instead of the condition. Again, I’m not sure in your situation.

1 Like