Jump to content


Photo
* * * - - 1 votes

Inventory And Shop Example


  • Please log in to reply
40 replies to this topic

#1 thegame

thegame

    Flying Penguin

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

Posted 27 March 2012 - 08:53 PM

  • Title: Example-Inventory / Example-Inventory-Lite
  • Description: An example of how to do an inventory system in your game.
  • GM Version: GM81, :GM8_new:, GM:S
  • Registered: Yes and no
  • File Type: .gm81, .gmk
  • File Size: 212 kB registered, 213 kB lite
  • File Link: (Registered: .gm81, .gmk) (Lite: .gm81, .gmk) | .gmz
  • Required Extensions: None
  • Required DLLs: None
Summary
This example shows one of many inventory systems, using a ds_grid for an item database, and the inventory itself. The inventory can have a maximum number of items to be held in it, and the item database can be added to at any time in the game, even if the initial size is reached, by extending the grid. The whole inventory can also be drawn and interacted with, only using one object. With the Lite version, arrays are substituted for the grid.

The shops are done in the same manner, though they are a grid that is created by a shopkeeper, and is removed when the shop is closed to conserve memory. The scripts that this example includes are:
  • inv_init() > Create the grids and input the initial values
  • new_item(name, description, sellPrice, buyPrice, maximumQuantity, sprite) > Easily add an item to the item database
  • update_item(index, field, replacement) > Change the information about the item in the item database
  • inv_read(slot, value) > Get the item index or the quantity of the item in the slot
  • itemdb_read(index, value) > Get information on an item by using it's index or name
  • inv_add(item, quantity) > Add the given amount of an item to the inventory
  • inv_buy(item, quantity) > The the item to the inventory, and subtract the buying price from the money variable
  • inv_subtract(slot, quantity) > Remove the amount of items from the slot of the inventory
  • inv_sell(slot, quantity) > Remove the items from the inventory, and add the selling price to the money variable
  • new_shop() > Make a new shop
  • shop_add(item) > Add the item to the shop's merchandise
  • shop_close() > Close the shop, freeing the memory used
  • shop_get(slot) > Get the item from the shop in the slot
  • findInItemDB(item) > Find the index of the item in the item database (only in Lite version)
  • findInInventory(item) > Find the slot the item is in in the inventory (Lite only)
Along with the inventory and shop, this example also shows a simple top down movement and collision system with doors, how to add a font from a sprite, how to do a custom cursor with the cursor turned off, and a pause system.
All code is commented and described so that you will hopefully be able to understand how it works.
If any more features are requested, I will be happy to add them in!

This example also includes a script by Ivaxlar for wrapping strings to a certain width.

Edited by thegame, 11 September 2013 - 11:22 PM.

  • 1

#2 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Retired Staff
  • 15729 posts
  • Version:GM:Studio

Posted 29 March 2012 - 06:17 PM

OK, looks promising. a good set of scripts for the inventory interface. reminds me of something I implemented a long time ago.

Though I found the demo a little plain and a little clumsy, but adequate for the game context.

Worth the look. Good job!
  • 0

#3 Silver Scratch

Silver Scratch

    GMC Member

  • GMC Member
  • 166 posts
  • Version:GM8

Posted 29 March 2012 - 08:32 PM

Wow, love it!
  • 0

#4 thegame

thegame

    Flying Penguin

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

Posted 31 March 2012 - 02:09 AM

Great example can similar to this example be done in Game Maker Lite?

Probably, though it won't be as easy...
I'll see if I can make a Lite version over the next few days.
  • 0

#5 thegame

thegame

    Flying Penguin

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

Posted 02 April 2012 - 08:42 PM

Alright! The Lite edition is now available. Works (hopefully) just as well as the Registered one!
  • 0

#6 decroded

decroded

    GMC Member

  • GMC Member
  • 572 posts
  • Version:GM8

Posted 16 April 2012 - 11:32 AM

.gmk crashed as soon as I ran it on registered v8.0.
Do I need to update or something?


___________________________________________
FATAL ERROR in
action number 1
of Draw Event
for object obj_doormat:

COMPILATION ERROR in code action
Error in code at line 1:
draw_self();
^
at position 1: Unknown function or script: draw_self
  • 1

#7 thegame

thegame

    Flying Penguin

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

Posted 16 April 2012 - 05:24 PM

Uhh... I am not really sure what would cause that... maybe they didn't invent draw_self yet?
Anyhow, you could fix that by replacing the line with
draw_sprite(sprite_index, image_index, x, y);

  • 0

#8 CptGT99

CptGT99

    GMC Member

  • New Member
  • 3 posts
  • Version:GM8

Posted 25 April 2012 - 03:48 PM

hi, im just noob with scripts and i am asking how to fix these 2 errors / i used your codes from lite and registered but almost same errors/ lite errors:
___________________________________________
ERROR in
action number 1
of Other Event: Game Start
for object obj_controller:

In script inv_init:
In script new_item:
Error in code at line 24:
/* Script: new_item(name, description, sell, buy, qty, sprite)argument0 -> The name of the item (string)argument1 -> A description of the item (string)argument2 -> The sell value of the item (real: -1 to be unsellable)argument3 -> The buy price of the item (real)argument4 -> The maximum quantity of the item (real: -1 for unlimited)argument5 -> A sprite of the object (real: sprite index)Adds a new item to the item database. Returns true if all values are correct,false otherwise. If the database is too small, it will make it bigger*/var name, description, sell, buy, qty, sprite, index;name = argument0;description = argument1;sell = argument2;buy = argument3;qty = argument4;sprite = argument5;//The index is the number of items in the databaseindex = itemsInDB;//Check to make sure all the given values are correctif(is_string(name) && is_string(description) && is_real(sell) && is_real(buy) && is_real(qty) && sprite_exists(sprite)) { //If they are, then insert them into the database itemDB[index, ItemName] = name; itemDB[index, ItemDesc] = description; itemDB[index, ItemSell] = sell; itemDB[index, ItemBuy] = buy; itemDB[index, ItemMax] = qty; itemDB[index, ItemSprite] = sprite; itemsInDB += 1; return true;} else return false;
^
at position 19: Unknown variable ItemName

___________________________________________
ERROR in
action number 1
of Create Event
for object obj_inventory:

Error in code at line 10:
while(inv_read(lastFilledSlot, ItemQty)) {
^
at position 33: Unknown variable ItemQty

please help
  • 0

#9 thegame

thegame

    Flying Penguin

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

Posted 26 April 2012 - 12:10 PM

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.
  • 0

#10 CptGT99

CptGT99

    GMC Member

  • New Member
  • 3 posts
  • Version:GM8

Posted 26 April 2012 - 05:02 PM

really thanks! :)
  • 0

#11 creators124

creators124

    awesomeliciousmember

  • GMC Member
  • 866 posts
  • Version:GM8

Posted 27 April 2012 - 12:28 AM

___________________________________________
ERROR in
action number 1
of Other Event: Game Start
for object obj_controller:

Error in code at line 5:
myFont = font_add_sprite(spr_font, 32, false, 2);
^
at position 11: This function is not available in the Lite Edition.

yeah that lite is still not done!
also the words overlap the key:
Posted Image

Edited by creators124, 27 April 2012 - 12:29 AM.

  • 0

#12 thegame

thegame

    Flying Penguin

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

Posted 27 April 2012 - 09:04 PM

Ahh, thank you for finding that one... I'll fix them both right now :P

Edit: Done!

Edited by thegame, 27 April 2012 - 09:22 PM.

  • 0

#13 andyou111

andyou111

    GMC Member

  • New Member
  • 7 posts
  • Version:GM7

Posted 18 May 2012 - 08:53 AM

i love it :wub: :wub: :wub:
  • 0

#14 aavitsland

aavitsland

    GMC Member

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

Posted 22 May 2012 - 07:12 PM

this is nice work done, and I would like to use it with some minor changes to fit my game, but I run into a problem.

ERROR in
action number 1
of Draw Event
for object obj_bag:

Error in code at line 4:
   draw_set_font(myFont);
                 ^
at position 16: Unknown variable myFont

At event game start of obj_controller I have
//Create the item database and the inventory
inv_init();
//Create a custom font from the sprite.
globalvar myFont;
myFont = font_add_sprite(spr_font, 32, false, 2);

Why am I running into that problem?
  • 0

#15 thegame

thegame

    Flying Penguin

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

Posted 22 May 2012 - 07:42 PM

You have definitely put the obj_controller in the first room?
Also make sure that myFont is not used anywhere else... such as another variable, or an object or sprite?
Other than that, there's not much I can think that would be going wrong at the moment... Check those things and if you still have the problem you can send a PM with a link to your game and I'll look at that.

Edited by thegame, 22 May 2012 - 07:42 PM.

  • 0

#16 aavitsland

aavitsland

    GMC Member

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

Posted 22 May 2012 - 08:04 PM

You have definitely put the obj_controller in the first room?
Also make sure that myFont is not used anywhere else... such as another variable, or an object or sprite?
Other than that, there's not much I can think that would be going wrong at the moment... Check those things and if you still have the problem you can send a PM with a link to your game and I'll look at that.


obj_controller is in the first room.
I can not see that is should have been used other places.
I will send You a pm because I am just a little confused about it :)
  • 0

#17 ConnerCoConnerB

ConnerCoConnerB

    GMC Member

  • GMC Member
  • 89 posts
  • Version:GM8

Posted 13 June 2012 - 08:45 PM

I'm Excited to try this. It seems like inventorys are a problem for lots of people
  • 0

#18 xblmetallink1993

xblmetallink1993

    GMC Member

  • New Member
  • 5 posts
  • Version:GM8

Posted 23 June 2012 - 02:49 PM

Perfect, thank you :D
  • 0

#19 atamasco

atamasco

    GMC Member

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

Posted 26 July 2012 - 08:32 PM

So If you open the inventory with, say, enemies around, will it freeze whatever is happening outside the inventory? Thanks!
~atty~
  • 0

#20 thegame

thegame

    Flying Penguin

  • GMC Member
  • 1482 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

#21 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

#22 thegame

thegame

    Flying Penguin

  • GMC Member
  • 1482 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

#23 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

#24 thegame

thegame

    Flying Penguin

  • GMC Member
  • 1482 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

#25 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

#26 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

#27 thegame

thegame

    Flying Penguin

  • GMC Member
  • 1482 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

#28 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

#29 thegame

thegame

    Flying Penguin

  • GMC Member
  • 1482 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

#30 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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users