Weapon Inventory Code

T2008

Member
I've been creating inventory system where I have many weapons. Right now, I have inventory menu that has a slot for each weapons (pistol slot 0, rifle slot2, etc). and I have code for the selection of it. I also have a bottom cycling hud that allows quick scrolling of weapons. The problem is, my system doesn't allow for someone to have different order for weapons. For example, if someone picks up rifle before pistol, then it won't move over to the first slot. I've looked around and most inventory tutorials I've seen are lengthy and complicated. Does any one have any suggestions as to how to do this (or a good tutorial for this regarding weapons)? I'm not advanced and haven't used arrays, etc. Any advice would be greatly appreciated! (My code is very lengthy so I only posted bottom inventory scrolling hud and not inventory menu code,).

Note: I added the clock code because when not there, it appears that the item is selected/deselected immediately; clock made it work.

Code:
Bottom Weapon Cycling Code:

//---Button 1 Mouse Over Coordinates
//---Check if mouse's coordinates are inside button
if mouse_x > _xx+3 //Coordinate of Left Side of Button
and mouse_x < _xx+19 // Coordinate of Right Side of Button
and mouse_y > _yy+518//36 //Coordinate of Top of Button
and mouse_y < _yy+534 //47 //Coordinate of Bottom of Button
{
    button1_mouse_over_button = true;
}
//---Check if mouse coordinates are not insdie button
if mouse_x < _xx+3 // Coordinate of Left Side of Button
or mouse_x > _xx+19 // Coordinate of Right Side of Button
or mouse_y < _yy+518 // Coordinate of Top of Button
or mouse_y > _yy+534 // Coordinate of Bottom of Button
{
    button1_mouse_over_button = false;
}
[omitted code for rest of buttons;; same thing with different coordiantes] 

//Button1 Selection
if (button1_mouse_over_button) 
&& (mouse_check_button_pressed(mb_left)) {
    //Play Sound
    audio_play_sound(snd_click,10,false);
    //Select Button
    if (button1_clock >= 10) && (!button1_selected) {
        //Change Select Variable To True
        button1_selected = true;
        //Assign Bottom Hud Number
        bottom_hud_item = 0;
        //Reset Button 2 
        button2_selected = false;
        button2_clock = 10;
        start_button2_clock = false;
        start_button2_clock_add = false;
        //Reset Button 3 - Spiritual Weapons
        button3_selected = false;
        button3_clock = 10;
        start_button3_clock = false;
        start_button3_clock_add = false;
        //Reset Button 4 - Special Items, Traps
        button4_selected = false;
        button4_clock = 10;
        start_button4_clock = false;
        start_button4_clock_add = false;
        //Reset Button 5 - Clothing
        button5_selected = false;
        button5_clock = 10;
        start_button5_clock = false;
        start_button5_clock_add = false;
        //Start Equip Clock
        start_button1_clock = true;
    }
    //Deselect Button
    if (button1_clock <= 0) && (button1_selected) {
        button1_selected = false;
        start_button1_clock_add = true;
    }    
}
    //---Start Equip Clock
if (start_button1_clock) {
    button1_clock -=1;   
}
if (button1_clock <= 0) {
    button1_clock = 0;
    start_button1_clock = false;
}
    //---Start Equip Clock Add
if (start_button1_clock_add) {
    button1_clock +=1;   
}
if (button1_clock >= 10) {
    button1_clock = 10;
    start_button1_clock_add = false;
}
[omitted code for rest of buttons, as it is the same] 

//GUN - Cycle Weapons And Set Gun Button Image
#region
if global.gun == 0 then gun_image = spr_gun0_button
if global.gun == 1 then gun_image = spr_gun1_button
if global.gun == 2 then gun_image = spr_gun2_button
if global.gun == 3 then gun_image = spr_gun3_button
if global.gun == 4 then gun_image = spr_gun4_button
if global.gun == 5 then gun_image = spr_gun5_button
if global.gun == 6 then gun_image = spr_gun6_button //empty image
    //---Scroll Left
if (button2_selected) {
    if (keyboard_check_pressed(ord("Q"))) {
        global.gun -= 1
        audio_play_sound(snd_click,10,false);
    }
    //---Scroll Right
    if (keyboard_check_pressed(ord("E"))) {
        global.gun += 1
        audio_play_sound(snd_click,10,false);
     }
    //---Cycle to Beginning
    //No Gun
    if (!global.gun1_green) && (!global.gun2_green) && (!global.gun3_green)
    && (!global.gun1_red) && (!global.gun2_red) && (!global.gun3_red) {
        global.gun = 6;
        
    }
    //Gun1 Green Only
    if (global.gun1_green) && (!global.gun2_green) && (!global.gun3_green)
    && (!global.gun1_red) && (!global.gun2_red) && (!global.gun3_red) {
         global.gun = 0;
    }
    //Gun1 Green and Gun2 Green
    if (global.gun1_green) && (global.gun2_green) && (!global.gun3_green)
    && (!global.gun1_red) && (!global.gun2_red) && (!global.gun3_red) {
        if (global.gun > 1) then global.gun = 0;
        if (global.gun < 0) then global.gun = 1;
    }
    //Gun1 Green, Gun2 Green, and Gun3 Green
    if (global.gun1_green) && (global.gun2_green) && (global.gun3_green)
    && (!global.gun1_red) && (!global.gun2_red) && (!global.gun3_red) {
        if (global.gun > 2) then global.gun = 0;
        if (global.gun < 0) then global.gun = 2;
    }
    //Gun1 Green, Gun2 Green, Gun3 Green, Gun1 Red
    if (global.gun1_green) && (global.gun2_green) && (global.gun3_green)
    && (global.gun1_red) && (!global.gun2_red) && (!global.gun3_red) {
        if (global.gun > 3) then global.gun = 0;
        if (global.gun < 0) then global.gun = 3;
    }
    //Gun1 Green, Gun2 Green, Gun3 Green, Gun1 Red, Gun2 Red
    if (global.gun1_green) && (global.gun2_green) && (global.gun3_green)
    && (global.gun1_red) && (global.gun2_red) && (!global.gun3_red) {
        if (global.gun > 4) then global.gun = 0;
        if (global.gun < 0) then global.gun = 4;
    }
    //Gun1 Green, Gun2 Green, Gun3 Green, Gun1 Red, Gun2 Red, Gun3 Red
    if (global.gun1_green) && (global.gun2_green) && (global.gun3_green)
    && (global.gun1_red) && (global.gun2_red) && (global.gun3_red) {
        if (global.gun > 5) then global.gun = 0;
        if (global.gun < 0) then global.gun = 5;
    }
}
#endregion
//Update Top HUD Object - Inventory Menu (this is main menu where you can also select/deselect guns)
#region
//---Gun 0
if (global.gun == 0) {  
    //Change Select Variable To True
    global.gun1_green_selected = true;
    //Reset Gun2 Green
    global.gun2_green_selected = false;
    with(obj_hud_top) {
        gun2_green_clock = 10;
        start_gun2_green_equip_clock = false;
        start_gun2_green_equip_clock_add = false;
    }
    //Reset Gun3 Green
    global.gun3_green_selected = false;
    with(obj_hud_top) {
    gun3_green_clock = 10;
    start_gun3_green_equip_clock = false;
    start_gun3_green_equip_clock_add = false;
    }
    //Reset Gun1 Red
    global.gun1_red_selected = false;
    with(obj_hud_top) {
    gun1_red_clock = 10;
    start_gun1_red_equip_clock = false;
    start_gun1_red_equip_clock_add = false;
    }
    //Reset Gun2 Red
    global.gun2_red_selected = false;
    with(obj_hud_top) {
    gun2_red_clock = 10;
    start_gun2_red_equip_clock = false;
    start_gun2_red_equip_clock_add = false;
    }
    //Reset Gun3 Red
    global.gun3_red_selected = false;
    with(obj_hud_top) {
    gun3_red_clock = 10;
    start_gun3_red_equip_clock = false;
    start_gun3_red_equip_clock_add = false;
    }
    
}
 

Rob

Member
I STRONGLY suggest you take some time to become comfortable with data structures - at least ds_lists. Arrays are cool and all, but they're not dynamic like lists are, so you'll have to do extra work with arrays (for some things) whereas a list does it for you (and there are many functions specifically for lists in gamemaker).

A ds_list is dynamic, in that you can have all your weapons added to the list, but in a different order.

Eg your list could look like this:

GML:
list[| 0] = 1;
list[| 1] = 3;
list[| 2] = 0;
list[| 3] = 2;
Weapon 1 would have been picked up first, then 3, 0, and finally 2.

I would also recommend using enumerators so that it's instantly knowable what 0,1,2,3 are.

I promise you that the time you invest in learning how to use a list will mean a lot less code (only 1 block of code for ALL weapons if you do it right, not 1 block for EACH) and a lot more options open up for you. If you need more help feel free to pm me.

[EDIT] I'm kind of biased towards lists obviously, you can achieve what you want with an array but arrays and lists seem like very similar things and lists come with some added benefits.
 

T2008

Member
Thanks for the response! Do you have a tutorial to recommend regarding weapons? A lot of what's out there relates to farming type stuff and I'm not doing a farming game.
 

Rob

Member
Thanks for the response! Do you have a tutorial to recommend regarding weapons? A lot of what's out there relates to farming type stuff and I'm not doing a farming game.
For me, a "weapon" is just a number, like 0, 3, 45. What you want to know is "How do I manage my different 'weapons' (numbers) so that I can actually make the game that I want. It doesn't matter what you want to do in your game if you understand data structures/arrays and how to use them.

For an introduction to lists try this video:

For "how to make an inventory using an array" try this video:
 
Top