How can i get the Balls_in_inventory to stop counting?

I’ve made a custom dictionary for counting the number of balls in different rooms. I cant seem to get the balls_in_inventory counter to stop when the balls in room A get depleted. I can’t set up a max value because the balls in room A increase through a different function when state != 1 and increases when state == 1

if event.is_action_pressed("Left"):
			# Performs Ball calculation in Room A
			if state == 1:
				Balls["A"] -= 5
				Ball_in_inventory += 5
				if Balls["A"] <= 0 or Balls["A"] == 0:
					Balls["A"] = 0
					Ball_in_inventory *= 1

It’s not quite clear to me what you are doing here, but you should probably check the amount of balls in A before adding them to the inventory.

if event.is_action_pressed("Left"):
			# Performs Ball calculation in Room A
			if state == 1:
				var balls_to_add = min(5, Balls["A"])
				Balls["A"] -= balls_to_add
				Ball_in_inventory += balls_to_add

min(5, Balls["A"]) gets the lower value of 5 and Balls["A"]. So, if there are more than 5 it uses 5, if there are less it will only use the amount of remaining balls.

1 Like

What @hyvernox said, you are just indiscriminately adding 5 when the action is pressed, you need to check how many are actually there.

A couple of notes on your code:

the second condition here is redundant, you checked if it was zero in the first one.

Is this meant to do something? Because it isn’t.

3 Likes

I kept thinking that it would continuously multiply the amount by 1, overriding the previous increment. I ended up doing this in the meantime to stop it from having increments indefinitely:

	if event.is_action_pressed("Left"):
		# Performs Ball calculation in Room A
		if state == 1 and Balls["A"] > 0:
			Balls["A"] -= 5
			Ball_in_inventory += 5
		elif state == 1 and Balls["A"] <= 0: 
			Balls["A"] = 0

@hyvernox solution makes more sense to me now. Thank you for helping me out :3