Jump to content


Photo
- - - - -

Inventory and Shop Example


  • Please log in to reply
31 replies to this topic

#1 thegame

thegame

    Flying Penguin

  • GMC Member
  • 1404 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:
  • Registered: Yes and no
  • File Type: .gm81, .gmk
  • File Size: 212 kB registered, 213 kB lite
  • File Link: (Registered: .gm81, .gmk) (Lite: .gm81, .gmk)
  • 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, 26 August 2012 - 04:01 PM.

  • 0

#2 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14391 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 Fira

Fira

    God Bless You All

  • GMC Member
  • 1390 posts

Posted 31 March 2012 - 12:32 AM

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

#5 thegame

thegame

    Flying Penguin

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

#6 thegame

thegame

    Flying Penguin

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

#7 decroded

decroded

    GMC Member

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

#8 thegame

thegame

    Flying Penguin

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

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

#10 thegame

thegame

    Flying Penguin

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

#11 CptGT99

CptGT99

    GMC Member

  • New Member
  • 3 posts
  • Version:GM8

Posted 26 April 2012 - 05:02 PM

really thanks! :)
  • 0

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

#13 thegame

thegame

    Flying Penguin

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

#14 andyou111

andyou111

    GMC Member

  • New Member
  • 7 posts
  • Version:GM7

Posted 18 May 2012 - 08:53 AM

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

#15 aavitsland

aavitsland

    GMC Member

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

#16 thegame

thegame

    Flying Penguin

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

#17 aavitsland

aavitsland

    GMC Member

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

#18 ConnerCoConnerB

ConnerCoConnerB

    GMC Member

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

#19 xblmetallink1993

xblmetallink1993

    GMC Member

  • New Member
  • 5 posts
  • Version:GM8

Posted 23 June 2012 - 02:49 PM

Perfect, thank you :D
  • 0

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users