Python adding too much to property

In this image[crap, it add’s it as attachment again - what the f**k does it mean?] you can see my inventory setup. Everything worked fine till I added theese lines of code:

if slot["slot"] == itemID:
            slot["amount"] = itemAmount + slot["amount"]

They still work if I do it like this:

if slot["slot"] == itemID:
            slot["amount"] = itemAmount

, but if I add itemAmount to slot[“amount”] instead of setting it, the python adds more than 1000 to slot[“amount” when I am collecting item with, for example, amount of 3. Why is that and how to fix that?

inventory pick up item?

1 are you ending the item you are picking up?

2 . post a .blend there is not enough code there to show me your problem
what is item amount?

I am ending the item.

if slot["slot"] == itemID:            slot["amount"] = itemAmount + slot["amount"]             break

and if you do:

if slot["slot"] == itemID:            count = itemAmount + slot["amount"] 
            slot["amount"] = count


It does the same this way…:frowning: I will post simplified .blend when I’m not bussy(now I will have to go away from home fora long time, mostly bussy this day).

if slot["slot"] == itemID:
    slot["amount"] = itemAmount + slot["amount"]             

It’s difficult to answer your question without a simplified .blend example, but “While” slot[“slot”] == itemID: the following code will be executed each frame. So if your python controller is linked to an always sensor in True pulse, at each frame you will add item amount to slot[“amount”] . “Break” changes nothing (It’s used inside a loop to stop it). But it doesn’t prevent the code to be executed at the next frame.
You should add another condition to if slot[“slot”] == itemID: (for example “and click.status == 1”) or change itemAmount after:

slot["amount"] = own["itemAmount"] + slot["amount"] 
own["itemAmount"] = 0

In this case itemAmount must be a gloobal variable (own[“itemAmount”] or bge.logic.itemAmount or…)

Here is simplified .blend:
I tried storing item amount in globalDict, but it didn’t help…:frowning:

Adjusted it a bit, think you should find your way around with this.

What did i do,

  • made new lists, a for loop in a for loop with 2 sensors in it is not the best.
  • made checks if list even exist
  • cleaned up your code
  • made the slot type to string instead of int (int counts double/triple amounts) a problem for you to find out.
  • changed the script a bit, now it will set slot use false aswell.

lots of things to do for you, it works but thats is.
Problem1.blend (1.68 MB)

Why use:

import bge
from bge import logic

you can just use one of the two, in your case:

from bge import logic

And remember you need more checks in order to count objects.

#get items in scene
for object in scene.objects:
    if "item" in object:

inventory.items is linked to an always sensor in True pulse. So inventory.items function is executed each frame. So if you have one object with “item” property in the scene, this object will be appended to the itemList at each frame. What you can do is to move this piece of code at the beginning of the script just after itemList = []

Anyway, I think you don’t need itemList and a for loop (if you make a loop for item in itemList, the following code will be executed len(itemList) times) in your inventory.items function. So you can replace items function with this:

#execute collect items

def items():

    col = sens["collide"]
    space = sens["space"]  
    if col.positive:

        obj = col.hitObject

        if space.status == 1:          
            collectItem(obj["item"], obj["amount"])

Another thing is that when you use space.positive, in certain cicumstances, the following can be executed several times. So you can use status instead: if space.status == 1 (just activated)
(2 = active; 3 = just released)

@cotax: Personnaly, I always import bge, because you can access bge.render, bge.texture etc…
It doesn’t prevent you to import logic as g (or anything else) after… But everyone has its own practices :slight_smile:

@cotax, your method would work, but when I collect item, it adds to all the slots. Anyway, thanks for hints and help!:wink:
@youleThanks, that’s all I needed, now it is fixed and works very well:)