• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Windows Help with weapons on my game Dungeon Crawler


Hi guys, I was here programming a Dungeon Crawler style game and I'm having some doubts about how to do certain functions for my weapons in the game.

So far I only have the pistol and a machine gun, my player when he gets to any of the weapons, he takes it and can shoot, then he reaches the other weapon that is on the ground and takes it and changes the weapon he just picked up, however, he he doesn’t switch weapons when the two are already caught, he doesn’t switch from pistol to machine gun or machine gun to pistol, besides my bullets are infinite and I just want the pistol bullets to be infinite.

in short, what I want is for my character to switch from one weapon to another and for the pistol to have infinite bullets and for the machine gun, limited bullets.

Create event of "oPistol" :
///@desc Variaveis

weapons[0] = ds_map_create();
ds_map_add(weapons[0], "sprite", -1);
ds_map_add(weapons[0], "recoil", 0);
ds_map_add(weapons[0], "recoil_push", 0);
ds_map_add(weapons[0], "damage", 0);
ds_map_add(weapons[0], "projectile", -1);
ds_map_add(weapons[0], "startup", 0);
ds_map_add(weapons[0], "length", 0);
ds_map_add(weapons[0], "cooldown", 0);
ds_map_add(weapons[0], "bulletspeed", 0);
ds_map_add(weapons[0], "automatic", false);

weapons[1] = ds_map_create();
ds_map_add(weapons[1], "sprite", sPistola);
ds_map_add(weapons[1], "recoil", 3);
ds_map_add(weapons[1], "recoil_push", 0);
ds_map_add(weapons[1], "damage", 1);
ds_map_add(weapons[1], "projectile", oBullet);
ds_map_add(weapons[1], "startup", 0);
ds_map_add(weapons[1], "length", 24);
ds_map_add(weapons[1], "cooldown", 3);
ds_map_add(weapons[1], "bulletspeed", 3);
ds_map_add(weapons[1], "automatic", false);

//SubMachine fodase

weapons[2] = ds_map_create();
ds_map_add(weapons[2], "sprite", sMetralhadora);
ds_map_add(weapons[2], "recoil", 6);
ds_map_add(weapons[2], "recoil_push", 2);
ds_map_add(weapons[2], "damage", 3);
ds_map_add(weapons[2], "projectile", oBullet);
ds_map_add(weapons[2], "startup", 0);
ds_map_add(weapons[2], "length", 24);
ds_map_add(weapons[2], "cooldown", 10);
ds_map_add(weapons[2], "bulletspeed", 16);
ds_map_add(weapons[2], "automatic", true);

weapon = 0;
ammo[array_length_1d(weapons)-1] = 0;
ammo[0] = -1;


current_cd = 0;
current_delay = -1;
current_recoil = 0;

My step event from "oPistol" :

x = oPlayer.x;
y = oPlayer.y;

direction = point_direction(x,y,mouse_x,mouse_y);

if (direction > 90) && (direction < 270) image_yscale =  -1; else image_yscale = 1;

image_angle = direction;

var mouseb;
if (automatic) mouseb = mouse_check_button(mb_left); else mouseb = mouse_check_button_pressed(mb_left);

if (mouseb)
    if (current_cd == 0)
        current_cd = cooldown;
        current_delay = startup;
        hSPD -= lengthdir_x(other.recoil_push,other.direction);
        vSPD -= lengthdir_y(other.recoil_push,other.direction);
        current_recoil = recoil;

if (current_delay == 0) && (projectile != -1) && (weapon != 0)
    if (ammo[weapon] != 0)
        with (instance_create_layer(x , y, "Tiros",projectile))
            direction = other.direction;
            speed = other.bulletspeed;
        ammo[weapon] -= 1;
current_delay = max(-1,current_delay-1);
if (current_delay == -1) current_cd = max(0,current_cd-1);
current_recoil = max(0,floor(current_recoil*0.8));

depth = oPlayer.depth-1;
My draw event from "oPistol":
if (sprite != -1)
my script "ChangeWeapon":

function ChangeWeapon(){
    weapon = argument0;
var wp_map = weapons[weapon];
sprite = ds_map_find_value(wp_map,"sprite");
recoil = wp_map[? "recoil"];
recoil_push = wp_map[? "recoil_push"];
damage = wp_map[? "damage"];
projectile = wp_map[? "projectile"];
startup = wp_map[? "startup"];
bulletspeed = wp_map[? "bulletspeed"];
length = wp_map[? "length"];
cooldown = wp_map[? "cooldown"];
automatic = wp_map[? "automatic"];

and for least my "oWeapon_pickup"


the tutorial i follow to do this is from Shaun Spalding:

sorry if my english is bad,im learning how to speak this language


Hi, this is a lot of code, but it seems like the most important part to fix the weapon switching would be the oPlayer event for oWeaponPickup. What does the yellow warning in that event say? I would make sure that there is nothing wrong there. As for the infinite bullets on the machine gun, can you include code from the machine gun? Specifically the part that is supposed to limit the ammo of the gun. Sorry that I can't exactly pinpoint the problem, this is just a lot of code to go over, it might be helpful if you could condense it to just the most important parts. Oh, and your English looks great :)


first of all the yellow warning is saying that "Number of arguments for function ChangeWeapong expected 0 got 1"
the code from machine gun is the same of "oPistol" cause submachine gun is a parent of "oWeapon_pickup")
i gonna say every code from "oWeapon_pickup"

when collide with player:
with (oPistol)
     ammo[weapon] += 20;
create event:

depth = -bbox_bottom;

//wave effect to weapons on the ground

frequency = 0.1;
amplitude = 0.1;
timer = 0;
step event

y = y + sin(timer*frequency)*amplitude;
timer ++;
draw event

i'm a little while thinking about how to limit the bullets only to the machine gun, but i don't think there is a way to do it with the logic i did


Hi, sorry I had to step away from my computer for a sec. First, arguments expected 0 got 1 sounds like an important thing to fix, I think your script is not functioning properly. For some reason it's expecting zero arguments when you are trying to pass it one? I think that would explain the weapons not switching... I would make sure you are declaring the argument correctly? It seems like you are, but it also seems like gamemaker is not recognizing your argument, I don't know why. Second, if the code from the machine gun is the same as the pistol and they are linked through parents, then wouldn't that make sense that you can't have them do different things at the same time? I don't know if I'm missing something, but you need to make sure that your child/parent trees aren't making it so that your machine gun and pistol have to have the same code.


so... the arguments i didn't fix cause i can't find a way to fix,and the ammo,i fixed yet,the only thing that is missing is that if I press any key, for example "Q", and I have both weapons on, he can switch between them, if I have the pistol collected and the machine gun is not showing, when I pressing the "Q" can go to machine gun, but I don't know how to write this code, I tried a few times but I don't know where to put it and how to program it better

like if i have "oPistol" and press "Q" go to "oSubMachine"


Hi, so there's a couple of ways to do that. First, you can use the keyboard pressed event, but I prefer not to do this because I like to have my code in one place. Another way, (the one I prefer) is to just use the step event and use the keyboard_check_pressed(ord("Q"))
I don't know exactly how your code works, but if I understand correctly, the way you would do this is
if (keyboard_check_pressed(ord("Q")))
Hope this helps! :)


i already tried this but didnt work,the game give a error,the character dont change of weapon,looks like i have to make another weapon code to do weapon change 😞