how to make a store/inventory?

I’ve tried making a store within a game but it isn’t working. I have next to no knowledge of python and therefore can’t get it working properly, is there any good tutorials?

which include saving the current number of coins and adding to total amount? then being able to restart the game without deleting saved values?

So your current solution doesn’t involve any python? You could still post the .blend and people could look at the logic setup and help you on it if you explain what exactly isn’t working.

You will most likely want to use python for inventory and store system. There’s a video tutorial out there with gameobject parenting but it won’t be viable for when you want to have inventory in an overlay scene, change levels, restart game etc where you can’t carry over gameobject references.

which include saving the current number of coins and adding to total amount? then being able to restart the game without deleting saved values?

You will probably want at least globalDict for storing your inventory and coins. I’m not sure what you mean by restarting game but if you want anything beyond what globalDict is able to do it’s going to have to be external text file you read and write at the start and end of the session.

Let’s say you have your actor/player (could be your camera object), in which you add a game property (Game Logic view).

From there, you can either choose to proceed with python or logic bricks. A simple logic brick setup could for example make use of the near type sensor => [AND] => Property type actuator with (for example) addition mode, your named property (could be money) and the value you wish to increase with. - Of course, the logic brick approach leaves a lot to be desired and is rather limited.

The python way gives much more freedom to do things. You could start connecting your always type sensor to a python controller which again is linked to a script.

The basic python setup could be:


from bge import logic as G

controller = G.getCurrentController()
owner = controller.owner

owner["Money"] = 100 #Money being the name you game your game property.

#Lets say we want to make let you have some money, a simple function could be:
def GiveMoney(amount):
    owner["Money"] += amount #Here we simply add the amount to the current amount of cash
    print("You now have: " + owner["Money"] + "bucks") #Here we want to say how much we've got now

Only concept, doesn’t work. - , but it’s the same basic approach. You could also access it from other objects, if you have the current scene declared to a variable and access the property through there, or as Kheetor mentions, make use of globalDict. As for the actual purchase of items in the store, you could probably create your own ID based selection of stuff to buy/sell. - In short, there’s a lot of approaches.

As for restarting the game (I presume you mean to completely close the gameplay instance), I think you’ll need to make use of external text files (as Kheetor mentions). Take a look at the at this: http://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files

Hope it helps :slight_smile:

As a comment to Naxela’s script: remember that if you simply write owner[“Money”] = 100 that is called every frame you can’t really change the amount. Below a globalDict example:


import bge

#this only happens if there is no notion of "coins" in globalDict so this happens only once
if not "coins" in bge.logic.globalDict:
   bge.logic.globalDict["coins"] = 100
   bge.logic.globalDict["inventory"] = [] #an empty list you can add items to by checking length for if there is space and appending

Writing to globalDict is easy because you don’t need to find other references besides bge.logic which you will most likely use in any script anyway. Storing it is also handy via bge.logic.saveGlobalDict() and bge.logic.loadGlobalDict().

For buying/selling you might want a separate script file that defines buying and selling that checks for inventory space and amount of coins or something else before making the changes. Item price and needed inventory space should probably be properties of the item that the script reads for item in question while checking the requirements.

Yes, it was rather clumsy of me, as it would of course as you say overwrite the property value with 100. To have it work it would require the 100 to be in another (one-time) script (an initialization script for example), or simply let the property have the default 0 integer value.
In any case, it was more of a concept rather than a practical approach.

I generally concur to the idea of handling as much of these kind of actions inside python as possible, and as such as you say, the globalDict approach is vastly more suitable and flexible. - The only downside is that (AFAIK) globalDict isn’t manipulable through logic bricks (which some less experienced python coders probably would find to be a nice approach).

But you could have a simple python bridge there as sort of coin pouch if you want to handle the rest through logic.


#Coinpouch module you use with Coinpouch.getCoins or Coinpouch.setCoins after adjusting the owner "coins" property.
#theoretical
import bge

def getCoins():
   bge.logic.getCurrentController().owner["coins"] = bge.logic.globalDict["coins"]

def setCoins():
    bge.logic.globalDict["coins"] = bge.logic.getCurrentController().owner["coins"]

But again this would be really cumbersome as is any logic solution compared to python. I strongly recommend python solution if you want to make a bigger game with multiple levels, save and load, overlay scene inventory etc… Making the logic behave correct in that environment is a lot of work.