Jump to content


Photo

Inventory(Bag)


  • Please log in to reply
22 replies to this topic

#1 jagkallasfrags

jagkallasfrags

    GMC Member

  • New Member
  • 63 posts

Posted 27 November 2011 - 01:42 PM

Hello, I want an inventory(bag) when I press Ctrl, I have the obj_inventory, and an obj_dollar that I want to pick up, and get in the inventory(bag), do anyone have any idea what I can do. bye
  • -1

#2 XTIDIA Games

XTIDIA Games

    GMC Member

  • GMC Member
  • 198 posts
  • Version:GM8

Posted 27 November 2011 - 01:56 PM

no idea from me but some offtopic
why do you ppl say bye ??
Its not a chat or anything
Im not saying its bad or anything but why??
  • -2

#3 Noele

Noele

    GMC Mentor

  • GMC Member
  • 2347 posts
  • Version:GM8.1

Posted 27 November 2011 - 02:31 PM

There are several ways to create an inventory system but first you need to decide how many items you want to be able to carry, if you can carry multiples of the same item, what each is used for, if items can be used (combined) with other items and how you get the player to use each.

If you have GM Pro or 8.1 standard you may be able to use an Inventory Extension (See Extending Game Maker forums). In any event you will probably need to understand how to use arrays so that would be a good place to start.

@jagkallasfrags: Saying Hello or bye is not a requirement however politeness costs nothing.
  • 0

#4 loverock125

loverock125

    GMC Member

  • GMC Member
  • 1599 posts
  • Version:GM8

Posted 27 November 2011 - 02:47 PM

This is a very general question because, as Noele stated, there are countless types of inventories and countless ways to program them. The most certain thing is that you will need to understand arrays. Arrays are like a table with values, and since an inventory is a table it is essential to use arrays.

Here is a basic example of an inventory:

Create Event of oInventory:
xslots = 4; //number of horizontal slots
yslots = 8; //number of vertical slots
slotWidth = 32; //width of slot
slotHeight = 32; //height of slot

//initializing a 2D array which holds the object index and the sprite index
var i;
for(i=0;i<xslots*yslots;i+=1) //loop 
{
invSlot[i,0] = -1; //object index
invSlot[i,1] = -1; //sprite index
}

/*
so that makes this table:

invSlot[0,0] = -1;
invSlot[0,1] = -1;
invSlot[1,0] = -1;
invSlot[1,1] = -1;
...and so on until invSlot[xslots*yslots] (to initialize all slots)
*/


And draw event of oInventory:
///Draw Inventory
//first draw a background color for the inventory
draw_set_alpha(0.6); //make it transparent
draw_set_color(make_color_rgb(153,217,234)); //make_color_rgb just lets you make your own color, you
//can find those values in paint
draw_rectangle(x,y,x + xslots * slotWidth,y + yslots * slotHeight,false);

//then draw the grid
var i,slotX,slotY,countX,countY; //initializing temporary variables
draw_set_color(c_white); //white grid
for(i=0;i<xslots*yslots;i+=1)  //loop through all slots
{
//Calculating at what xslot and yslot we are on
countX = i mod xslots; //this is simple math, can't really explain it
countY = i div xslots;

//position of the current slot
slotX = x + countX * slotWidth;
slotY = y + countY * slotHeight;

//draw the slot
draw_rectangle(slotX,slotY,slotX + slotWidth,slotY + slotHeight,true);

    //now if the slot has an item in it
    if invSlot[i,1]!=-1
    {
    //then just draw the item's sprite index over this slot
    draw_sprite(invSlot[i,1],0,slotX,slotY);
    }

    
    //if the mouse is over this slot then just draw smaller rectangle to indicate that
    if mouse_x>slotX and mouse_y>slotY and mouse_x<slotX + slotWidth and mouse_y<slotY + slotHeight
    {
    draw_rectangle(slotX + 2, slotY + 2, slotX + slotWidth - 2, slotY + slotHeight - 2, true);
        
        //now if the mouse is over a slot which has an item in it
        if invSlot[i,1]!=-1 
        {
        //(you can even draw tooltips of the item here)
        
            //and he presses it
            if mouse_check_button_pressed(mb_right)
            {
            //drop the item, oPlayer is the player object 
            instance_create(oPlayer.x,oPlayer.y,invSlot[i,0] ); //creates the item at the player's position
            invSlot[i,0] = -1; //clear slot's object index
            invSlot[i,1] = -1; //clear slot's sprite index
            }
        }
    }

}


Now to fill the inventory with items, you just need to change the values of the array.
If you do something like this:

invSlot[0,0] = oDollar;
invSlot[1,0] = sDollar;

The first slot of the inventory will get filled with the sprite sDollar, and when the user presses it an instance of oDollar will get created at the player's position. You will probably want to loop through all slots again, check if one is empty and then put the item in it. It's basically the same check I did in the draw event, to see if there's an item in a slot.

Now to make it be appear/disappear when pressing the CTRL key, just set the object 'oInventory' visible/invisible.

I'm not very good at explaining things but I did the best I can. Also you could use my example of inventory here, but It's very complicated and I don't really recommend it for beginners.

Good Luck!

Edited by loverock125, 27 November 2011 - 02:54 PM.

  • 0

#5 jagkallasfrags

jagkallasfrags

    GMC Member

  • New Member
  • 63 posts

Posted 27 November 2011 - 03:49 PM

This is a very general question because, as Noele stated, there are countless types of inventories and countless ways to program them. The most certain thing is that you will need to understand arrays. Arrays are like a table with values, and since an inventory is a table it is essential to use arrays.

Here is a basic example of an inventory:

Create Event of oInventory:

xslots = 4; //number of horizontal slots
yslots = 8; //number of vertical slots
slotWidth = 32; //width of slot
slotHeight = 32; //height of slot

//initializing a 2D array which holds the object index and the sprite index
var i;
for(i=0;i<xslots*yslots;i+=1) //loop 
{
invSlot[i,0] = -1; //object index
invSlot[i,1] = -1; //sprite index
}

/*
so that makes this table:

invSlot[0,0] = -1;
invSlot[0,1] = -1;
invSlot[1,0] = -1;
invSlot[1,1] = -1;
...and so on until invSlot[xslots*yslots] (to initialize all slots)
*/


And draw event of oInventory:
///Draw Inventory
//first draw a background color for the inventory
draw_set_alpha(0.6); //make it transparent
draw_set_color(make_color_rgb(153,217,234)); //make_color_rgb just lets you make your own color, you
//can find those values in paint
draw_rectangle(x,y,x + xslots * slotWidth,y + yslots * slotHeight,false);

//then draw the grid
var i,slotX,slotY,countX,countY; //initializing temporary variables
draw_set_color(c_white); //white grid
for(i=0;i<xslots*yslots;i+=1)  //loop through all slots
{
//Calculating at what xslot and yslot we are on
countX = i mod xslots; //this is simple math, can't really explain it
countY = i div xslots;

//position of the current slot
slotX = x + countX * slotWidth;
slotY = y + countY * slotHeight;

//draw the slot
draw_rectangle(slotX,slotY,slotX + slotWidth,slotY + slotHeight,true);

    //now if the slot has an item in it
    if invSlot[i,1]!=-1
    {
    //then just draw the item's sprite index over this slot
    draw_sprite(invSlot[i,1],0,slotX,slotY);
    }

    
    //if the mouse is over this slot then just draw smaller rectangle to indicate that
    if mouse_x>slotX and mouse_y>slotY and mouse_x<slotX + slotWidth and mouse_y<slotY + slotHeight
    {
    draw_rectangle(slotX + 2, slotY + 2, slotX + slotWidth - 2, slotY + slotHeight - 2, true);
        
        //now if the mouse is over a slot which has an item in it
        if invSlot[i,1]!=-1 
        {
        //(you can even draw tooltips of the item here)
        
            //and he presses it
            if mouse_check_button_pressed(mb_right)
            {
            //drop the item, oPlayer is the player object 
            instance_create(oPlayer.x,oPlayer.y,invSlot[i,0] ); //creates the item at the player's position
            invSlot[i,0] = -1; //clear slot's object index
            invSlot[i,1] = -1; //clear slot's sprite index
            }
        }
    }

}


Now to fill the inventory with items, you just need to change the values of the array.
If you do something like this:

invSlot[0,0] = oDollar;
invSlot[1,0] = sDollar;

The first slot of the inventory will get filled with the sprite sDollar, and when the user presses it an instance of oDollar will get created at the player's position. You will probably want to loop through all slots again, check if one is empty and then put the item in it. It's basically the same check I did in the draw event, to see if there's an item in a slot.

Now to make it be appear/disappear when pressing the CTRL key, just set the object 'oInventory' visible/invisible.

I'm not very good at explaining things but I did the best I can. Also you could use my example of inventory here, but It's very complicated and I don't really recommend it for beginners.

Good Luck!



WOW, that was a bit complicated, can u download my game and like fix it or make a video or like a easy example, and 1 more thing, I only have 1 Item right now and that is oDollar, and if I get like 2 or more of them, I want it to say how many it is in the top right corner of the 16x16 item slot were the items is. but I hope that you can fix this for me I will send it to you.
So bye.
  • 0

#6 Noele

Noele

    GMC Mentor

  • GMC Member
  • 2347 posts
  • Version:GM8.1

Posted 27 November 2011 - 04:35 PM

What you are requesting is fairly complex but unless you understand how arrays work, you will struggle to use or adapt any system.

An array is a variable with several elements indexed by a number.
item[0] = "First slot"
item[1] = "Next slot"
item[2] = "Empty slot"
Thus if your bag can contain 10 items, you can create an array with 10 elements (or slots if you prefer.) To add an item to the bag you first loop through to locate an empty slot and simply replace its contents with the item to add.

In practice you will require more than a single array. If you are new to arrays you may find data structures easier to use. These essentially use arrays but contain useful built-in functions.

For example ds_maps from the help file:

In quite a few situations you need to store pairs consisting of a key and a value. For example, a character can have a number of different items and for each item it has a particular number of those. In this case the item is the key and the number is the value. Maps maintain such pairs, sorted by key. You can add pairs to the map and search for the value corresponding to certain keys. Because the keys are sorted you can also find previous and next keys. Sometimes it is also useful to use a map to just store keys without a corresponding value. In that case you can simply use a value of 0.


Collecting items is the easy part; if you want the player to be able to use them (like drop them, spend them or eat them etc) you need some way of selecting them individually. Adding such a system to an existing game would require a lot of re-writing, and a little unfair to expect anyone to do for you.
  • 0

#7 jagkallasfrags

jagkallasfrags

    GMC Member

  • New Member
  • 63 posts

Posted 28 November 2011 - 02:19 PM

This is a very general question because, as Noele stated, there are countless types of inventories and countless ways to program them. The most certain thing is that you will need to understand arrays. Arrays are like a table with values, and since an inventory is a table it is essential to use arrays.

Here is a basic example of an inventory:

Create Event of oInventory:

xslots = 4; //number of horizontal slots
yslots = 8; //number of vertical slots
slotWidth = 32; //width of slot
slotHeight = 32; //height of slot

//initializing a 2D array which holds the object index and the sprite index
var i;
for(i=0;i<xslots*yslots;i+=1) //loop 
{
invSlot[i,0] = -1; //object index
invSlot[i,1] = -1; //sprite index
}

/*
so that makes this table:

invSlot[0,0] = -1;
invSlot[0,1] = -1;
invSlot[1,0] = -1;
invSlot[1,1] = -1;
...and so on until invSlot[xslots*yslots] (to initialize all slots)
*/


And draw event of oInventory:
///Draw Inventory
//first draw a background color for the inventory
draw_set_alpha(0.6); //make it transparent
draw_set_color(make_color_rgb(153,217,234)); //make_color_rgb just lets you make your own color, you
//can find those values in paint
draw_rectangle(x,y,x + xslots * slotWidth,y + yslots * slotHeight,false);

//then draw the grid
var i,slotX,slotY,countX,countY; //initializing temporary variables
draw_set_color(c_white); //white grid
for(i=0;i<xslots*yslots;i+=1)  //loop through all slots
{
//Calculating at what xslot and yslot we are on
countX = i mod xslots; //this is simple math, can't really explain it
countY = i div xslots;

//position of the current slot
slotX = x + countX * slotWidth;
slotY = y + countY * slotHeight;

//draw the slot
draw_rectangle(slotX,slotY,slotX + slotWidth,slotY + slotHeight,true);

    //now if the slot has an item in it
    if invSlot[i,1]!=-1
    {
    //then just draw the item's sprite index over this slot
    draw_sprite(invSlot[i,1],0,slotX,slotY);
    }

    
    //if the mouse is over this slot then just draw smaller rectangle to indicate that
    if mouse_x>slotX and mouse_y>slotY and mouse_x<slotX + slotWidth and mouse_y<slotY + slotHeight
    {
    draw_rectangle(slotX + 2, slotY + 2, slotX + slotWidth - 2, slotY + slotHeight - 2, true);
        
        //now if the mouse is over a slot which has an item in it
        if invSlot[i,1]!=-1 
        {
        //(you can even draw tooltips of the item here)
        
            //and he presses it
            if mouse_check_button_pressed(mb_right)
            {
            //drop the item, oPlayer is the player object 
            instance_create(oPlayer.x,oPlayer.y,invSlot[i,0] ); //creates the item at the player's position
            invSlot[i,0] = -1; //clear slot's object index
            invSlot[i,1] = -1; //clear slot's sprite index
            }
        }
    }

}


Now to fill the inventory with items, you just need to change the values of the array.
If you do something like this:

invSlot[0,0] = oDollar;
invSlot[1,0] = sDollar;

The first slot of the inventory will get filled with the sprite sDollar, and when the user presses it an instance of oDollar will get created at the player's position. You will probably want to loop through all slots again, check if one is empty and then put the item in it. It's basically the same check I did in the draw event, to see if there's an item in a slot.

Now to make it be appear/disappear when pressing the CTRL key, just set the object 'oInventory' visible/invisible.

I'm not very good at explaining things but I did the best I can. Also you could use my example of inventory here, but It's very complicated and I don't really recommend it for beginners.

Good Luck!




I made one like this, 2 problems, when i drop stuff (right click on them) then I get stuck in them, and all the Items that I want to pick up just get like solid so my obj_char cant walk into them.

now the big problem, I walk like on pokemon (the screen follows the obj_char) but the inventory is in the top left corner of the whole game, so how do I get it to follow the screen with all the objects in?
  • 0

#8 jagkallasfrags

jagkallasfrags

    GMC Member

  • New Member
  • 63 posts

Posted 29 November 2011 - 02:47 PM

please answer fast?
  • 0

#9 Noele

Noele

    GMC Mentor

  • GMC Member
  • 2347 posts
  • Version:GM8.1

Posted 29 November 2011 - 03:12 PM

First problem:
Ensure moving instances are not set to solid and do not have a parent object which is also solid. Solids generate two collision events (one for each instance) and move the moving one back out of collision. Non-solids only generate an event for the instance causing the collision and do not place it out of collision, thus allowing each to go over the top. In general it is better only to have static objects (like walls) as solids.

When "dropping" your items ensure they are moved clear of your character and any solid instances, possibly using move_outside_all?

Second problem.
You can use x+view_xview[0] and y+view_yview[0] when displaying things in the inventory. This will position them relative to the view position - so they stay put. Alternatively you can create another view (a static one) and display it there.
  • 0

#10 jagkallasfrags

jagkallasfrags

    GMC Member

  • New Member
  • 63 posts

Posted 29 November 2011 - 04:57 PM

You can use x+view_xview[0] and y+view_yview[0] when displaying things in the inventory. This will position them relative to the view position - so they stay put. Alternatively you can create another view (a static one) and display it there.


Thanks for the first problem, but this? This was a bit harder to understand but i dont want to do like a "static one" becouse it is a bit transparent, so can you explane the x+view and that a bit better, or make a example game?
thanks again and bye.
  • 0

#11 Noele

Noele

    GMC Mentor

  • GMC Member
  • 2347 posts
  • Version:GM8.1

Posted 29 November 2011 - 05:14 PM

Either method works equally well but I will try to explain how to use view_xview[0] and view_yview[0].

These are both system variables (the zero in the square brackets is the view number 0..7). These hold the x,y starting position for that view and they increase / decrease as the view moves.

Thus to draw something which is always in the view you can use these as an offset:
EG:
draw_text(10,10,"This is a test");
Draws "This is a test" in a fixed position in the room.
draw_text(10+view_xview,10+view_yview,"This is a test");
Draws the same text at 10,10 but relative to where the view is.

The view_xview and view_y_view values change proportionately with the view, thus by adding these values to where you want then placed or things you want drawn you are ensuring they are drawn in the same place on the screen no matter where the view is in the room.

This works with sprites, backgrounds, text, any of the drawing functions and even objects.

Does that make sense to you?
  • 0

#12 jagkallasfrags

jagkallasfrags

    GMC Member

  • New Member
  • 63 posts

Posted 29 November 2011 - 05:58 PM

Either method works equally well but I will try to explain how to use view_xview[0] and view_yview[0].

These are both system variables (the zero in the square brackets is the view number 0..7). These hold the x,y starting position for that view and they increase / decrease as the view moves.

Thus to draw something which is always in the view you can use these as an offset:
EG:

draw_text(10,10,"This is a test");
Draws "This is a test" in a fixed position in the room.
draw_text(10+view_xview,10+view_yview,"This is a test");
Draws the same text at 10,10 but relative to where the view is.

The view_xview and view_y_view values change proportionately with the view, thus by adding these values to where you want then placed or things you want drawn you are ensuring they are drawn in the same place on the screen no matter where the view is in the room.

This works with sprites, backgrounds, text, any of the drawing functions and even objects.

Does that make sense to you?

Pretty much no, please make a example game(test game) before today, becouse I need it today please.

Edited by jagkallasfrags, 29 November 2011 - 06:03 PM.

  • 0

#13 loverock125

loverock125

    GMC Member

  • GMC Member
  • 1599 posts
  • Version:GM8

Posted 29 November 2011 - 07:09 PM

Noele explained it pretty good, to make the inventory follow the view, just go to the draw event and replace the x and y with view_xview+x and view_yview+y respectively.

///Draw Inventory
//first draw a background color for the inventory
draw_set_alpha(0.6); //make it transparent
draw_set_color(make_color_rgb(153,217,234)); //make_color_rgb just lets you make your own color, you
//can find those values in paint
draw_rectangle(x+view_xview,y+view_yview,x+view_xview + xslots * slotWidth,y+view_yview + yslots * slotHeight,false);

//then draw the grid
var i,slotX,slotY,countX,countY; //initializing temporary variables
draw_set_color(c_white); //white grid
for(i=0;i<xslots*yslots;i+=1)  //loop through all slots
{
//Calculating at what xslot and yslot we are on
countX = i mod xslots; //this is simple math, can't really explain it
countY = i div xslots;

//position of the current slot
slotX = x + view_xview + countX * slotWidth;
slotY = y + view_yview + countY * slotHeight;


  • 0

#14 jagkallasfrags

jagkallasfrags

    GMC Member

  • New Member
  • 63 posts

Posted 30 November 2011 - 07:25 AM

Noele explained it pretty good, to make the inventory follow the view, just go to the draw event and replace the x and y with view_xview+x and view_yview+y respectively.

///Draw Inventory
//first draw a background color for the inventory
draw_set_alpha(0.6); //make it transparent
draw_set_color(make_color_rgb(153,217,234)); //make_color_rgb just lets you make your own color, you
//can find those values in paint
draw_rectangle(x+view_xview,y+view_yview,x+view_xview + xslots * slotWidth,y+view_yview + yslots * slotHeight,false);

//then draw the grid
var i,slotX,slotY,countX,countY; //initializing temporary variables
draw_set_color(c_white); //white grid
for(i=0;i<xslots*yslots;i+=1)  //loop through all slots
{
//Calculating at what xslot and yslot we are on
countX = i mod xslots; //this is simple math, can't really explain it
countY = i div xslots;

//position of the current slot
slotX = x + view_xview + countX * slotWidth;
slotY = y + view_yview + countY * slotHeight;


thanks that helped.

PROB1: how do I get the inventory to show if I press "E" then when I press it again it should disapear?
PROB2: When obj_char die(gets hp to 0) I want everything in the inventory to disapear, how do I do that?

Hope someone can help.
Bye.

Edited by jagkallasfrags, 30 November 2011 - 08:09 AM.

  • 0

#15 Noele

Noele

    GMC Mentor

  • GMC Member
  • 2347 posts
  • Version:GM8.1

Posted 30 November 2011 - 10:37 AM

The solutions will depend upon your design model. When creating games the overall design is very important, particularly when adding new features at a later date.

The fundamental objective of even a basic inventory system is to be able to add items to it which the player can use at a later time. There are literally hundreds of ways to create an inventory; there are even inventory extensions for GM to help perform the array handling, however the reason a player collects items is so they can use them at a later time thus it is important to design a system which uses similar controls the rest of the game does.

For example, since my own games are mouse controlled and need them to be clickable I create inventory items as instances of the same inventory object, assigning each a different sprite and giving the array index the id of that instance. I also set the inventory object to persistent. This gives the ability to carry items from room to room and allows each item to be selected by clicking upon it. For rooms which I don't want the inventory shown (like an options menu, pause game or min-puzzle rooms) I can easily hide them by deactivating all their instances.

Getting back to your problems...

PROB1: how do I get the inventory to show if I press "E" then when I press it again it should disapear?

If your inventory items are instances of an object you can set their visibility to false or deactivate them. Visibility simply makes them visible or not; it does not affect any events thus although they are not seen, they will still respond to events. Deactivating them also renders them invisible but this time they no longer generate events - they are temporarily removed from the game.

If you have an object which draws your inventory items you can make this instance invisible (visible = false;) or if it draws other things use a variable to show or hide the items when you draw them.
E.G
In its Create Event add:
is_hidden = false;
In its Draw Event add to the start:
///Draw Inventory
if(is_hidden) {exit;} // we are hidden so just exit
// 
//first draw a background color for the inventory
draw_set_alpha(0.6); //make it transparent
draw_set_color(make_color_rgb(153,217,234)); //make_color_rgb just lets you make your own color, you
//can find those values in paint
//etc...
You can then change the variable "is_hidden" to true or false to show or hide the items.

PROB2: When obj_char die(gets hp to 0) I want everything in the inventory to disapear, how do I do that?

The inventory is held in an array so to empty it you must loop through the array, setting the contents of each back to nothing. This sounds harder than it is; you only need to know the number of inventory items the array can hold and the type of data it contains (string or real) and a simple for loop will do the rest.

In fact you probably already have this code where you initialized the array, possibly in its create event, however if you want to reference it from a different object (like a controller or player), where the player dies you can use:
with (obj_your_inv_controler){
  //initializing a 2D array which holds the object index and the sprite index
  var i;
  for(i=0;i<xslots*yslots;i+=1) //loop 
  {
    invSlot[i,0] = -1; //object index
    invSlot[i,1] = -1; //sprite index
  }
}
That will reset/clear all the items.
  • 0

#16 jagkallasfrags

jagkallasfrags

    GMC Member

  • New Member
  • 63 posts

Posted 30 November 2011 - 03:17 PM

The solutions will depend upon your design model. When creating games the overall design is very important, particularly when adding new features at a later date.

The fundamental objective of even a basic inventory system is to be able to add items to it which the player can use at a later time. There are literally hundreds of ways to create an inventory; there are even inventory extensions for GM to help perform the array handling, however the reason a player collects items is so they can use them at a later time thus it is important to design a system which uses similar controls the rest of the game does.

For example, since my own games are mouse controlled and need them to be clickable I create inventory items as instances of the same inventory object, assigning each a different sprite and giving the array index the id of that instance. I also set the inventory object to persistent. This gives the ability to carry items from room to room and allows each item to be selected by clicking upon it. For rooms which I don't want the inventory shown (like an options menu, pause game or min-puzzle rooms) I can easily hide them by deactivating all their instances.

Getting back to your problems...

PROB1: how do I get the inventory to show if I press "E" then when I press it again it should disapear?

If your inventory items are instances of an object you can set their visibility to false or deactivate them. Visibility simply makes them visible or not; it does not affect any events thus although they are not seen, they will still respond to events. Deactivating them also renders them invisible but this time they no longer generate events - they are temporarily removed from the game.

If you have an object which draws your inventory items you can make this instance invisible (visible = false;) or if it draws other things use a variable to show or hide the items when you draw them.
E.G
In its Create Event add:
is_hidden = false;
In its Draw Event add to the start:
///Draw Inventory
if(is_hidden) {exit;} // we are hidden so just exit
// 
//first draw a background color for the inventory
draw_set_alpha(0.6); //make it transparent
draw_set_color(make_color_rgb(153,217,234)); //make_color_rgb just lets you make your own color, you
//can find those values in paint
//etc...
You can then change the variable "is_hidden" to true or false to show or hide the items.

PROB2: When obj_char die(gets hp to 0) I want everything in the inventory to disapear, how do I do that?

The inventory is held in an array so to empty it you must loop through the array, setting the contents of each back to nothing. This sounds harder than it is; you only need to know the number of inventory items the array can hold and the type of data it contains (string or real) and a simple for loop will do the rest.

In fact you probably already have this code where you initialized the array, possibly in its create event, however if you want to reference it from a different object (like a controller or player), where the player dies you can use:
with (obj_your_inv_controler){
  //initializing a 2D array which holds the object index and the sprite index
  var i;
  for(i=0;i<xslots*yslots;i+=1) //loop 
  {
    invSlot[i,0] = -1; //object index
    invSlot[i,1] = -1; //sprite index
  }
}
That will reset/clear all the items.





dont understand, please make a example game or something
  • 0

#17 Noele

Noele

    GMC Mentor

  • GMC Member
  • 2347 posts
  • Version:GM8.1

Posted 30 November 2011 - 03:45 PM

Without knowing how you are accomplishing things it is difficult to create a demo.

The object of collecting items is so the player can use them later. If you have say, a 10 slot inventory system and don't allow the player to remove things from it, once it becomes full, nothing more can be collected.

The way in which you allow the player to use the items they have collected is fundamental to solving your problems.

If your inventory items are simply drawn images, they will have no collision properties of their own. In other words they will not be able to be selected with the mouse. You can assign keyboard events to each item collected however the keys you select should be obvious to the player and unique; if you assign the same key to two items, both will get "used" at the same time.

If your inventory items are instances of an inventory object (that has say an empty box sprite) a drawn image can be superimposed on top (making it appear there is something in the box) and the entire box sprite can respond to mouse clicks.

How does a player collect items - on collision or key press?
How does a player select which item to use?
How you plan to use items collected?
What you need the items to do?
Do they need to be remembered going from room to room?
Are you using any persistent rooms?

These points may be clear to you but we cannot see your game so can only glean knowledge of it from what you tell us. Any demo would have to do what you want and be written in a way you can understand otherwise you may find it difficult to implement in your game.
  • 0

#18 jagkallasfrags

jagkallasfrags

    GMC Member

  • New Member
  • 63 posts

Posted 30 November 2011 - 04:06 PM

Without knowing how you are accomplishing things it is difficult to create a demo.

The object of collecting items is so the player can use them later. If you have say, a 10 slot inventory system and don't allow the player to remove things from it, once it becomes full, nothing more can be collected.

The way in which you allow the player to use the items they have collected is fundamental to solving your problems.

If your inventory items are simply drawn images, they will have no collision properties of their own. In other words they will not be able to be selected with the mouse. You can assign keyboard events to each item collected however the keys you select should be obvious to the player and unique; if you assign the same key to two items, both will get "used" at the same time.

If your inventory items are instances of an inventory object (that has say an empty box sprite) a drawn image can be superimposed on top (making it appear there is something in the box) and the entire box sprite can respond to mouse clicks.

How does a player collect items - on collision or key press?
How does a player select which item to use?
How you plan to use items collected?
What you need the items to do?
Do they need to be remembered going from room to room?
Are you using any persistent rooms?

These points may be clear to you but we cannot see your game so can only glean knowledge of it from what you tell us. Any demo would have to do what you want and be written in a way you can understand otherwise you may find it difficult to implement in your game.



Well, you can try it self.
http://www.2shared.com/file/c2dXVbLB/DEMO3.html
  • 0

#19 Gzack

Gzack

    GMC Member

  • New Member
  • 52 posts
  • Version:GM8

Posted 01 December 2011 - 07:40 PM

I'm having a hard time understanding where to put items in, is the array a global thing, or do I have to manually put in an item?
  • 0

#20 Noele

Noele

    GMC Mentor

  • GMC Member
  • 2347 posts
  • Version:GM8.1

Posted 01 December 2011 - 08:21 PM

Having global arrays makes it possible for any object to access them, so it makes sense to make them global. But yes, you still need to put items into them and remove them again yourself.

I have not been able to glean the answers from jagkallasfrags executable game other than it is using both keys and mouse. Thus I will create a demo game which contains both an inventory system and uses a static view with which to display it. This is however likely to take some time but both are recurring topic questions.
  • 1




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users