Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Creating A Inventory System

Author
Message
Greg_C
14
Years of Service
User Offline
Joined: 22nd May 2010
Location:
Posted: 27th Jul 2010 00:05 Edited at: 27th Jul 2010 00:09
Okay so I have this basic layout of an inventory but I need soem help on making it more dynamic.(See the picture attached)



Now in this each slot is the Grey boxes and there are mouse over states for them and click states but here are the classes that I used:


Inventory Class:



Slot Class:



Treasure Class:



Main:



Now the main problem I am running into is how do I know what game object is in each slot? I want to be able to select a slot and then see a description of the object in that slot. Does any one have a good way of doing this? I am a little stumped on how to create a dynamic inventory system.
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 27th Jul 2010 11:21
no need to read your classes, you can very simply adjust your structure of the system, the easiest and best way to do this is the following:

bag (inventory) class contains
-number of slots
-array of slots (( INVENTORYITEM items[10][10]; ))

INVENTORYITEM is a structure that can look like the following
struct INVENTORYITEM
{
bool bFilled; //the slot is filled?
int imageID; //image id of the item
int spriteID; //sprite id of the item
bool (*USE) ( void ); //the item's trigger function
};

So i guess everything's clear except the USE function, you simply create a function for each item in your game, let's say it's a health potion, it should look like this:


i designed it so that it returns true if the item should be used, otherwise return false (the item's use condition is false), you can modify this to fit your needs

hope that helps a bit

Greg_C
14
Years of Service
User Offline
Joined: 22nd May 2010
Location:
Posted: 28th Jul 2010 00:57 Edited at: 28th Jul 2010 04:52
Ahh okay that makes since I am going to have to try it out and for the Use function I am not to worried about it because I just have to do the inventory system and thats it. No game play is really involved but thanks for the post and I will post back with what I got in the next couple of days.
Greg_C
14
Years of Service
User Offline
Joined: 22nd May 2010
Location:
Posted: 29th Jul 2010 18:42
Hey I got a question on adding items to the inventory. So I went a head and did what you suggested but I am having trouble adding items to the inventory I guess dynamically. What I mean is I derived from the Slot class to make new items with new images and descriptions and what not. Now I want to be able to add items by calling the AddItem function which when I first created it looks like his:



so basically it loops through my items in the inventory and checks if they are filled, if they aren't then it adds an item to the inventory. Few problems with this first off I want the slots to be what ever Item I create. Meaning when I derive a class from Slot called Chain I want to be able to put that new chain item in the parameters of the AddItem and then insert it into the slot that isn't filled. How would I do this? Or does anyone have a better way of adding items to the inventory?
Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 29th Jul 2010 22:11 Edited at: 29th Jul 2010 22:13
I don't completely understand your code but here is how I would approach the problem.

I would have a Slot class, maybe each slot object would have a SpriteID, posx, posY etc. It would also hold a pointer to an Item object(Item should not be derived from Slot imo), when you draw your slot object it uses the image from the Item object it has a pointer to, if it does not point to an Item object yet you could have a default image it could use.

Here are some class functions Slot may contain:


I would then perhaps have a Slot_Group class, an object of this class may hold a number of slots(array or vector etc) and position them on the screen and perhaps update them all with one call.

I don't think this is the kind of answer you were looking for but it may be some of your class design choices are now making things a bit harder. I have not put a great deal of though into this so feel free to tell me if there is a glaring issue I may have missed.

Greg_C
14
Years of Service
User Offline
Joined: 22nd May 2010
Location:
Posted: 29th Jul 2010 22:48
Yeah that's what I started to do up in my first post. I was getting stuck on a few things which the GetItem() function I think will help me in. I am going to try it today and see if I can go back to what I original wanted to do because I like the idea of actually using game objects rather than trying to just fake it with slots. I will post back if I run into any big issues, and maybe you can help me along the way.
Greg_C
14
Years of Service
User Offline
Joined: 22nd May 2010
Location:
Posted: 29th Jul 2010 23:57
Hey for the SetItem() function are you saying to do something like this:



then in GetItem() I just return SlotObject Right?
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 30th Jul 2010 00:20 Edited at: 30th Jul 2010 00:25
in the additem method, why are you passing the slot place for the item? i believe the inventory system should decide where the item's position should be

anyway, in conclusion, you should have the following:

Item structure : contains :
-image ID of the item
-a pointer to a function that executes the item
-possibly an ID (if you want it to be polished)
****no sprite ID should be used, since you may have more than one slot containing that item, and they cant have the same sprite ID, alternatively let the inventory system pick a suitable sprite ID

Slot structure : contains :
-a pointer to the item
-quantity of the item ****a slot may have a stack of items (multiple items of the same type in one slot)
-position in the inventory (x/y)
****to find out whether its filled or not use this ( if ( !items.size ( ) ) { not filled ( 0 items ) } )

InventorySystem class : contains :
-a vector (or an array) of Slots
-number of rows/cols

i believe those should cover all the base system, you can add additional stuff to fill your needs

sorry if that just repeats what's posted above and hope it helped

Greg_C
14
Years of Service
User Offline
Joined: 22nd May 2010
Location:
Posted: 30th Jul 2010 00:44
What are the differences in making them a Struct or class? Or is it really just a preference? I mean I know structs are basically a class but everything is public. Is this true or is there more to it?

Thanks for the help so far I am trying to get this to work right now so hopefully I will have something working in the next couple of hours.
Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 30th Jul 2010 00:44
Quote: "Hey for the SetItem() function are you saying to do something like this:
then in GetItem() I just return SlotObject Right? "


Yeh, pretty much, your slot(and inventory) then has access to any item it holds and you can change it dynamically which is where you seemed to be having a problem. If you are quite new to creating this kind of access just be careful that when you delete the item, any slot object which is using it is told. This may mean that the item holds a pointer to the slot it belongs to and informs the slot of its destruction, or you may do it different its up to you.


@Hassan - I was of the understanding that you can't use function pointers inside a class, have you done something like this before(in a class)?

Greg_C
14
Years of Service
User Offline
Joined: 22nd May 2010
Location:
Posted: 30th Jul 2010 01:41 Edited at: 30th Jul 2010 01:42
Hey Matty or Hassan,
So I have a GameObject Pointer in my slot class and I want to call the Update of my GameObject in my slot class but since the GameObject doesn't exist yet I am getting errors. I tried setting an if check to make sure it wouldn't call the update if it is NULL but I keep getting the same error:
C2027: use of undefined type 'GameObject'
So what do I need to do to let my slot class know to wait or to allow me to call these functions until there is something in the slot?
Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 30th Jul 2010 02:03
Have you included the GameObject.h in Slot.

Each slot could hold a bool hasObject, set to true when setObject is called, only update object if hasObject == true.

Greg_C
14
Years of Service
User Offline
Joined: 22nd May 2010
Location:
Posted: 30th Jul 2010 02:06
haha man I can't believe I forgot that. Thanks I think I got it to work now.
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 30th Jul 2010 02:27 Edited at: 30th Jul 2010 02:30
Quote: "What are the differences in making them a Struct or class? Or is it really just a preference? I mean I know structs are basically a class but everything is public. Is this true or is there more to it?"


there is no difference, the only difference is that struct is public by default and class is private by default, but it's more of a habit to me, because i never used a function in a struct ( in C not C++ ), and when C++ came out (classes also), i then figured out that you can have functions in classes (and structs) so i started doing it

Quote: "@Hassan - I was of the understanding that you can't use function pointers inside a class, have you done something like this before(in a class)?"


i'm using this


and its working just fine, so you are either using it in a different way or structs and classes are different at this

Login to post a reply

Server time is: 2024-11-19 20:39:22
Your offset time is: 2024-11-19 20:39:22