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"]
break
They still work if I do it like this:
if slot["slot"] == itemID:
slot["amount"] = itemAmount
break
, 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?
It does the same this way… 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"]
break
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:
#get items in scene
for object in scene.objects:
if "item" in object:
itemList.append(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"])
obj.endObject()
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
@cotax, your method would work, but when I collect item, it adds to all the slots. Anyway, thanks for hints and help! @youleThanks, that’s all I needed, now it is fixed and works very well:)