I am trying to create an inventory system in upbge with the little amount of python i know. But cant quite figure out how I wud go about doing this.
since its a tps game I need to be able to collect items(eg: guns), store them in my inventory and be able to equip them as well as dropping the item if necessary.
How extensive are you wanting the inventory system? I know it is possible using just logic if you do not want RPG level of inventory (That is still probably possible, but it would be a huge spider web of logic bricks). The only problem with logic bricks is usually when I make something like that it is fairly unstable if someone is button mashing (like in my FPS game if you switch to the sniper rifle too quickly it can cause it to break).
If you are looking for something similar to Jedi Academy where you get items and click 1-9 to switch between those items I might be able to throw a prototype together with logic bricks (though you may want to wait for someone with python knowledge to come help you out)
I am super excited to see your game coming to life! Good luck and have lots of fun
Fredstash
p.s. I just remembered that I had a small example of this I was working on for a game (This game has left development for now, but If you want to take a look at the logic feel free )
edit: I just realized you said UPBGE- this game is in BGE 2.79 but the principles should move over to UPBGE too.
The controls are WASD, Space, Mouselook, Left mouse button shoot, right mouse button scope (on sniper rifle) 1 for machine gun, 2 for sniper rifle- this game is very much not ready for players… but it works as long as you don’t button mash a ton
with little knowledge that’s not an easy task, but doable if you got the mindset.
very simple:
put objects in a list, count the items in the list, now you have the item and the amount.
this can be used to display the inventory, for loop trough the list and add planes as ‘slots’, then give the slots some action on what to do with the item, then add ingame and remove from list.
or little harder but the right way is using the global dict, this also enables the option to save it to your Hdd/Ssd/Nvme.
i got an GD inventory in the resource section: but this has no equip option only a pickup and drop option. Works in 0.2.4 or in blender itself
The best way is to create a custom class tailor made specifically for your particular game, but I’m guessing that’s not what you want.
The easiest way is to create a hard coded dictionary containing all inventory objects and some info about them.
This code won’t actually work, but it might give you an idea.
# Make an empty dict
inv = ["active":none]
# Make dicts for the items
pistol = [ "name":"Pistol", "reveled":false,"amo":0, "meshName":"pistoMesh" ]
rifel = [ "name":"Rifel", "reveled":false,"amo":0, "meshName":"rifelMesh" ]
# Put the items in the inventory
inv[ "pistol": pistol, "rifel":rifel]
# Make a function that can be called by bricks or wtvr
def equip(itemName):
# Get the dictionary for the item from the inventory
item = inv[itemName]
# Get the name of the mesh from the item
meshName = item["meshName"]
# Replace the gun mesh so it looks like the gun in question. This probably isn't the right command
own.children('gunMesh').changeMesh(meshName)
# Set the active gun to the one equipped
inv["active":itemName]
# Run it
equip("pistol")
Logic nodes provides lot of things, and high level character and cars logic nodes, perhaps they could provide inventory easy setup and running in some minutes.