GM Tutorial #1(Inventory System) 1/4

in #tutorial6 years ago

Our end goal for this tutorial will look somewhat like this.
Capture.PNG
First off your going to want to create a new object called obj_inventory.

For the create event you will need
///Drag variables
dragPosY = 704;
dragPosX = 704;
dragPosYmin = 80;
dragPosYmax = 784;
What this does is setups where the dragging bar will be well drawn on at.

You will also need to init all the other variables as well.
posX = 384;
posY = 480;
selection = 1;
item = "break_tool";
in = 0;
ic=0
info_x = 0;
info_y = 0;
info_id = "";
info = false;
You will see what all of these do later in the tutorial.

For the alarm[0] event you just need
///Hide info
info = false;
This basically just hides the info when your mouse is not over the box.

Now for the global Mouse Left Released Event you need
///Selection
lastX = device_mouse_x_to_gui(0);
lastY = device_mouse_y_to_gui(0);

pos = 1;
dpy = 0;
repeat(10){
dpx = 0;
repeat(8){
if (point_in_rectangle(lastX,lastY,posX+dpx,posY+dpy,posX+dpx+64,posY+dpy+64)){
item = ini_player_read_string("inventory_item",string(pos));
info_x = dpx; info_y = dpy; info_id = item; info = true; alarm[0] = 60;
selection = pos;
}
pos += 1;
dpx += 64;
}
dpy += 64;
}
This just loops through the item boxes checking if there is an item already in it and if so it will select it.

Finally in the first draw gui event you need.

///Draw
draw_set_color(c_white);
draw_sprite(spr_inventory,0,posX-32,posY-32);
pos = 1;
row = 1;
dpy = posY;
repeat(10){
col = 1;
dpx = posX;
repeat(8){
in = safe_ini_player_read_string("inventory_item",string(pos))
ic = safe_ini_player_read_real("inventory_item_count",string(pos));
if (safe_ini_player_exists("inventory_item_count",string(pos))){
if (selection = pos){
draw_sprite(spr_selection,0,dpx,dpy);
if (info = true){
draw_text_cover(dpx+32,dpy-16,item_name(item));
}
}
on_item_draw(dpx,dpy,in,ic);
if (ini_player_read_string("wearing","hat") = in)
or (ini_player_read_string("wearing","face") = in)
or (ini_player_read_string("wearing","shirt") = in)
or (ini_player_read_string("wearing","pants") = in)
or (ini_player_read_string("wearing","hand") = in)
or (ini_player_read_string("wearing","back") = in)
or (ini_player_read_string("wearing","legs") = in){
draw_sprite(spr_checked,0,dpx,dpy);
}
}
col += 1;
pos += 1;
dpx += 64;
}
row += 1;
dpy += 64;
}
What this does is checks if you have any clothes on and if so it selects them this also draws the sprites for each item it also draws their name and amount

Finally for the dragging will need this in your draw gui event
///Drag proccess
draw_sprite(spr_drag,1,dragPosX,dragPosY);

gui_mouse_x = device_mouse_x_to_gui(0);
gui_mouse_y = device_mouse_y_to_gui(0);

if (point_in_rectangle(gui_mouse_x,gui_mouse_y,dragPosX-64,dragPosY-32,dragPosX+64,dragPosY+32)and mouse_check_button(mb_left)){
dragPosY = gui_mouse_y;
global.drag = true;
}
else{
global.drag = false;
}

if (dragPosY < dragPosYmin){dragPosY = dragPosYmin}
if (dragPosY > dragPosYmax){dragPosY = dragPosYmax}

posY = dragPosY + 48;
Basically this checks if your clicking on it and if so it changes the y of the drag sprite, to your mouse y.

Since, this tutorial is already getting pretty long and there is still a lot of things to cover I will split this into a four part series
1.obj_inventory Setup
2.Inventory related scripts setup
3.Player Equipping
4.Final Notes

Finally, heres some sprites for the inventory if you don't want to do them yourself.
![spr_inventory_0.png](
spr_inv_0.png
spr_slot_0.png
spr_selection_0.png
spr_drag_0.png

Coin Marketplace

STEEM 0.22
TRX 0.20
JST 0.034
BTC 98713.95
ETH 3352.77
USDT 1.00
SBD 3.07