Jump to content


Photo
* * * - - 1 votes

Inventory And Shop Example


  • Please log in to reply
46 replies to this topic

#1 thegame

thegame

    OinkIguana

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

Examples: Inventory and Shop Example ||| Dialog System with messages, questions, input and more ||| Old dialog box example (May not work in new versions of GM:Studio)
Jam Games: 11: Abby and Elliot - 21st place ||| 12: Forks - 16th place ||| 13: White - 19th place
Completed games: White


#2 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • GMC Elder
  • 17247 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

    OinkIguana

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

Examples: Inventory and Shop Example ||| Dialog System with messages, questions, input and more ||| Old dialog box example (May not work in new versions of GM:Studio)
Jam Games: 11: Abby and Elliot - 21st place ||| 12: Forks - 16th place ||| 13: White - 19th place
Completed games: White


#5 thegame

thegame

    OinkIguana

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

Examples: Inventory and Shop Example ||| Dialog System with messages, questions, input and more ||| Old dialog box example (May not work in new versions of GM:Studio)
Jam Games: 11: Abby and Elliot - 21st place ||| 12: Forks - 16th place ||| 13: White - 19th place
Completed games: White


#6 decroded

decroded

    GMC Member

  • GMC Member
  • 584 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
Don't forget to +1 if I have helped you ========>

#7 thegame

thegame

    OinkIguana

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

Examples: Inventory and Shop Example ||| Dialog System with messages, questions, input and more ||| Old dialog box example (May not work in new versions of GM:Studio)
Jam Games: 11: Abby and Elliot - 21st place ||| 12: Forks - 16th place ||| 13: White - 19th place
Completed games: White


#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

    OinkIguana

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

Examples: Inventory and Shop Example ||| Dialog System with messages, questions, input and more ||| Old dialog box example (May not work in new versions of GM:Studio)
Jam Games: 11: Abby and Elliot - 21st place ||| 12: Forks - 16th place ||| 13: White - 19th place
Completed games: White


#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
Posted ImageRate this topic:0 | 1 | 2 | 3 | 4 | 5

#12 thegame

thegame

    OinkIguana

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

Examples: Inventory and Shop Example ||| Dialog System with messages, questions, input and more ||| Old dialog box example (May not work in new versions of GM:Studio)
Jam Games: 11: Abby and Elliot - 21st place ||| 12: Forks - 16th place ||| 13: White - 19th place
Completed games: White


#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

    OinkIguana

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

Examples: Inventory and Shop Example ||| Dialog System with messages, questions, input and more ||| Old dialog box example (May not work in new versions of GM:Studio)
Jam Games: 11: Abby and Elliot - 21st place ||| 12: Forks - 16th place ||| 13: White - 19th place
Completed games: White


#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
Posted Image

#18 xblmetallink1993

xblmetallink1993

    GMC Member

  • New Member
  • 5 posts
  • Version:GM8

Posted 23 June 2012 - 02:49 PM

Perfect, thank you :D
  • 0
Hiiiiyahhh

#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

    OinkIguana

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

Examples: Inventory and Shop Example ||| Dialog System with messages, questions, input and more ||| Old dialog box example (May not work in new versions of GM:Studio)
Jam Games: 11: Abby and Elliot - 21st place ||| 12: Forks - 16th place ||| 13: White - 19th place
Completed games: White


#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

    OinkIguana

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

Examples: Inventory and Shop Example ||| Dialog System with messages, questions, input and more ||| Old dialog box example (May not work in new versions of GM:Studio)
Jam Games: 11: Abby and Elliot - 21st place ||| 12: Forks - 16th place ||| 13: White - 19th place
Completed games: White


#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

    OinkIguana

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

Examples: Inventory and Shop Example ||| Dialog System with messages, questions, input and more ||| Old dialog box example (May not work in new versions of GM:Studio)
Jam Games: 11: Abby and Elliot - 21st place ||| 12: Forks - 16th place ||| 13: White - 19th place
Completed games: White


#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

    OinkIguana

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

Examples: Inventory and Shop Example ||| Dialog System with messages, questions, input and more ||| Old dialog box example (May not work in new versions of GM:Studio)
Jam Games: 11: Abby and Elliot - 21st place ||| 12: Forks - 16th place ||| 13: White - 19th place
Completed games: White


#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
working on mechanics alone for my game and already more than 520 lines of code :P
this is insane
still really need an animator, anyone wanna join The Hidden Games as an animator with 10 % game sales?

#29 thegame

thegame

    OinkIguana

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

Examples: Inventory and Shop Example ||| Dialog System with messages, questions, input and more ||| Old dialog box example (May not work in new versions of GM:Studio)
Jam Games: 11: Abby and Elliot - 21st place ||| 12: Forks - 16th place ||| 13: White - 19th place
Completed games: White


#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

#31 thegame

thegame

    OinkIguana

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

Examples: Inventory and Shop Example ||| Dialog System with messages, questions, input and more ||| Old dialog box example (May not work in new versions of GM:Studio)
Jam Games: 11: Abby and Elliot - 21st place ||| 12: Forks - 16th place ||| 13: White - 19th place
Completed games: White


#32 Killakitty

Killakitty

    GMC Member

  • New Member
  • 11 posts

Posted 09 July 2013 - 07:08 PM

OMFG I AM IN LOVE WITH THIS!!! :D USING IT!!! tysm YOU ROCK!!!!!!!


  • 0

#33 vinyard88

vinyard88

    GMC Member

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

Posted 24 August 2013 - 01:45 AM

Hi,

 

Does anyone know how to move the location of the grid the items goes into in the inventory? I'm playing around with the numbers but the grid doesn't appear to move. Which values in here would determine where the grid is created? Or am I looking in the wrong spot?

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;
}

Any help would be great!


  • 0

#34 thegame

thegame

    OinkIguana

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

Posted 24 August 2013 - 02:50 AM

The grid is not created there... It's created in inv_init(). The code you have shown here simply finds the location of an item in the grid, and then ends. This looks like just the beginning of a script. Showing the rest of it would help solve your problem.

Anyway, there is no way to 'move a grid', or 'determine where a grid is created'. Grids are given a number (their grid ID) which is stored as a variable in an object or as a global variable. This is used to access it, and cannot be changed if that's what you mean by move it.

If you meant move from the bank to the inventory, you need to write your own script. Assuming you have bank scripts that are named bank_add, and bank_subtract, and do essentially the same thing as the inv_ equivalents, something like this should work for transferring an item to the bank from the inventory:
var slot = argument0;
var item = inv_read(slot, ItemName);
if(inv_subtract(slot, 1)) {
    bank_add(item, 1);
}
Note that this is not tested or anything, so I'm not sure... but hopefully it gives you an idea.
Edit: I pressed submit by accident :P. Just finishing up

Edited by thegame, 24 August 2013 - 02:54 AM.

  • 0

Examples: Inventory and Shop Example ||| Dialog System with messages, questions, input and more ||| Old dialog box example (May not work in new versions of GM:Studio)
Jam Games: 11: Abby and Elliot - 21st place ||| 12: Forks - 16th place ||| 13: White - 19th place
Completed games: White


#35 vinyard88

vinyard88

    GMC Member

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

Posted 24 August 2013 - 05:12 AM

The grid is not created there... It's created in inv_init(). The code you have shown here simply finds the location of an item in the grid, and then ends. This looks like just the beginning of a script. Showing the rest of it would help solve your problem.Anyway, there is no way to 'move a grid', or 'determine where a grid is created'. Grids are given a number (their grid ID) which is stored as a variable in an object or as a global variable. This is used to access it, and cannot be changed if that's what you mean by move it.If you meant move from the bank to the inventory, you need to write your own script. Assuming you have bank scripts that are named bank_add, and bank_subtract, and do essentially the same thing as the inv_ equivalents, something like this should work for transferring an item to the bank from the inventory:

var slot = argument0;
var item = inv_read(slot, ItemName);
if(inv_subtract(slot, 1)) {
    bank_add(item, 1);
}
Note that this is not tested or anything, so I'm not sure... but hopefully it gives you an idea.Edit: I pressed submit by accident :P. Just finishing up

Hi, what I mean is how can I select where on the grid the first item is entered when picked up. The item goes into the top left of the screen and can't appear to move the location.

Thanks,
  • 0

#36 thegame

thegame

    OinkIguana

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

Posted 24 August 2013 - 02:35 PM

Ah. I see. Finding the slot in the grid that holds the item can be done by adding this script, which I called inv_has in my game:
/* Script: inv_has(item);
argument0: item     -> The item to look for (string or real)

Returns the slot of the item in the inventory, or -1 if its
not there.
*/
var item = argument0;
if(is_string(item)) {
    //Get the index if the item given is a string
    item = ds_grid_value_x(itemDB, 0, ItemName, ds_grid_width(itemDB) - 1, ItemName, item);
}
if(ds_grid_value_exists(inv, 0, ItemIndex, ds_grid_width(inv) - 1, ItemIndex, item)) {
    return ds_grid_value_x(inv, 0, ItemIndex, ds_grid_width(inv) - 1, ItemIndex, item);
} else {
    return -1;
}
To move it on the screen, you have to do something with it where it is being drawn. I think that was in the Draw event of the Controller.
When drawing, make the x position of the item (ItemW * (slot % itemsPerRow)), or something like that so that it moves over depending on the grid slot.
  • 0

Examples: Inventory and Shop Example ||| Dialog System with messages, questions, input and more ||| Old dialog box example (May not work in new versions of GM:Studio)
Jam Games: 11: Abby and Elliot - 21st place ||| 12: Forks - 16th place ||| 13: White - 19th place
Completed games: White


#37 Mrbear91

Mrbear91

    Bear Games Dev

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

Posted 08 September 2013 - 04:29 AM

i hope this post isent dead!

 

Downloaded it and get this error, im using Game make studio

 

In object obj_controller, event Game Start, action 1 at line 5: Unknown function or script: font_add_sprite
In object obj_shop, event Draw, action 1 at line 32: Unknown function or script: shop_get
In object obj_shop, event Key Press B-key, action 1 at line 2: Unknown function or script: shop_get
In object obj_shop, event Key Press X-key, action 1 at line 2: Unknown function or script: shop_close
In object obj_inventory, event Draw, action 1 at line 43: Unknown function or script: wrap_string
In object parent_collision_box, event Create, action 1 at line 4: Unknown function or script: variable_local_exists

  • 0

coollogo_com-13168104_zps6w7ocqzz.jpg

Silent Warfare v1.3 Out NOW! Jam 18 Rank: 47

http://gmc.yoyogames...opic=662454&hl=


#38 thegame

thegame

    OinkIguana

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

Posted 09 September 2013 - 12:27 AM

Hmm... the last one is an obsolete function, so it makes sense that you would get that error. This example was created in GM 8.1 and I'm not sure I updated it since then.
The first function is not allowed to be used in the free version of GM:S. Is it possible you are using that one?
The other ones as well, due to the limited number of scripts in the free version it might give those errors...

Edited by thegame, 09 September 2013 - 12:29 AM.

  • 0

Examples: Inventory and Shop Example ||| Dialog System with messages, questions, input and more ||| Old dialog box example (May not work in new versions of GM:Studio)
Jam Games: 11: Abby and Elliot - 21st place ||| 12: Forks - 16th place ||| 13: White - 19th place
Completed games: White


#39 Boysano

Boysano

    GMS Pro Member

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

Posted 11 September 2013 - 07:50 AM

We really need to start updating everything to GMS, this is a good example, even a staff choice which doesnt work after the so called easy import to new studio, since obsolete functions exist.

 

Nice that you shared this example though! 

 

Ok I got it to work basically in GMS Pro registered, after I imported the .gmk and then editted the Create code in the object "parent_collision_box"

to this

 

//if(variable_local_exists("width"))
width = 2;
    image_xscale = width / 2;
//if(variable_local_exists("height"))
height = 2;
    image_yscale = height / 2;

 

Basically GMS doesnt like variables which is not declared locally. So this is just a workaround for demo to run.


Edited by Boysano, 11 September 2013 - 08:06 AM.

  • 0

#40 thegame

thegame

    OinkIguana

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

Posted 11 September 2013 - 11:20 PM

Another workaround (that still has the collisions working) is to use the new room editor feature that allows you to scale the objects in the editor, and remove that code completely.
I've modified the example to do it that way, and fixed a few other incompatibility issues as well.
The .gmz is now available for download on the OP.

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

  • 0

Examples: Inventory and Shop Example ||| Dialog System with messages, questions, input and more ||| Old dialog box example (May not work in new versions of GM:Studio)
Jam Games: 11: Abby and Elliot - 21st place ||| 12: Forks - 16th place ||| 13: White - 19th place
Completed games: White


#41 Nexusrex

Nexusrex

    WRYYYY!

  • GMC Member
  • 429 posts
  • Version:GM8

Posted 12 June 2014 - 07:33 PM

great example i will use it in my rpg game its really great example it contains everything thank you


  • 0

e5LGZAz.png

My W.I.P Sidescrolling Shooter


#42 idVORON

idVORON

    GMC Member

  • New Member
  • 4 posts
  • Version:GM:Studio

Posted 13 January 2015 - 05:02 PM

Awesome!  :thanks:


  • 0

#43 buckcreekbeast

buckcreekbeast

    GMC Member

  • New Member
  • 12 posts
  • Version:GM:Studio

Posted 20 April 2015 - 12:52 AM

I love it. Works flawlessly and has a lot of potential to expand upon and customize. Yours are some of the only examples\resources that do exactly what they are intended to. So for all these reasons, thank you!


  • 0

/// Your great Uncle Rudy.

/// I bloody hate it when I misplace a sandwich . . .


#44 Nexusrex

Nexusrex

    WRYYYY!

  • GMC Member
  • 429 posts
  • Version:GM8

Posted 20 May 2015 - 10:10 AM

Great Job...After looking at the example..Just one question..Where did you put the variable of the score (the first 30 point)??


  • 0

e5LGZAz.png

My W.I.P Sidescrolling Shooter


#45 thegame

thegame

    OinkIguana

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

Posted 21 May 2015 - 11:02 AM

score is first set at the bottom of the inv_init(); script.
  • 1

Examples: Inventory and Shop Example ||| Dialog System with messages, questions, input and more ||| Old dialog box example (May not work in new versions of GM:Studio)
Jam Games: 11: Abby and Elliot - 21st place ||| 12: Forks - 16th place ||| 13: White - 19th place
Completed games: White


#46 Nexusrex

Nexusrex

    WRYYYY!

  • GMC Member
  • 429 posts
  • Version:GM8

Posted 21 May 2015 - 04:27 PM

Well, another noob question..If i want to make a money object (that increases score), In the collision with player object event, what can i put?


Edited by Nexusrex, 21 May 2015 - 04:32 PM.

  • 0

e5LGZAz.png

My W.I.P Sidescrolling Shooter


#47 thegame

thegame

    OinkIguana

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

Posted 22 May 2015 - 12:29 AM

score is a built-in global variable, which means it's the same no matter where you use it. To add money, just write:
score += 5
In the collision with Player event. It can be any number you want, that will be how much money you give.
  • 0

Examples: Inventory and Shop Example ||| Dialog System with messages, questions, input and more ||| Old dialog box example (May not work in new versions of GM:Studio)
Jam Games: 11: Abby and Elliot - 21st place ||| 12: Forks - 16th place ||| 13: White - 19th place
Completed games: White





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users