Weapon select

T

The Creator

Guest
Okay, so in my game, I need to make a weapon select system but I don't know how to implement it. Each weapon is set to true or false, and I used to have it so that if the gun was true, and the weapon ID was a certain number the weapon would be selected. But then I thought what if they received the weapons in different orders so I was stuck there. I tried to make a combination system so if certain guns were true and if the ID was a certain number the weapon would be true. That didn't work and ended up ruining my game. I dunno, I may just be thinking it over way too hard. Do you guys have any ideas?
 

3dgeminis

Member
Uses a 2D array. Every time you get a new weapon you add it to the array.
In Contra Hard Corps, the position of each weapon is already defined, when you grab it it appears where it should.

Is that how you want to do it?
 
Friends,

In the case of the video example above, how would you go about changing the weapons with the mouse scroll?

I'm testing something like this, but the result is not achieved.

Mouse Wheel UP
Code:
var ammor = array_length_1d(weapons)-1

if (ammor < 0)
{
    ChangeWeapon(ammor++)
}
Mouse Wheel Down
Code:
var ammor = array_length_1d(weapons)-1
if (ammor > 0)
{
    ChangeWeapon(ammor--)
   
}
else
{
    ChangeWeapon(array_length_1d(weapons)-1)
}
 

Slyddar

Member
In the case of the video example above, how would you go about changing the weapons with the mouse scroll?
I've actually done that tutorial a while ago, and added scroll wheel changing. I'm sure there is other ways, but I just modified the declaration by adding a total variable and a num variable to each one. Total was incremented for each weapon, and declared as -1 initially.
Code:
//pistol
total++;
weapons[total] = ds_map_create();
ds_map_add(weapons[total],"name","Pistol");                    //weapon name
ds_map_add(weapons[total],"num",total);                        //the number of this weapon
So total always gave me how many weapons and num was the one we were on.
To change weapons you can then grab the num variable of the current weapon and use it to change to the next or previous, if possible, as you need to adjust for when num = total or num = 0.

Hope that helps.
 
friend helps and lots of it. I'll implement here.

Another question.

using that same tutorial, say that I want to show in my GUI / HUD of the game which weapon is the current one and for example the amount of bullets that I am at the moment.

I have my code this information in an array, and now changing to that ds_map I'm still lost.

I used to.
Code:
if (instance_exists(oGun))
    {
        //draw HUD weapon BG
        draw_sprite_ext(sBottonWeapon,1,RES_W,RES_H,0.7,0.7,0,-1,1);
        //draw HUD weapon
        draw_sprite_ext(global.weaponArray[global.weapon,15],1,RES_W-440,RES_H-75,0.6,0.6,0,-1,1);
   }
 

Slyddar

Member
To see the bullets you should be able to just check the ammo array for the "weapon" variable, in o_weapon, if you are using a "weapon" variable to store what current weapon you are using. If using my num method above, you would need to extract that variable first.
Code:
with(o_weapon) {
  var bullets = ammo[weapon];
}
 
Last edited:
To see the bullets you should be able to just check the ammo array for the "weapon" variable, in o_weapon, if you are using a "weapon" variable to store what current weapon you are using. If using my num method above, you would need to extract that variable first.
Code:
with(o_weapon) {
  var bullets = ammo[weapon];
}
Thanks, it works perfect.

I've actually done that tutorial a while ago, and added scroll wheel changing. I'm sure there is other ways, but I just modified the declaration by adding a total variable and a num variable to each one. Total was incremented for each weapon, and declared as -1 initially.
Code:
//pistol
total++;
weapons[total] = ds_map_create();
ds_map_add(weapons[total],"name","Pistol");                    //weapon name
ds_map_add(weapons[total],"num",total);                        //the number of this weapon
So total always gave me how many weapons and num was the one we were on.
To change weapons you can then grab the num variable of the current weapon and use it to change to the next or previous, if possible, as you need to adjust for when num = total or num = 0.

Hope that helps.
I understand the operation, but my logic does not seem correct, I feel a slow.

UP
Code:
if (num < total)
{
    ChangeWeapon(num++);
}
else
{
    ChangeWeapon(1);
}
DOWN
Code:
if (num > 1)
{
    ChangeWeapon(num--);
}
else
{
    ChangeWeapon(total);
}
my create
Code:
total = -1;
total++;
weapons[total] = ds_map_create();
ds_map_add(weapons[total],"name","name");
ds_map_add(weapons[total],"num",total);

//Resolver 38
total++;
weapons[total] = ds_map_create();
ds_map_add(weapons[total],"name","Revolver 38");
ds_map_add(weapons[total],"num",total);

//Revolver 38 Short
total++;
weapons[total] = ds_map_create();
ds_map_add(weapons[total],"name","Revolver 38 Short");
ds_map_add(weapons[total],"num",total)
 
Last edited:

Slyddar

Member
Code:
total = -1;
total++;
weapons[total] = ds_map_create();
I was doing other things with total beforehand, hence the -1 declaration. In your situation though, for the first entry, you can just do this and save a line :)
Code:
total = 0;
weapons[total] = ds_map_create();
Been a while since I had visited the code. Shaun uses the variable "weapon" to dictate the current one he is on, and he updates it when changing weapons. I used num as a ds_map entry for other reasons. Probably easier for you to just use his "weapon" variable to change, as long as your ChangeWeapon script updates the variable each time you pickup, or you do it when switching.

I see you already have added a weapons array, global.weaponArray and the weapon variable, global.weapon, which helps. If mousing up, you would need to look at your array and see which position the current global.weapon is, then if the array allows it, i.e there is space, do a global.weapon++ and run ChangeWeapon(global.weapon), and if no space is there, set global.weapon to 0 and run ChangeWeapon(0).

I can't be specific cause I don't know how you have set your array. You can either have it add a new entry per weapon pickup, or have each slot allocated beforehand, and just a flag to say if it's been picked up. Both require different solutions, as the position of the weapon is not associated with the global.weapon variable, and you would have to search the array for the variable, then move to the next position based on that finding.
 

Slyddar

Member
Went back to my old code from doing the tutorial and added weapon switching with the mouse. As mentioned, you need to know how you are setting up your weapons. For me I added a ds_grid that was as wide as all the weapons I have available, and when the player picks one up, the picked up weapons number is allocated to the next free space. Each weapon can only be picked up once (as new), so eventually we fill the whole grid. We can then use that to know which weapon to pick next when cycling up or down. I also added a mouse_wheel_delay variable, so you can cycle up/down fast on the wheel, and only 1 weapon will change, otherwise it's hard to move just one weapon.

My first weapon in the weapons ds_map was just called fists, and I used that as the first current_weapon. I'm sure you can use some of this to do what you need. Also ensure you destroy the grid in a cleanup event.

Player Create
Code:
//total possible weapons
global.max_weapons = 4;

//how many of the possible weapons have been picked up.  We start with fists.
global.found_weapons = 1;

global.weaponArray = ds_grid_create(global.max_weapons, 2);
// 0 - weapon number
// 1 - ammo (I haven't added the code for this, but it's here for you to use)

//set grid to -1 except for weapon 0, fists which is 0, 0.
ds_grid_set_region(global.weaponArray, 1, 0, ds_grid_width(global.weaponArray), 2, -1);

//current weapon being used
current_weapon = 0;

//scroll delay
mouse_wheel_delay_initial = 15;
mouse_wheel_delay = 0;

Player Step
Code:
if (mouse_wheel_up() or mouse_wheel_down()) and mouse_wheel_delay <= 0 {
    mouse_wheel_delay = mouse_wheel_delay_initial;
    if mouse_wheel_up() var _move = 1 else var _move = -1;
    //have we found any weapons yet?
    if global.found_weapons != 1 {
        //get current weapons x position in grid
        var x_pos = ds_grid_value_x(global.weaponArray, 0, 0, ds_grid_width(global.weaponArray), 0, current_weapon);
        var new_x_pos, new_wpn, _size = ds_grid_width(global.weaponArray);
        do {
            switch _move {
            case -1: //down
                //get new x position
                if x_pos = 0 {
                    //x_pos is currently at the start of the grid, so wrap to end
                    x_pos = _size - 1;
                } else {
                    x_pos--;
                }
            break;
            case 1: //up
                //get new x position
                if x_pos = _size - 1 {
                    //x_pos is currently at the end of the grid, so wrap to start
                    x_pos = 0;
                } else {
                    x_pos++;
                }
            break;
            }
            //get weapon number
            new_wpn =  global.weaponArray[# x_pos, 0];
        } until new_wpn != -1;    //meaning we have found a spot with a weapon in it
 
        //change weapon
        with(o_weapon) change_weapon(new_wpn);
 
        //update player
        current_weapon = new_wpn;
    }
} else mouse_wheel_delay--;

A few other options for managing what is picked up. You can use a dynamic grid, and just resize it each time you pick up a weapon instead. Alternatively, you can use a ds_list for the weapons picked up, which is easier to manage, as you can simply move up and down the list, and not worry which one has been picked up. You would then need another list to hold the ammo for each weapon as well though.

Hopefully out of all that you can find a solution that suits.
 
Last edited:
Hello Friend.

Sorry for taking your time.

My Create Player looks like this:

Code:
//total possible weapons
global.max_weapons = 3;

//how many of the possible weapons have been picked up.  We start with fists.
global.found_weapons = 1;

global.weaponArray = ds_grid_create(global.max_weapons, 2);
// 0 - weapon number
// 1 - ammo (I haven't added the code for this, but it's here for you to use)

//set grid to -1 except for weapon 0, fists which is 0, 0.
ds_grid_set_region(global.weaponArray, 1, 0, ds_grid_width(global.weaponArray), 2, -1);

//current weapon being used
current_weapon = 0;

//scroll delay
mouse_wheel_delay_initial = 15;
mouse_wheel_delay = 0;

Step Player
Code:
//Weapons
if (mouse_wheel_up() or mouse_wheel_down()) and mouse_wheel_delay <= 0 {
    mouse_wheel_delay = mouse_wheel_delay_initial;
    if mouse_wheel_up() var _move = 1 else var _move = -1;
    //have we found any weapons yet?
    if global.found_weapons != 1 {
        //get current weapons x position in grid
        var x_pos = ds_grid_value_x(global.weaponArray, 0, 0, ds_grid_width(global.weaponArray), 0, current_weapon);
        var new_x_pos, new_wpn, _size = ds_grid_width(global.weaponArray);
        do {
            switch _move {
            case -1: //down
                //get new x position
                if x_pos = 0 {
                    //x_pos is currently at the start of the grid, so wrap to end
                    x_pos = _size - 1;
                } else {
                    x_pos--;
                }
            break;
            case 1: //up
                //get new x position
                if x_pos = _size - 1 {
                    //x_pos is currently at the end of the grid, so wrap to start
                    x_pos = 0;
                } else {
                    x_pos++;
                }
            break;
            }
            //get weapon number
            new_wpn =  global.weaponArray[# x_pos, 0];
        } until new_wpn != -1;    //meaning we have found a spot with a weapon in it


        show_debug_message("Move 1 " + string(_move));
        show_debug_message("New " + string(new_wpn));

        //change weapon
        with(oWeapon) ChangeWeapon(new_wpn);

        //update player
        current_weapon = new_wpn;
    }
} else mouse_wheel_delay--;

In the Pickup Object, I increased the global.found_weapons
Code:
with (oWeapon)
{
    ChangeWeapon(other.weapon);
    ammo[weapon] += 1000;
}
global.found_weapons++;
instance_destroy();

I start the game with no Weapons and global.found_weapons is 0. Ok.

When I take a weapon it changes to 1. When I take the second weapon it changes to 2.

But by moving the mouse scroll, A Weapon disappears.

I debugged and the value of the temporary variable new_wpn is not getting the increment from the position that we spin the scroll of the Mouse.

the _move variable changes between 1 and -1 correctly.
 

Slyddar

Member
In the pickup object collision ensure you are updating players current_weapon.
Mine looks like this:

Code:
if global.found_weapons < global.max_weapons {
    other.current_weapon = weapon;
    with (o_weapon)
    {
        change_weapon(other.weapon);   
        ammo[weapon] += 20;
        global.weaponArray[# global.found_weapons, 0] = weapon;        //assign the weapon number to next slot
        global.weaponArray[# global.found_weapons, 1] = ammo[weapon];    //assign ammo
        global.found_weapons++;
    }
    instance_destroy();
}
 
Sorry, forgot to post translated.

The concept DS LIST for me is different from the Array I was using, lol.

I need to study this code more.

Now it works perfectly for me.

I want to make a Shop to buy bullet / Weapons
 
Last edited:
I would stay again.

I'm calling a "Store" object, it creates another 3 ItemStore objects in different screen positions.

Within the Loop Item I'm drawing in GUI Draw using with (oWeapon). But the values are not shown.

What I want to do is this.

Create obj_store
Code:
button1 = instance_create_depth(xx + ww / 2 - 300, yy + hh /2 + 130,-100,oStoreBuyButton);
button1.array = 0;

button2 = instance_create_depth(xx + ww / 2, yy + hh /2 + 130,-100,oStoreBuyButton);
button2.array = 1;

button3 = instance_create_depth(xx + ww / 2 + 300, yy + hh /2 + 130,-100,oStoreBuyButton);
button3.array = 2;

DRAW GUI - oStoreBuyButton
Code:
with(oWeapon){
 
        //draw HUB weapon BG
        draw_sprite_ext(sBottonWeapon,1,RES_W,RES_H,0.7,0.7,0,-1,1);
        //draw HUB weapon
        
        draw_sprite_ext(hud_sprite,1,RES_W-440,RES_H-75,0.6,0.6,0,-1,1);
    
        show_debug_message(string(global.found_weapons));
 
        //draw Name Weapon
        draw_set_font(fHud);
        draw_set_halign(fa_center);
        draw_set_valign(fa_center);
        DrawTextOutline(RES_W-210,RES_H-102,yd_lang(name),make_color_rgb(255,255,0),make_color_rgb(255,204,0),1, c_black,c_black,1,2,20,1,1,0);
        //draw current Ammo
        draw_set_halign(fa_right);
        DrawTextOutline(RES_W-100,RES_H-45,"x"+string(ammo[weapon]),make_color_rgb(255,255,0),make_color_rgb(255,204,0),1, c_black,c_black,1,2,20,1,1,0);
 
    }

The Store object will call 3x oStoreBuyButtont.

to each item it will load the Weapon information according to a global.max_weapons for.

If the global.max_weapons is equal to 6 in case I would have buttons to navigate between the Weapons in this Store Menu.

I do not know if I understand.

The result should be more or less that.
 
Hi.

Create oStore
Code:
//create variable
global.pages = 1;

global.item1 = 1
global.item2 = 2
global.item3 = 3

show_debug_message(global.item1);
show_debug_message(global.item2);
show_debug_message(global.item3);

//create 3 buttons in array
button1 = instance_create_depth(xx + ww / 2 - 300, yy + hh /2 + 130,-100,oStoreBuyButton);
button1.array = global.item1;

button2 = instance_create_depth(xx + ww / 2, yy + hh /2 + 130,-100,oStoreBuyButton);
button2.array = global.item2;

button3 = instance_create_depth(xx + ww / 2 + 300, yy + hh /2 + 130,-100,oStoreBuyButton);
button3.array = global.item3;

//chama botão para fechar
instance_create_depth(xx + ww / 2 + 500, yy + hh/2 - 450,-200,oBtnExitStore);
//chama botão da direita
instance_create_depth(xx + ww / 2 + 500, yy + hh/2 - 50,-300,oBtnStoreRight);
//chama botão da esquerda
instance_create_depth(xx + ww / 2 - 500, yy + hh/2 - 50,-300,oBtnStoreLeft);
//marcador da pagina
instance_create_depth(xx + ww / 2, yy + hh/2 + 280,-300,oBpageMarq);

Event DRAW oStoreBuyButton

Code:
with(oWeapon){
// Set Temp variables
var wp_map = weapons[other.array];
var name = ds_map_find_value(wp_map,"name");
var store_sprite = ds_map_find_value(wp_map,"store_sprite");
var store_weapon_price_type_sprite = ds_map_find_value(wp_map,"store_weapon_price_type_sprite");
var store_spd_desc = ds_map_find_value(wp_map,"store_spd_desc");
var store_damage_desc = ds_map_find_value(wp_map,"store_damage_desc");
var store_bullets_price = ds_map_find_value(wp_map,"store_bullets_price");
var store_bullets_amount = ds_map_find_value(wp_map,"store_bullets_amount");

//Item Sprite Bullet Center
draw_sprite_ext(store_sprite,0,other.x, other.y -165,1,1,0,-1,1);
       
//Draw Text
draw_set_valign(fa_center);
draw_set_color(c_white);

//Draw Text Name
draw_set_font(fStoreSmall);
draw_set_halign(fa_center);
draw_set_valign(fa_center);
DrawTextOutline(other.x,
                other.y  -295,
                yd_lang(name),
                make_color_rgb(255,255,0),
                make_color_rgb(255,204,0),1,
                c_black,c_black,1,4,20,1,1,0);

//draw Tips Current Ammo
draw_sprite_ext(sStoreTips,0,other.x,other.y -80,0.8,0.8,0,-1,1);

//Button Price Sprite
draw_set_font(fStoreSmall);
draw_set_halign(fa_left);
draw_set_valign(fa_center);
draw_sprite_ext(store_weapon_price_type_sprite,0,other.x -55, other.y -5 ,0.7,0.7,0,-1,1);

//Draw Text Description
draw_set_font(fStoreVerySmall);
draw_set_halign(fa_center);
draw_set_valign(fa_center);
DrawTextOutline(other.x,
                other.y-70,
                yd_lang("store_speed")+ ": "
                + yd_lang(store_spd_desc)+ "\n"
                + yd_lang("store_damage") +": "
                + yd_lang(store_damage_desc),
                make_color_rgb(255,255,0),
                make_color_rgb(255,204,0),1, c_black,c_black,1,1,20,1,1,0);
was forgetting to use the Other.
 
Last edited:
Top