Jump to content


Photo
- - - - -

Inventory and Shop Example


  • Please log in to reply
31 replies to this topic

#21 thegame

thegame

    Flying Penguin

  • GMC Member
  • 1404 posts
  • Version:GM:Studio

Posted 27 July 2012 - 02:22 AM

Yes, when the inventory is opened the rest of the game is paused.
  • 0

#22 atamasco

atamasco

    GMC Member

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

Posted 06 August 2012 - 10:44 PM

Yes, when the inventory is opened the rest of the game is paused.

thanks! another thing, could you add an item to a shop when you sell it? for instance, sell the shopkeeper a bow, and when you check his shop he'll have it for re-sale? I plan for this to be a simple banking system. Thanks again! :D
~atty~



  • 0

#23 thegame

thegame

    Flying Penguin

  • GMC Member
  • 1404 posts
  • Version:GM:Studio

Posted 07 August 2012 - 05:49 PM

For a banking system, you may want to create a second grid the same way as the inventory and make that your bank. Add items to the bank at the same time as removing from your inventory:
/* Script: bank_add(item, amt)
argument0 -> The slot to remove the item from in the inventory
argument1 -> The amount the item to move to the bank

Adds items to the bank then removes the item from the inventory. 
Returns true on success.
*/
var bankSlot, invSlot, item, amt;
invSlot = argument0;
item = inv_read(invSlot, ItemIndex);

if(ds_grid_value_exists(bank, 0, 0, ds_grid_width(bank), 0, item)) {
    //bankSlot is either the place where the item is already being stored...
    bankSlot = ds_grid_value_x(bank, 0, 0, ds_grid_width(bank), 0, item);
} else if(ds_grid_value_exists(bank, 0, 0, ds_grid_width(bank), 0, -1)) {
    //... or the first empty space if the item is not already in the bank
    bankSlot = ds_grid_value_x(bank, 0, 0, ds_grid_width(bank), 0, -1);
} else {
    //Return false and end script if the bank is full
    return false;
    exit;
}
amt = argument1;

if(inv_read(itemSlot, ItemQty) >= amt) {
    //Set the index
    ds_grid_set(bank, slot, ItemIndex, item);
    //And add the amount to the quantity
    ds_grid_add(bank, slot, ItemQty, amt);
    inv_subtract(invSlot, amt);
    return true;
} else return false; //Not enough of the item in the inventory

//////////////////////////////////////////////
//To add to the bank would be something like this
bank_add(slotOfItem, 1);

Just so you know, the above script isn't tested or anything... some things may be wrong. It should basically be the same as the inv_add() script, but adds to bank and removes the item from inventory afterwards.

As for selling to a shop and it being there later, that would require you to save the items that have been sold to each shop in some way, which may lead to many grids.

Edited by thegame, 07 August 2012 - 05:51 PM.

  • 0

#24 atamasco

atamasco

    GMC Member

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

Posted 07 August 2012 - 06:53 PM

For a banking system, you may want to create a second grid the same way as the inventory and make that your bank. Add items to the bank at the same time as removing from your inventory:

/* Script: bank_add(item, amt)
argument0 -> The slot to remove the item from in the inventory
argument1 -> The amount the item to move to the bank

Adds items to the bank then removes the item from the inventory. 
Returns true on success.
*/
var bankSlot, invSlot, item, amt;
invSlot = argument0;
item = inv_read(invSlot, ItemIndex);

if(ds_grid_value_exists(bank, 0, 0, ds_grid_width(bank), 0, item)) {
    //bankSlot is either the place where the item is already being stored...
    bankSlot = ds_grid_value_x(bank, 0, 0, ds_grid_width(bank), 0, item);
} else if(ds_grid_value_exists(bank, 0, 0, ds_grid_width(bank), 0, -1)) {
    //... or the first empty space if the item is not already in the bank
    bankSlot = ds_grid_value_x(bank, 0, 0, ds_grid_width(bank), 0, -1);
} else {
    //Return false and end script if the bank is full
    return false;
    exit;
}
amt = argument1;

if(inv_read(itemSlot, ItemQty) >= amt) {
    //Set the index
    ds_grid_set(bank, slot, ItemIndex, item);
    //And add the amount to the quantity
    ds_grid_add(bank, slot, ItemQty, amt);
    inv_subtract(invSlot, amt);
    return true;
} else return false; //Not enough of the item in the inventory

//////////////////////////////////////////////
//To add to the bank would be something like this
bank_add(slotOfItem, 1);

Just so you know, the above script isn't tested or anything... some things may be wrong. It should basically be the same as the inv_add() script, but adds to bank and removes the item from inventory afterwards.

As for selling to a shop and it being there later, that would require you to save the items that have been sold to each shop in some way, which may lead to many grids.

Thanks! That's really helpful, and definitely easy enough to modify for my needs. But, how exactly would you create the bank? Just create an inventory by another name? It seems to me bank_add is just inventory add, but rename a few variables. Is that correct? :) thanks so much!
edit: and one more thing, I hope i'm not being annoying, will it add to the bank from the inventory while the bank controller object isn't activated (i.e. the bank screen is closed)? Would you need to have the bank open a lot, taking up memory?



Edited by atamasco, 07 August 2012 - 07:00 PM.

  • 0

#25 thegame

thegame

    Flying Penguin

  • GMC Member
  • 1404 posts
  • Version:GM:Studio

Posted 07 August 2012 - 07:35 PM

Yes, this is just another inventory by another name. The script is basically the same with different variables. The bank would be like your inventory, but only accessible from the bank locations.

If you store the bank in a global variable (just like the inventory) you can add to the bank from anywhere. You will have to keep the bank grid available at all times, just like the inventory, but it will not always be showed on screen (so don't close it like the shop or you will lose all the items in it). This won't take too much memory, as it is just 1 more grid. The reason you have to close the shops is because you will probably have many shops in the game, and if a player visits 30 of them, or opens the same one 30 times, you'll have created 30 shop grids.

Edit: typo

Edited by thegame, 07 August 2012 - 07:36 PM.

  • 0

#26 atamasco

atamasco

    GMC Member

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

Posted 07 August 2012 - 08:51 PM

Yes, this is just another inventory by another name. The script is basically the same with different variables. The bank would be like your inventory, but only accessible from the bank locations.

If you store the bank in a global variable (just like the inventory) you can add to the bank from anywhere. You will have to keep the bank grid available at all times, just like the inventory, but it will not always be showed on screen (so don't close it like the shop or you will lose all the items in it). This won't take too much memory, as it is just 1 more grid. The reason you have to close the shops is because you will probably have many shops in the game, and if a player visits 30 of them, or opens the same one 30 times, you'll have created 30 shop grids.

Edit: typo

thank you so much! truly wonderful news. i'll just duplicate a few scripts, change a few names, values, and there, a bank. You sir, have earned a place in the start-up credits.
regards,
Atty

Edited by atamasco, 09 August 2012 - 10:15 PM.

  • 0

#27 Antin

Antin

    GMC Member

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

Posted 19 August 2012 - 05:51 PM

Hi,
thank's for this very good example! I would like to insert it in my game, but I've a problem with constants. Aparently, I need to do this :

You have to define them in the constants, found under [Resources > Define Constants (Shift + Ctrl + N)]
Open this in your file and the example file, then make yours look the same as the example.


but I work with the lite edition and that's impossible! I can run your example without problems but I can't make this inventory in a differant file!
May be the problem must be solve by using variables? In that case, could you give me the informations associate with these constants?

thank's for your help!
  • 0

#28 thegame

thegame

    Flying Penguin

  • GMC Member
  • 1404 posts
  • Version:GM:Studio

Posted 26 August 2012 - 03:55 PM

Ahh... that is a problem. All you need to do to solve this is put this code in the Game Start event of your controller object, BEFORE the inv_init():
globalvar ItemIndex, ItemQty, ItemName, ItemDesc, ItemSell, ItemBuy, ItemMax, ItemSprite, ItemGet, ItemUse, ItemW, ItemH;
ItemIndex = 0;
ItemQty = 1;
ItemName = 0;
ItemDesc = 1;
ItemSell = 2;
ItemBuy = 3;
ItemMax = 4;
ItemSprite = 5;
ItemGet = 0;
ItemUse = 1;
ItemW = 32;
ItemH = 32;
This will basically be the same as making constants, though they can be changed during the game (which would cause errors with the inventory scripts. Do not change them!)

I'll add this into the lite version of the example now.

Edited by thegame, 26 August 2012 - 03:57 PM.

  • 0

#29 Ventus

Ventus

    GMC Member

  • New Member
  • 21 posts
  • Version:GM8.1

Posted 03 September 2012 - 05:37 PM

for some reason i cant download any of the links, is there a way you can send it to me directly or somthing? maybe by email or pm?
i really need this for my game so i hope i can get it
  • 0

#30 thegame

thegame

    Flying Penguin

  • GMC Member
  • 1404 posts
  • Version:GM:Studio

Posted 04 September 2012 - 09:55 PM

for some reason i cant download any of the links, is there a way you can send it to me directly or somthing? maybe by email or pm?
i really need this for my game so i hope i can get it

Just to make sure, are you right clicking and saving link as? If so, that will not work... Click on the link (left click) and then on the green Download button in the middle.

Otherwise, I'll find a way to send it to you.
  • 0

#31 oscargarin

oscargarin

    GMC Member

  • GMC Member
  • 7 posts

Posted 23 February 2013 - 06:09 PM

Hi, do you know if the source fiel exists for Game Maker Studio?

OG
  • 0

#32 thegame

thegame

    Flying Penguin

  • GMC Member
  • 1404 posts
  • Version:GM:Studio

Posted 24 February 2013 - 09:16 PM

There's no GM:S specific one, but I'm pretty sure that the current version can be ported into GM:S without problems.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users