N
NTiger7
Guest
IDE v. 2.3.0.529, runtime v. 2.3.0.401
So I successfully have made an shop where the player can purchase items from and it will be added to their inventory, but I would like to use the same code for all the other shops so it doesn't use up more memory. The only thing I would have to change is what items are being displayed, how many items are available to purchase at the particular store, and converting the items in the store to the items in the inventory. I have used the create event, draw gui event, step event, and clean up event in my object where the item shop code is located. How would I go to reuse this? In order to get the shop to display, the player has to run into the trigger area and press 'w' and then the shop will display. Should I use this trigger area to rename variables? Is it even possible? Should I change this to a script? I don't know what is the best course of action for this. Here is the code I would like to change:
In the create event (Note: I just am showing one item as an example, but I have several different items here).
In the draw gui event:
In the step event:
There are multiple else ifs with each item. There probably is a more efficient way to do this but I'm not sure.
And the clean up event:
Or do I have to duplicate the object and change all the instance variable names for each shop (which there will be several so it will just take some time)? If the code I have known is not enough, just let me know, but all the other code would be the same except if I need to rename the instance variables. Thank you for any advice.
So I successfully have made an shop where the player can purchase items from and it will be added to their inventory, but I would like to use the same code for all the other shops so it doesn't use up more memory. The only thing I would have to change is what items are being displayed, how many items are available to purchase at the particular store, and converting the items in the store to the items in the inventory. I have used the create event, draw gui event, step event, and clean up event in my object where the item shop code is located. How would I go to reuse this? In order to get the shop to display, the player has to run into the trigger area and press 'w' and then the shop will display. Should I use this trigger area to rename variables? Is it even possible? Should I change this to a script? I don't know what is the best course of action for this. Here is the code I would like to change:
In the create event (Note: I just am showing one item as an example, but I have several different items here).
GML:
shop_items = 6;
/................... Several lines down
enum burgershopItems {
burger = 0,
}
//Shop Inventory
ds_shop[# 0, 0] = burgershopItems.burger;
//Price per item
ds_shop[# 1, 0] = 5;
#region Create Items Info Grid
ds_shop_items_info = ds_grid_create(4, burgershopItems.height);
//---Item Names
//example
var z = 0, i = 0;
ds_shop_items_info[# z, i++] = "Burger";
//---Item Descriptions
var z = 1, i = 0;
ds_shop_items_info[# z, i++] = "This is the item description.";
//----Mass in kg
var z = 2, i = 0;
ds_shop_items_info[# z, i++] = 0.200;
//---Item Volume in cm^3
var z = 3, i = 0;
ds_shop_items_info[# z, i++] = 194;
GML:
var shop_grid = ds_shop;
shop_Info_grid = ds_shop_items_info;
//Anywhere in the draw GUI where these variables are found would equal the new grid
GML:
if (mouse_check_button_pressed(mb_left)) && (shop_hovered_slot >= 0) {
global.totalMoney = global.totalMoney - ss_item_price;
with (oInventory) {
var inv_grid = ds_inventory;
var ii = 0;
repeat (inv_slots) {
var iItem = inv_grid[# 0, ii];
if (iItem > 0) ii++;
else {
if (ss_item = 0) {
for (var j = 0; j < inv_slots; j++) { //if that item exists, stack on top of it
if (inv_grid[# 0, j] = 1) {
inv_grid[# 1, j] += 1;
totalMass += ds_items_info[# 2, inv_grid[# 0, j]];
totalVolume += (ds_items_info[# 3, inv_grid[# 0, j]] / 1000);
break;
}
}
if (j == inv_slots) { //if item does not exist, create new item
inv_grid[# 0, ii] = 1;
inv_grid[# 1, ii] = 1;
totalMass += (inv_grid[# 1, ii] * ds_items_info[# 2, inv_grid[# 0, ii]]);
totalVolume += (inv_grid[# 1, ii] * (ds_items_info[# 3, inv_grid[# 0, ii]] / 1000));
}
}
And the clean up event:
GML:
ds_grid_destroy(ds_shop);
ds_grid_destroy(ds_shop_items_info);