Weapon Drop/Pickup not working

TheOnlyWRT

Member
So i am trying to code in my game the ability to pickup weapons when the enemy dies and drops their weapon. It worked great, and still does, but then i tried to add the ability of be able to drop your weapon whenever, and also drop your current weapon when you pickup a new one. And now whatever weapon is the first that i pickup, that is the weapon that all other dropped/picked up weapons are.

here is the code in the enemies to drop a weapon:
Code:
var shouldDrop = choose(3); //choose a number to see if the enemt should drop a weapon or not
    if(shouldDrop == 3){
    
        var setType = currentWeapon;
        with instance_create_depth(x, y, 1, objectWeaponDrop){
            
            type = setType; //set the type of the weapon being dropped
            ammo = irandom_range(1, weapon[type, v_magazineCapacity]);
            magazines = irandom_range(1, 3);
            
        }   
    }
Here is the code for the player dropping a weapon:
Code:
if(keyboard_check_pressed(ord("D"))){

    if(currentWeapon != w_punch){
    
        var weaponSet = currentWeapon;
        var ammoSet = weapon[currentWeapon, v_ammo];
        var magazineSet = weapon[currentWeapon, v_magazines];
    
        with instance_create_depth(x, y, 1, objectWeaponDrop){
    
            type = weaponSet;
            ammo = ammoSet;
            magaine = magazineSet;
    
        }
    
        currentWeapon = w_punch;
    
    }

}
and here is the code for the player picking up a weapon (in the collision with objectWeaponDrop event)
Code:
if(keyboard_check_pressed(ord("F"))){

    if(currentWeapon == objectWeaponDrop.type){

        currentWeapon = objectWeaponDrop.type;
        weapon[currentWeapon, v_ammo] = weapon[currentWeapon, v_ammo] + objectWeaponDrop.ammo;
        weapon[currentWeapon, v_magazines] = weapon[currentWeapon, v_magazines] + objectWeaponDrop.magazines;

    } else {

        
        if(currentWeapon != w_punch){
    
            var weaponSet = currentWeapon;
            var ammoSet = weapon[currentWeapon, v_ammo];
            var magazineSet = weapon[currentWeapon, v_magazines];
        
            with instance_create_depth(x, y, 1, objectWeaponDrop){
    
                type = weaponSet;
                ammo = ammoSet;
                magaine = magazineSet;
    
            }
    
        }
        

        currentWeapon = objectWeaponDrop.type;
        weapon[currentWeapon, v_ammo] = objectWeaponDrop.ammo;
        weapon[currentWeapon, v_magazines] = objectWeaponDrop.magazines;

    }   

}
So basically its sets the picked up weapon (and ammo and magazines) to the last used weapons values.... and if i used a pistol, then picked up a machine gun, it is still a pistol.... so its a mess now.....

Thanks for your help!!!!
 
G

Guest User

Guest
well this is a bit of a shot in the dark but it might just be a mixup when using the 'with()' and 'objectWeaponDrop.variable' parts. perhaps try doing it the "other way" using the instance ids of 'objectWeaponDrop' and see if that works?

also, if i'm not mistaken, that 'choose(3)' should be 'irandom(3)' because otherwise it's just going to choose '3' every single time, and thus drop a weapon every single time.
Enemy Weapon Drop
Code:
// PART: Weapon Drop Chance
if(irandom(3) == 3) {
    // PART: Drop Weapon
    var _type = currentWeapon;
    var _inst = instance_create_depth(x, y, 1, objectWeaponDrop);
        _inst.type = _type;
        _inst.ammo = irandom_range(1, weapon[_type, v_magazineCapacity]);
        _inst.magazine = irandom_range(1, 3); }
Player Weapon Drop
Code:
// PART: Check If Weapon Equipped
if(keyboard_check_pressed(ord("D")) && currentWeapon != w_punch) {
    // PART: Drop Weapon
    var _type = currentWeapon;
    var _inst = instance_create_depth(x, y, 1, objectWeaponDrop);
        _inst.type = currentWeapon;
        _inst.ammo = weapon[_type, v_ammo];
        _inst.magazine = weapon[_type, v_magazine];
    currentWeapon = w_punch; }
Player Weapon Pickup
Code:
// PART: Check For Collision With 'objectWeaponDrop'
if(keyboard_check_pressed(ord("F"))) {
    var _inst = instance_place(x, y, objectWeaponDrop);
    if(_inst != noone) {
       // PART: Add Weapon
        if(currentWeapon == _inst.type) {
            weapon[currentWeapon, v_ammo] += _inst.ammo;
            weapon[currentWeapon, v_magazines] += _inst.magazines; } }
          
        // PART: Switch Weapon
        else {
           // PART: Drop Old Weapon
            if(current_weapon != w_punch) {
                var _type = currentWeapon;
                var _drop = instance_create_depth(x, y, 1, objectWeaponDrop);
                    _drop.type = _type;
                    _drop.ammo = weapon[_type, v_ammo];
                    _drop.magazine = weapon[_type, v_magazine]; }
                  
            // PART: Pickup New Weapon
            currentWeapon = _inst.type;
            weapon[currentWeapon, v_ammo] = _inst.ammo;
            weapon[currentWeapon, v_magazine] = _inst.magazine; } }
 

TheOnlyWRT

Member
yeah, this is kind of a confusing post..... let me know if you need more details. ill test thenew way and see if that helps!!! and yeah, its on 3 for now so that it drops a weapon every time (for testing purposes); usually its 1 through 5.
 

TheOnlyWRT

Member
so i just tried the code, and when i press the "F" key to pick up a weapon, it doesnt pick it up...
 
Last edited:

TheOnlyWRT

Member
UPDATE: I removed one of the brackets and put it at the end, and then edited a few variable names that didnt match, and now it is working except when i drop a weapon, the number of magazines is set back to 0..... heres the new code

Player drop:
Code:
// PART: Check If Weapon Equipped
if(keyboard_check_pressed(ord("D")) && currentWeapon != w_punch) {
    // PART: Drop Weapon
    var _type = currentWeapon;
    var _inst = instance_create_depth(x, y, 1, objectWeaponDrop);
    _inst.type = currentWeapon;
    _inst.ammo = weapon[_type, v_ammo];
    _inst.magazines = weapon[_type, v_magazines];
    currentWeapon = w_punch;
}
and player pick up:
Code:
// PART: Check For Collision With 'objectWeaponDrop'
if(keyboard_check_pressed(ord("F"))){

    //see if there is a weapon to be picked up
    var _inst = instance_place(x, y, objectWeaponDrop);
    
    if(_inst != noone){
      
        //if the dropped weapon is the same as the equipped one
        if(currentWeapon == _inst.type) { //if the dropped weapon is the same as the equipped
        
            weapon[currentWeapon, v_ammo] += _inst.ammo; //add to the ammo
            weapon[currentWeapon, v_magazines] += _inst.magazines; //add to the magazines
        }
    //}
          
        // not the same, drop the old weapon and pick up the new one
        else {
        
           //Drop Old Weapon
            if(currentWeapon != w_punch) { //make sure we have a weapon
                var _type = currentWeapon; //get the current type
                var _drop = instance_create_depth(x, y, 1, objectWeaponDrop); //create a new dropped weapon and store its id in the variable
                    _drop.type = _type;
                    _drop.ammo = weapon[_type, v_ammo];
                    _drop.magazines = weapon[_type, v_magazines];
            }
                  
            // PART: Pickup New Weapon
            currentWeapon = _inst.type;
            weapon[currentWeapon, v_ammo] = _inst.ammo;
            weapon[currentWeapon, v_magazines] = _inst.magazines;
            
        }
    }
}
i added comments to help me understand it better. Thanks for the help!
 
Last edited:
G

Guest User

Guest
ok i'm glad you figured it out.
here's an explanation of every part of that last bit if you still need it. obv does not include your corrections though so keep that in mind:
Code:
var _inst = instance_place(x, y, objectWeaponDrop);
   if(_inst != noone) {
this checks to see if an instance of 'objectWeaponDrop' exists at point (x, y), the coordinates of the player. if an instance is not found, it returns the value 'noone' so we have to check that an instance was actually found before carrying on otherwise the code will try to use 'noone's values, but 'noone' doesn't exist so we'll get an error.
Code:
      // PART: Add Weapon
       if(currentWeapon == _inst.type) {
           weapon[currentWeapon, v_ammo] += _inst.ammo;
           weapon[currentWeapon, v_magazines] += _inst.magazines; } }
this checks if the current weapon is the same as the weapon held by 'objectWeaponDrop'. if it is, then we add the values 'ammo' and 'magazine' stored in 'objectWeaponDrop' to the player's weapon because they're the same.
Code:
       // PART: Switch Weapon
       else {
          // PART: Drop Old Weapon
           if(current_weapon != w_punch) {
               var _type = currentWeapon;
               var _drop = instance_create_depth(x, y, 1, objectWeaponDrop);
                   _drop.type = _type;
                   _drop.ammo = weapon[_type, v_ammo];
                   _drop.magazine = weapon[_type, v_magazine]; }
if the weapon held by the player is NOT the same as the weapon held by 'objectWeaponDrop', however, we must drop the current weapon. so this just creates that weapon on the floor as if it were dropped.
Code:
           // PART: Pickup New Weapon
           currentWeapon = _inst.type;
           weapon[currentWeapon, v_ammo] = _inst.ammo;
           weapon[currentWeapon, v_magazine] = _inst.magazine; } }
now this sets the current weapon, ammo, and magazine to what 'objectWeaponDrop' has because we've now picked it up and it's ours.
 

TheOnlyWRT

Member
so the code works perfectly, except for when i drop a weapon. When i drop it, the ammo says the same, but the magazines gets reset to 0..... looking at the code (the same that i posted above) everything seems ok.
 

TheOnlyWRT

Member
UPDATE: i just realized that there was a piece of uncommented code that was messing with the dropping of a weapon, so now it is working just fine!
 
Top