Hi I have a question. I have an inventory script using a list (I’ve already received tons of help with it on here - thanks guys!) and I have a script here that causes the player to cycle through the inventory and select an item. This should ideally spawn the item and destroy the old item. The objects aren’t ending and he ends up having all the items in his hand at once. Here’s the code, any ideas?
And sorry I’ve been posting so many questions, and probably will post many more, I’m just really excited about this game and I’m actually making some pretty good progress.
# † ><>
# Select items
import bge.logic
def main():
scene = bge.logic.getCurrentScene()
cont = bge.logic.getCurrentController()
player = cont.owner
indexUp = cont.sensors['indexUp']
indexDown = cont.sensors['indexDown']
invSpace = len(player['inventory'])
# This is at the top so that the rest of the code will work. This is the problem, isn't it...
selectedItem = scene.addObject(player['inventory'][player['invIndex']],player,0)
selectedItem.setParent(player, 0, 1)
if indexUp.positive and player['invIndex'] < invSpace - 1:
selectedItem.endObject()
player['invIndex'] += 1
elif indexUp.positive and player['invIndex'] == invSpace - 1:
selectedItem.endObject()
player['invIndex'] = 0
if indexDown.positive and player['invIndex'] > 0:
selectedItem.endObject()
player['invIndex'] -= 1
elif indexDown.positive and player['invIndex'] == 0:
selectedItem.endObject()
player['invIndex'] = invSpace - 1
# inventory is already a list.
player['selectedItem'] = player['inventory'][player['invIndex']]
main()
Thanks. But does this return the object I need to end or does it remove it from the inventory list? Cuz I don’t want to remove it cuz the player still has it he’s just not holding it.
Ok so I tried that it didn’t seem to work sorry. I have a feeling it has something to do with those two lines I pointed out in my code but i don’t know what or how to get around that
I’m not completely understanding your problem or your question. It sounds like essentially:
You have a script that handles your inventory. You press a button to bring up the inventory, and the keys (the indexUp and indexDown sensors) handle cycling through the inventory. When you select an item, it spawns the inventory object corresponding to your choice. Your problem is that when the player is holding an object already and selects a new item, it doesn’t destroy the old object.
If this is the problem, then assuming you stored the currently held object somewhere, you should be able to just use endObject() to end that object in-game (i.e. obj[‘currently_held’].endObject()). Of course, you should do something with that reference and not leave it there (i.e. “obj[‘currently_held’] = None” or something).
EDIT: For a bit more clarification, how do you know what the player’s holding? The inventory just is a list of what he has, not what he has equipped or is holding, right?
Ok well if you guys see the code that’s essentially exactly what I have. selectedItem is a scene object its in another layer and it spawns it to the player when he has it selected. I later have selectedItem.endObject() to end it after he swaps items. Ièll try to explain it in more detail.
Ok so so there’s a seperate script to allow him to pick up an object and append the property itemName of that object to his inventory list. Once he has picked up an item or several items, he can scroll through his inventory. selectedItem is the item he currently selected and it is a game object. The scene knows how to add it because when he picked the item up it appened the itemName property from the item sensor object to pick it up, and this property has the exact same name of the item that it’s supposed to spawn. So this is how I create the item basically. Now if I want him to switch items I use selectedItem.endObject and then the script loops and adds the new one since at the same time invIndex went up one, selecting the next itemName. I hope this will clear up my script, sorry for confusion. Again BPR thank you for helping me set this up And SolarLune you’re understanding my problem exactly, that’s exactly what’s going on. I will try setting changing selectedItem to equal “none” and I’ll get back to you guys to see if it works, but I think that at the top of the script it sort of refreshes what it is.
[Edit] Nope sorry doing that didn’t change anything…
you need to store selected item, as when you recall the script it will not be saved,
own[‘equipped’]=addedObject
to recall
python won’t generate selected item in the frame you need it so… you have to store it
import bge.logic
def main():
scene = bge.logic.getCurrentScene()
cont = bge.logic.getCurrentController()
player = cont.owner
indexUp = cont.sensors['indexUp']
indexDown = cont.sensors['indexDown']
invSpace = len(player['inventory'])
# This is at the top so that the rest of the code will work. This is the problem, isn't it...
selectedItem = scene.addObject(player['inventory'][player['invIndex']],player,0)
selectedItem.setParent(player, 0, 1)
if indexUp.positive and player['invIndex'] < invSpace - 1:
selectedItem.endObject()
player['invIndex'] += 1
elif indexUp.positive and player['invIndex'] == invSpace - 1:
selectedItem.endObject()
player['invIndex'] = 0
if indexDown.positive and player['invIndex'] > 0:
selectedItem.endObject()
player['invIndex'] -= 1
elif indexDown.positive and player['invIndex'] == 0:
selectedItem.endObject()
player['invIndex'] = invSpace - 1
# inventory is already a list.
player['selectedItem'] = player['inventory'][player['invIndex']]
main()
with this logic I believe you are adding the same object over and over each frame,
Ok I have a new idea. Each time the player cycles, I’ll check for items parented to the player inventory thing, if so, return it and store it as previousItem or something, end it, increment invIndex, and add the new object at the bottom.