i’m trying to code an inventory working from the list-based inventory i used in Game Maker. here is the basic code:
inv_add
//In this script we will add items to the inventory
var found = -1;
for (var i = 0; i < global.invMax; i++) {
if (global.inv[i, 0] == argument0) {
found = i; //We affect to found the place of the item if we find it
break;
}
}
//If it doesn't exist
if (found == -1) {
//Let's check if there is an empty place to add our item
for (var j = 0; j < global.invMax; j++) {
if (global.inv[j, 0] == global.NOTHING) {
found = j;
break;
}
}
}
//should have an "inventory is full" contingency
//Now let's add our item to the place we have *
global.inv[found, 0] = argument0; //Argument 0 is the id of the item
global.inv[found, 1] += argument1; //Argument 1 is the amount of the item to add
inv_init
//This script will initialize the inventory
global.inv[0, 0] = 0;//Inv = the inventory array, invMax = max items
global.invMax = 30;//The max items can hold
//Initialize the inv array
for (var i = 0; i < global.invMax; i++) {
global.inv[i, 0] = 0;//This is the item id, every item has its id in the inv_data_item()
global.inv[i, 1] = 0; //This is the amount of the current item
}
inv_data_item
//In this script we will store our items' data
//Let's start with some id
//Note from George: going to store the numbers in an external file and read them into this somehow
//This is just to remember
global.NAME = 0;
global.DESC = 1;
global.PRICE = 2;
global.OBJ_NAME = 3;
//Id of items
global.NOTHING = 0;
global.HP_POTION = 1;
global.MP_POTION = 2;
global.SWORD = 3;
global.SHIELD = 4;
//The data
global.item[0, 0] = global.NOTHING;//the id
global.item[0, 1] = "Nothing";//the name
global.item[0, 2] = "Nothing";//the description
global.item[0, 3] = 0;//price
global.item[0, 4] = "nothing";//object name ???
global.item[1, 0] = global.HP_POTION;//the id
global.item[1, 1] = "HP Potion";//the name
global.item[1, 2] = "Heals HP.";//the description
global.item[1, 3] = 10;//price
global.item[1, 4] = "obj_HP_POTION";//object name
global.item[2, 0] = global.MP_POTION;//the id
global.item[2, 1] = "MP Potion"//the name
global.item[2, 2] = "Restores MP"//the description
global.item[2, 3] = 10;//price
global.item[2, 4] = "obj_MP_POTION";//object name
global.item[3, 0] = global.SWORD;//the id
global.item[3, 1] = "Sword";//the name
global.item[3, 2] = "Sharp object."//the description
global.item[3, 3] = 100;//price
global.item[3, 4] = "obj_SWORD";//object name
global.item[4, 0] = global.SHIELD;//the id
global.item[4, 1] = "Shield";//the name
global.item[4, 2] = "Blocks attacks.";//the description
global.item[4, 3] = 50;//price
global.item[4, 4] = "obj_SHIELD";//object name
i need to use some kind of external file editor for the list of all items and i have no idea where to even start with that. there is JSON and i found an SQL editor in the assets store. i’m going to have to change a lot of things going from GM to Unity. is this framework still feasible for unity? there aren’t global variables in unity so i’m not sure what i’m going to do to replace that. any feedback?
i decided i might as well create a thread for this because i’m probably going to have more questions as i proceed.
i can add more code in additional posts if you ask for it. this inventory is actually why i quit Game Maker but i’m glad to be using Unity now. it’s better in a lot of ways.