Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Game inventory

+4 votes
asked Jun 23, 2020 by Litterbox42 (230 points)
Can someone show me what code to write if I want to be able to have a character inventory?

2 Answers

+1 vote
answered Jun 24, 2020 by Peter Minarik (88,520 points)
edited Jun 25, 2020 by Peter Minarik

I will not write the code for you...

... as it should be your own pleasure creating something that works. It's really a great feeling. Don't rob yourself of it.

If you have questions, share your existing code, ask specific questions and people are more than happy to point out errors or improvement opportunities.

I will help you design your solution

Note: I have minimal Python-development experience (I mostly work with C#, C/C++), but object oriented programming and design principles apply all the same.

So here we go.

What do you need?

So you will need a few objects:

  • container: it can hold items. An inventory is such an object. But when you loot someone/something else that can have items, it should be a container (or a derived object) too.
  • item: something that can be placed in a container

Also you need to have a few functions to operate with

  • create: create a new item and place it into a container. These should be called only during the initialization time.
  • move: move one item from one container to another container. You shouldn't destroy and crate new items when they change "owner".
  • destroy: destroys an item, removes it from the container (for good). This can be useful for instance when you consume something (eat, or craft, etc).

Let's get started

So I think this should give you the basic idea of (one way, as there are surely many, of) creating an inventory system.

You can look up online resources, just like this one that explains how classes work in Python: https://www.w3schools.com/python/python_classes.asp

Dig in, try to create it and share your solution if you'd like.

You can spice it up and add properties to items (e.g. quality or weight) and the container (e.g. number of items inside or total weight), but I think first of all item placements and transfers should be your highest priority for now.

Good luck. :)

0 votes
answered Oct 6, 2020 by G4BR13l R0S4 (440 points)
make one variable for iten,if the variable are true,you have the iten but if the variable are false you donĀ“t have the iten
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.