Weapon change system

skofrant

Member
But look i already swapped that before ... and still can't pick a non-primary weapon

ok below all the code for the weapon exchange system
with the corrections applied ... despite this, I still can't change my weapon ... to magma, even though I take it from the board ... she adds, because the sprte in gui is changing ...


obj_game_controller (perssistent)

create



GML:
enum e_wep { have,ammo,firerate,dmg_min,dmg_max,ammo_type }
enum e_gun   { basic,magma,shotgun,laser,crossbow }




current_weapon = 0;
weapons_total = 5;



var i = 0;


//primary weapon(bullet)

global.wep[0 ,e_wep.ammo_type] = obj_bullet;
global.wep[0, e_wep.have] = false;
global.wep[0 ,e_wep.ammo] = 100; //unlimited
global.wep[0 ,e_wep.firerate] = 5;
global.wep[0 ,e_wep.dmg_min] = 3;
global.wep[0 ,e_wep.dmg_max] = 5;


//magma weapon

global.wep[1 ,e_wep.ammo_type] = obj_magma;
global.wep[1, e_wep.have] = false;
global.wep[1 ,e_wep.ammo] = 30;
global.wep[1 ,e_wep.firerate] = 10;
global.wep[1 ,e_wep.dmg_min] = 3;
global.wep[1 ,e_wep.dmg_max] = 5;

//shotgun weapon

global.wep[2 ,e_wep.ammo_type] = obj_shotgun;
global.wep[2, e_wep.have] = false;
global.wep[2 ,e_wep.ammo] = 20;
global.wep[2 ,e_wep.firerate] = 15;
global.wep[2 ,e_wep.dmg_min] = 3;
global.wep[2 ,e_wep.dmg_max] = 5;


//laser weapon

global.wep[3 ,e_wep.ammo_type] = obj_laser;
global.wep[3, e_wep.have] = false;
global.wep[3 ,e_wep.ammo] = 10;
global.wep[3 ,e_wep.firerate] = 20;
global.wep[3 ,e_wep.dmg_min] = 3;
global.wep[3 ,e_wep.dmg_max] = 5;


//crossbow weapon

global.wep[4 ,e_wep.ammo_type] = obj_crossbow;
global.wep[4, e_wep.have] = false;
global.wep[4 ,e_wep.ammo] = 5;
global.wep[4 ,e_wep.firerate] = 25;
global.wep[4 ,e_wep.dmg_min] = 3;
global.wep[4 ,e_wep.dmg_max] = 5;

obj_game_controller (perssistent)

step


GML:
for(var i=0;i<weapons_total;i++)
    if keyboard_check_pressed(ord("1")+i)
        if global.wep[i,e_wep.have]
            current_weapon = i;


if global.wep[0 ,e_wep.ammo] <= 100
{
global.wep[0 ,e_wep.ammo] = 100;
}

obj_game_controller (perssistent)

DRAW GUI


GML:
//draw_gui event
var box_width = sprite_get_width(mask_explosion); //how wide is each box that will display a weapon?

for (var i = 0; i < weapons_total; i ++){
   draw_x = i * box_width;
   draw_y = 0; //Assuming sprites origin is drawn from top left

   //if you have each weapon image in the same sprite, you can draw them all just using "i"
   if ( global.wep[i,e_wep.have] == true ) draw_sprite(spr_weapon,  i, draw_x, draw_y);
}

obj_player

create


GML:
attClock = 0;
have = true;
ammo = 10;
firerate = 15;
dmg_min = 3;
dmg_max = 5;
obj_player

step



GML:
attClock--

var cur = obj_game_controller.current_weapon; // the control object have the index of the current weapon, so we need to fetch that first

if keyboard_check_pressed(ord('X'))
&& attClock <= 0
&& global.wep[cur,e_wep.ammo] > 0{
    attClock = global.wep[cur,e_wep.firerate];
    global.wep[cur,e_wep.ammo]-=1;

    var ins = instance_create(x, y-17,global.wep[cur,e_wep.ammo_type] );
    //var ins = instance_create(x, y-17, obj_bullet); // this line here needs some love
    ins.hspeed = image_xscale * 15;
    ins.dmg = irandom_range(global.wep[cur,e_wep.dmg_min],global.wep[cur,e_wep.dmg_max]);

   audio_play_sound(shot_sounds, 1, false); //shot sounds

}

Hero event collision (obj_player with an object chest with weapons and ammunition obj_chest)

This object is immediately in the first stage(room)
the hero takes it adds and weapon and the sprite ammo in the gui is displayed, but still cannot shoot magma .. all the time the player has a primary weapon ..
buttons from 1 to 5 do not respond to switching weapons.


GML:
///weapon magma

global.wep[1,e_wep.have] = true;
global.wep[1,e_wep.ammo] += 30;

obj_magma

create


GML:
gun = e_gun.magma;
 
Last edited:

skofrant

Member
it works!

it was enough to change this line from false to true


obj_game_controller perssistent

create

GML:
global.wep[0, e_wep.have] = true;

and this code

and this code was removed from the step event

obj_game_controller perssistent

step


GML:
if global.wep[0 ,e_wep.ammo] <= 100
{
global.wep[0 ,e_wep.ammo] = 100;
}
now two things remain ..

1. How to solve the problem with the primary weapon so that it has unlimited ammunition?

2. Weapons sprite in DRAW GUI EVENT what to do to display 5 sprites in one sprite and with the sum of ammunition .. because now they are all displayed next to each other
 

skofrant

Member
Now, with the display of the sprite of weapons, I would like to get the effect as in the video below time from 5:01
That is 5 sprites displayed in one sprite ... with the amount of ammo. Primary weapon displayed without ammo ... Unless someone makes the infinity sign for the primary weapon

 

TailBit

Member
1. you already know the problem and have been told how to solve it:

it's this part of the shooting code
GML:
&& global.wep[cur,e_wep.ammo] > 0{
    attClock = global.wep[cur,e_wep.firerate];
    global.wep[cur,e_wep.ammo]-=1;
that only checks if ammo is greater then 0 .. you need to check if it is >0 or -1 .. and don't allow it to lower the ammo if it is -1

2. well, that change your whole draw gui code to just 2 lines, just draw the sprite with the weapon and use current_weapon as the image index

then draw the ammo number of current_weapon .. you can check if it is -1 first and draw a infinite symbol instead


EDIT: you can write the player step even a bit different, so that you instead of writing out the arrays all the time, you unload the values you want to use first .. will make the core code look a bit more manageable
Code:
attClock--

// have some checks before so that it doesn't have to fetch the variables every step
if keyboard_check_pressed(ord('X'))
&& attClock <= 0{

    // then we fetch the variables we want to use
    var cur       = obj_game_controller.current_weapon; // the control object have the index of the current weapon, so we need to fetch that first
    var ammo      = global.wep[cur,e_wep.ammo];
    var firerate  = global.wep[cur,e_wep.firerate];
    var ammo_type = global.wep[cur,e_wep.ammo_type];
    var dmg_min   = global.wep[cur,e_wep.dmg_min];
    var dmg_max   = global.wep[cur,e_wep.dmg_max];

    // those variables will make this code easier to read
    if ammo > 0{
        attClock = firerate;
        ammo-=1;

        var ins = instance_create(x, y-17, ammo_type );
        ins.hspeed = image_xscale * 15;
        ins.dmg = irandom_range(dmg_min,dmg_max);

        audio_play_sound(shot_sounds, 1, false); //shot sounds
    }
   
    // but those variables you might change should be updated at the end:
    global.wep[cur,e_wep.ammo] = ammo;

}
 
Last edited:

skofrant

Member
ok what about the collision mask? because in the original code I had ... the point is that my hero shoots from one hand and there I have perfectly set collisions in the mask

step hero

GML:
with instance_create(x,y,par_attack) {
     sprite_index = mask_player_attack;
     image_xscale = other.image_xscale;

{
    var box, ID;
    if image_xscale == -1
        box = bbox_left;
    else
        box = bbox_right;
       
        ...
       
       
        }
 
Last edited:

skofrant

Member
how should i fix it? for the hero to have unlimited weapons?


step obj_player

GML:
&& global.wep[cur,e_wep.ammo] > 0{
    attClock = global.wep[cur,e_wep.firerate];
    global.wep[cur,e_wep.ammo]-=1;
and in the draw gui event, how to adjust this code as in the video above to achieve this goal?


draw gui

GML:
//draw_gui event
var box_width = sprite_get_width(mask_explosion); //how wide is each box that will display a weapon?

for (var i = 0; i < weapons_total; i ++){
   draw_x = i * box_width;
   draw_y = 0; //Assuming sprites origin is drawn from top left

   //if you have each weapon image in the same sprite, you can draw them all just using "i"
   if ( global.wep[i,e_wep.have] == true ) draw_sprite(spr_weapon,  i, draw_x, draw_y);
}
that's the last thing ... these two things
I will not tire you anymore ;-)
 

TailBit

Member
I think he got the brackets mixed up a bit in that code, the par_attack can be completely isolated, then

GML:
        with instance_create(x,y,par_attack) {
            sprite_index = mask_player_attack;
            image_xscale = other.image_xscale;
            owner = par_player;

            var box;
            if(image_xscale == -1) box = bbox_left; else box = bbox_right;
        }

        var ins = instance_create(box+image_xscale*49, y-17, ammo_type );
        ins.hspeed = image_xscale * 15;
        ins.dmg = irandom_range(dmg_min,dmg_max);
well, actually you could just change the player mask_index to mask_player_attack, then change it back to -1 after setting the box value, so you wouldn't need par_attack.


.. what is the par_attack for?

how should i fix it? for the hero to have unlimited weapons?
you have already written the solution for this .. I have explained how you must change the arguments, what you must check for ..

and in the draw gui event, how to adjust this code as in the video above to achieve this goal?
delete or comment out your whole draw gui event

draw sprite .. use image with subimages .. use current_weapon as image_index .. done

then draw the ammo variable for the current_weapon .. maybe use if to check if it is -1 to draw a infinite sign instead .. done

Good luck .. I'm done.
 

skofrant

Member
Hmmnm...

Obj_player

Step???

I mixed up something here :-(

GML:
&& ( global.wep[cur,e_wep.ammo] > 0 || attClock== -1){

if (global.wep[cur,e_wep.firerate]   != -1) global.wep[cur,e_wep.ammo]-=1;

}

how to combine this my step hero code with a part of the hero collision mask?

Obj_player

Step

GML:
attClock--

var cur = obj_game_controller.current_weapon; // the control object have the index of the current weapon, so we need to fetch that first

if keyboard_check_pressed(ord('X'))
&& attClock <= 0
&& global.wep[cur,e_wep.ammo] > 0{
    attClock = global.wep[cur,e_wep.firerate];
    global.wep[cur,e_wep.ammo]-=1;

    var ins = instance_create(x, y-17,global.wep[cur,e_wep.ammo_type] );
    //var ins = instance_create(x, y-17, obj_bullet); // this line here needs some love
    ins.hspeed = image_xscale * 15;
    ins.dmg = irandom_range(global.wep[cur,e_wep.dmg_min],global.wep[cur,e_wep.dmg_max]);

   audio_play_sound(shot_sounds, 1, false); //shot sounds

}
GML:
with instance_create(x,y,par_attack) {
            sprite_index = mask_player_attack;
            image_xscale = other.image_xscale;
            owner = par_player;

            var box;
            if(image_xscale == -1) box = bbox_left; else box = bbox_right;
        }

        var ins = instance_create(box+image_xscale*49, y-17, ammo_type );
        ins.hspeed = image_xscale * 15;
        ins.dmg = irandom_range(dmg_min,dmg_max);
 

skofrant

Member

When I have the hero event step removed from par attack and mask player attack, I will not be able to set the attack collision mask so that the hero shoots perfectly from the hand and not from the abdomen, etc.





GML:
sprite_index = mask_player_attack;// The hero's attack sprite allows the hero to shoot perfectly from his hand
image_xscale = other.image_xscale;


How to combine this with my new hero code from the step event



GML:
attClock--

var cur = obj_game_controller.current_weapon; // the control object have the index of the current weapon, so we need to fetch that first

if keyboard_check_pressed(ord('X'))
&& attClock <= 0
&& global.wep[cur,e_wep.ammo] > 0{
    attClock = global.wep[cur,e_wep.firerate];
    global.wep[cur,e_wep.ammo]-=1;

    var ins = instance_create(x, y-17,global.wep[cur,e_wep.ammo_type] );
    //var ins = instance_create(x, y-17, obj_bullet); // this line here needs some love
    ins.hspeed = image_xscale * 15;
    ins.dmg = irandom_range(global.wep[cur,e_wep.dmg_min],global.wep[cur,e_wep.dmg_max]);

   audio_play_sound(shot_sounds, 1, false); //shot sounds

}


As for par_attack you mentioned that it is not needed ... I think so too ... and the par_attack code looks like this

par_attack object

create


GML:
///Setup
owner = noone;
followOwner = true;
loop = false;
strenght = 1;

begin step

GML:
///No owner
if !owner == noone && !instance_exists(owner) {
 instance_destroy();
}
if !owner == noone && instance_exists(owner) && followOwner == true {
 image_xscale = owner.image_xscale;
 x = owner.x;
 y = owner.y;
 image_angle = owner.image_angle;
}
animation end

GML:
///Destroy
if loop == false {
 instance_destroy();
}

As for the case from the previous message, so that the main weapon ammunition would not run out of the hero, I was stuck in this place



GML:
&& ( global.wep[cur,e_wep.ammo] > 0 || attClock== -1){

if (global.wep[cur,e_wep.firerate]   != -1) global.wep[cur,e_wep.ammo]-=1;

}
 

skofrant

Member
You have to take this part of the code into account, because the whole hero's attack animation crashes.
there must be this part of the code ..

obj_player

step


GML:
attClock = 15;  
curAttack = 1;
with instance_create(x,y,par_attack) {
     sprite_index = mask_player_attack;
     image_xscale = other.image_xscale;

{
    var box, ID;
    if image_xscale == -1
        box = bbox_left;
    else
        box = bbox_right;

GML:
attClock--

var cur = obj_game_controller.current_weapon; // the control object have the index of the current weapon, so we need to fetch that first

if keyboard_check_pressed(ord('X'))
&& attClock <= 0
&& global.wep[cur,e_wep.ammo] > 0{
    attClock = global.wep[cur,e_wep.firerate];
    global.wep[cur,e_wep.ammo]-=1;

    var ins = instance_create(x, y-17,global.wep[cur,e_wep.ammo_type] );
    //var ins = instance_create(x, y-17, obj_bullet); // this line here needs some love
    ins.hspeed = image_xscale * 15;
    ins.dmg = irandom_range(global.wep[cur,e_wep.dmg_min],global.wep[cur,e_wep.dmg_max]);

   audio_play_sound(shot_sounds, 1, false); //shot sounds

}

will anyone help me put it together?
 
Last edited:

skofrant

Member
I recorded a video showing what the problem is:

in brief:


after modifying the attack code in the hero's steppe, my attack animation looks just like in the video:



it changed as i now have a weapon swap system ...
Before that, everything was fine.


all my hero code below


obj_player

create



GML:
hp = 10;
maxHp = 10;
attClock = 0;
curAttack = 0;
hurtClock = 0;
swimDir = 0;
drawAngle = 0;
jumpStr = 0;
jumpTimes = 0;
climbing = false;

//Power Ups
pwrupSpd = 0;
pwrupStr = 0;
pwrupJmp = 0;

//Spawn position and center view
spawnX = x;
spawnY = y;
centerViewOnMe();


have = true;
ammo = 10;
firerate = 15;
dmg_min = 3;
dmg_max = 5;

obj_player

step


GML:
///Controls
if !place_meeting(x,y,obj_water) {

//Normal movement
if climbing == false {
  if keyboard_check_pressed(kJump) && inAir == false {
   vspd = -10-sign(pwrupJmp*3);
   jumpStr = 2;
 
   //Step down
   if keyboard_check(kDown) && place_meeting(x,y+1,par_solid_ghost) {
    y += 4;
    vspd = 0;
    jumpStr = 0;
    jumpTimes = 0;
   }
  } else if keyboard_check(kJump) && jumpStr > 0 {
   vspd -= 2+sign(pwrupJmp);
   jumpStr--;
  } else {
   if keyboard_check_released(kJump) && jumpStr > 2 {
    vspd += 2+sign(pwrupJmp);
   }
   jumpStr = 0;
  }
  if jumpTimes > 0 && keyboard_check_pressed(kJump) && inAir == true {
   vspd = -11-sign(pwrupJmp*3);
   jumpStr = 0;
   jumpTimes--;
  }




attClock--

var cur = obj_game_controller.current_weapon; // the control object have the index of the current weapon, so we need to fetch that first

if keyboard_check_pressed(ord('X'))
&& attClock <= 0
&& global.wep[cur,e_wep.ammo] > 0{
    attClock = global.wep[cur,e_wep.firerate];
    global.wep[cur,e_wep.ammo]-=1;
   
    var ins = instance_create(x, y-17,global.wep[cur,e_wep.ammo_type] );
    //var ins = instance_create(x, y-17, obj_kula); // this line here needs some love
    ins.hspeed = image_xscale * 15;
    ins.dmg = irandom_range(global.wep[cur,e_wep.dmg_min],global.wep[cur,e_wep.dmg_max]);

    //audio_play_sound(shot_sounds, 1, false); //shot sounds

}


/////////////////////is the attack code prior to the introduction of the weapon swap system
/*

if keyboard_check_pressed(ord('X')) && attClock == false && inAir=false  && instance_number(obj_bullet) < 3
{
attClock = 15;  
curAttack = 1;
with instance_create(x,y,par_attack) {
     sprite_index = mask_player_attack;
     image_xscale = other.image_xscale;

{
    var box, ID;
    if image_xscale == -1
        box = bbox_left;
    else
        box = bbox_right;
       
    ID = instance_create(box+image_xscale*49, y-17, obj_bullet);
    ID.hspeed = image_xscale * 15;
    owner = par_player;
//audio_play_sound(shot_sounds, 1, false); shot sounds
}    
}
  }

*/



///////////////////////////////the hero shoots from the air
/*
if keyboard_check_pressed(ord('X'))and (key_check(key.jump)) && attClock == false && instance_number(obj_bullet) < 3
{

attClock = 15;   
curAttack  = 1;
with instance_create(x,y,par_attack) {
     sprite_index = spr_player_shoot_up;
     image_xscale = other.image_xscale;

{
    var box, ID;
    if image_xscale == -1
        box = bbox_left;
    else
        box = bbox_right;
       
    ID = instance_create(box+image_xscale*-10, y-25, obj_bullet);
    ID.hspeed = image_xscale * 15;
    owner = par_player;
   audio_play_sound(shot_sounds, 1, false); shot sounds
}     
}
  } 
   
*/

the hero shoots from the ladder
/*
if keyboard_check_pressed(ord('X'))and (key_check(key.jump)) && attClock == false &&   place_meeting(x,y,par_climbable) &&  climbing==true   &&  instance_number(obj_bullet) < 3
{

attClock = 15;   
curAttack  = 1;
with instance_create(x,y,par_attack) {
     sprite_index = spr_player_shoot_up;
     image_xscale = other.image_xscale;

{
    var box, ID;
    if image_xscale == -1
        box = bbox_left;
    else
        box = bbox_right;
       
    ID = instance_create(box+image_xscale*-10, y-25, obj_bullet);
    ID.hspeed = image_xscale * 15;
    owner = par_player;
   audio_play_sound(shot_sounds, 1, false); shot sounds
}     
}
  } 
*/




  if keyboard_check(kRight) && !keyboard_check(kLeft) {
   if hspd < 10+(sign(pwrupSpd)*5) {
    hspd = min(hspd+(2+(sign(pwrupSpd)*2))*multiF,10+(sign(pwrupSpd)*5));
   }
   image_xscale = 1;
  }
  if keyboard_check(kLeft) && !keyboard_check(kRight) {
   if hspd > -10-(sign(pwrupSpd)*5) {
    hspd = max(hspd-(2+(sign(pwrupSpd)*2))*multiF,-10-(sign(pwrupSpd)*5));
   }
   image_xscale = -1;
  }

  //Climb
  if place_meeting(x,y,par_climbable) && place_meeting(x,y-32,par_climbable) && place_meeting(x,y+32,par_climbable) && attClock <= 0 && (keyboard_check(kUp) || keyboard_check(kDown)) {
   climbing = true;
   vspd = 0;
   hspd = 0;
  }
}

//Climbing
if climbing == true {
  var __hsign = keyboard_check(vk_right)-keyboard_check(vk_left);
  var __vsign = keyboard_check(vk_down)-keyboard_check(vk_up);

  hspd = min(max(hspd+__hsign,-2),2);
  vspd = min(max(vspd+__vsign*2,-4),4);

  if __vsign == -1 && !place_meeting(x,y-32,par_climbable) {
   climbing = false;
   vspd = -10;
  }

  if __hsign != 0 {
   image_xscale = __hsign;
  }

  if !place_meeting(x,y,par_climbable) || !place_meeting(x,y+32,par_climbable) {
   climbing = false;
  }
}
} else {

//Swimming
swimDir = point_direction(0,0,keyboard_check(kRight)-keyboard_check(kLeft),keyboard_check(kDown)-keyboard_check(kUp));
if abs(keyboard_check(kRight)-keyboard_check(kLeft))+abs(keyboard_check(kDown)-keyboard_check(kUp)) != 0 {
  hspd += lengthdir_x(.5,swimDir);
  if !(swimDir > 0 && swimDir < 180) || place_meeting(x,y-sprite_height/3-1,obj_water) {
   vspd += lengthdir_y(.5,swimDir);
  }
}
if keyboard_check(kUp) && !place_meeting(x,y-sprite_height/1.5,obj_water) && vspd <= 0 {
  vspd = -12;
}
}

obj_player

end step



GML:
///Sprites
if !place_meeting(x,y,obj_water) {
if inAir == false && keyboard_check(kRight) = keyboard_check(kLeft) && sprite_index != spr_player_stand && climbing == false {
  sprite_index = spr_player_stand;
  image_index = 0;
}
if inAir == false && (keyboard_check(kRight) || keyboard_check(kLeft)) && keyboard_check(kRight) != keyboard_check(kLeft) && sprite_index != spr_player_run && climbing == false {
  sprite_index = spr_player_run;
  image_index = 0;
}



    if inAir == true && climbing = false

{
sprite_index = spr_player_air;// hero jump animation

}


if climbing == true {
  if x == xprevious && y == yprevious {
   if sprite_index != spr_player_hang {
    sprite_index = spr_player_hang;
   }
  } else {
   if sprite_index != spr_player_climb {
    sprite_index = spr_player_climb;
   }
  }
}

if attClock > 0 && curAttack == 0 {
  sprite_index = spr_player_attack;
  image_index = 23-(attClock);
  attClock -= 0.5;
}
if attClock > 0 && curAttack == 1 {
  sprite_index = spr_player_shoot;
  image_index = 1-(attClock);
  attClock -= 0.5;
}

if climbing == true {
if attClock > 0 && curAttack == 1  {
  sprite_index = spr_player_shoot_ladder;
  image_index = 1-(attClock);
  attClock -= 0.5;
}
}


if inAir == false {
   if attClock > 0 && curAttack == 1 && (key_check(key.right) || key_check(key.left)) {
  sprite_index = spr_player_attack;
  image_index = 23-(attClock);
  attClock -= 0.5;
}
  }

else



    if attClock > 0 && curAttack == 1 && (key_check(key.jump)) {
  sprite_index = spr_player_shoot_up;
  image_index = 91-(attClock);
  attClock -= 0.5;
}







swimDir = point_direction(xprevious,yprevious,x,y);
drawAngle = swimDir;
if image_xscale < 0 {
  drawAngle += 180;
}
} else {
if sprite_index != spr_player_water {
  sprite_index = spr_player_water;
  image_index = 0;
}

if keyboard_check(kRight) && !keyboard_check(kLeft) && image_xscale = -1 {
  image_xscale = 1;
  drawAngle -= 180;
} else if keyboard_check(kLeft) && !keyboard_check(kRight) && image_xscale = 1 {
  image_xscale = -1;
  drawAngle += 180;
}

if abs(keyboard_check(kRight)-keyboard_check(kLeft))+abs(keyboard_check(kDown)-keyboard_check(kUp)) != 0 {
  image_index += 2;
  if image_xscale > 0 {
   drawAngle -= angle_difference(drawAngle,swimDir)/16;
  } else {
   drawAngle -= angle_difference(drawAngle,180+swimDir)/16;
  }
}
}
 
Last edited:

skofrant

Member
I think the reason for this may be removing the variable
curAttack

hero from the attack code from the event step

prior to the changes, this variable was present and the attack animation played correctly

I don't know how to put it all together in the hero's step event


obj_player

step

new attack code


GML:
attClock--

var cur = obj_game_controller.current_weapon; // the control object have the index of the current weapon, so we need to fetch that first

if keyboard_check_pressed(ord('X')) && attClock <= 0 &&
(global.wep[cur,e_wep.ammo] > 0 || global.wep[cur,e_wep.ammo] == -1){
attClock = global.wep[cur,e_wep.firerate];

if (global.wep[cur,e_wep.ammo] >= 1) // if not infinite ammo
{
global.wep[cur,e_wep.ammo]-=1;
}

var ins = instance_create(x+lengthdir_x(64,direction),y-16,global.wep[cur,e_wep.ammo_type] );
ins.hspeed = image_xscale * 15;
ins.dmg = irandom_range(global.wep[cur,e_wep.dmg_min],global.wep[cur,e_wep.dmg_max]);

audio_play_sound(shot_sounds, 1, false); //shot sounds

}
obj_player

step

Attack code before changes with hero attack animation working




GML:
if keyboard_check_pressed(ord('X')) && attClock == false && inAir=false  && instance_number(obj_bullet) < 3
{

attClock = 15;   
curAttack = 1;

   with instance_create(x,y,par_attack) {
     sprite_index = mask_player_attack;
     image_xscale = other.image_xscale;
 
{
    var box, ID;
    if image_xscale == -1
        box = bbox_left;
    else
        box = bbox_right;
        
    ID = instance_create(box+image_xscale*49, y-17, obj_bullet);
    ID.hspeed = image_xscale * 15;
    owner = par_player;
    audio_play_sound(shot_sounds, 1, false); shot sounds
}     
 }
  }

below the end event step with hero sprite

obj_player

end step


GML:
///Sprites
if !place_meeting(x,y,obj_water) {
if inAir == false && keyboard_check(kRight) = keyboard_check(kLeft) && sprite_index != spr_player_stand && climbing == false {
  sprite_index = spr_player_stand;
  image_index = 0;
}
if inAir == false && (keyboard_check(kRight) || keyboard_check(kLeft)) && keyboard_check(kRight) != keyboard_check(kLeft) && sprite_index != spr_player_run && climbing == false {
  sprite_index = spr_player_run;
  image_index = 0;
}



    if inAir == true && climbing = false

{
sprite_index = spr_player_air;// hero jump animation

}


if climbing == true {
  if x == xprevious && y == yprevious {
   if sprite_index != spr_player_hang {
    sprite_index = spr_player_hang;
   }
  } else {
   if sprite_index != spr_player_climb {
    sprite_index = spr_player_climb;
   }
  }
}

if attClock > 0 && curAttack == 0 {
  sprite_index = spr_player_attack;
  image_index = 23-(attClock);
  attClock -= 0.5;
}
if attClock > 0 && curAttack == 1 {
  sprite_index = spr_player_shoot;
  image_index = 1-(attClock);
  attClock -= 0.5;
}

if climbing == true {
if attClock > 0 && curAttack == 1  {
  sprite_index = spr_player_shoot_ladder;
  image_index = 1-(attClock);
  attClock -= 0.5;
}
}


if inAir == false {
   if attClock > 0 && curAttack == 1 && (key_check(key.right) || key_check(key.left)) {
  sprite_index = spr_player_attack;
  image_index = 23-(attClock);
  attClock -= 0.5;
}
  }

else



    if attClock > 0 && curAttack == 1 && (key_check(key.jump)) {
  sprite_index = spr_player_shoot_up;
  image_index = 91-(attClock);
  attClock -= 0.5;
}







swimDir = point_direction(xprevious,yprevious,x,y);
drawAngle = swimDir;
if image_xscale < 0 {
  drawAngle += 180;
}
} else {
if sprite_index != spr_player_water {
  sprite_index = spr_player_water;
  image_index = 0;
}

if keyboard_check(kRight) && !keyboard_check(kLeft) && image_xscale = -1 {
  image_xscale = 1;
  drawAngle -= 180;
} else if keyboard_check(kLeft) && !keyboard_check(kRight) && image_xscale = 1 {
  image_xscale = -1;
  drawAngle += 180;
}

if abs(keyboard_check(kRight)-keyboard_check(kLeft))+abs(keyboard_check(kDown)-keyboard_check(kUp)) != 0 {
  image_index += 2;
  if image_xscale > 0 {
   drawAngle -= angle_difference(drawAngle,swimDir)/16;
  } else {
   drawAngle -= angle_difference(drawAngle,180+swimDir)/16;
  }
}
}
 
Last edited:

skofrant

Member
I was finally able to fix the entire hero attack animation.
but I can't locate the bug why my main gun has limited ammo should be unlimited ammo



obj_player

step

GML:
var cur = obj_game_controller.current_weapon; // the control object have the index of the current weapon, so we need to fetch that first

if keyboard_check_pressed(ord('X')) && attClock <= 0 && inAir=false &&
(global.wep[cur,e_wep.ammo] > 0 || global.wep[cur,e_wep.ammo] == -1) {


attClock = global.wep[cur,e_wep.firerate];

attClock = 20;
curAttack =1;

with instance_create(x,y,par_attack) {
sprite_index = mask_player_attack;
image_xscale = other.image_xscale;


}

    var box, ID;
    if image_xscale == -1
        box = bbox_left;
    else
        box = bbox_right;


if (global.wep[cur,e_wep.ammo] >= 1) // if not infinite ammo
{
global.wep[cur,e_wep.ammo]-=1;
}

  if(global.wep[0,e_wep.ammo] != -1){

    global.wep[0 ,e_wep.ammo]-=1;}





var ins = instance_create(x+lengthdir_x(64,direction),y-16,global.wep[cur,e_wep.ammo_type] );
ins.hspeed = image_xscale * 15;
ins.dmg = irandom_range(global.wep[cur,e_wep.dmg_min],global.wep[cur,e_wep.dmg_max]);

audio_play_sound(shot_sounds, 1, false); //shot sounds

}

I tried as below. Primary weapon in index is number 0

GML:
if (global.wep[0 ,e_wep.ammo] > 0 || global.wep[0 ,e_wep.ammo] == -1){ if(global.wep[0 ,e_wep.ammo] != -1) global.wep[0 ,e_wep.ammo]-=1; }
 
Last edited:
Top