View Single Post
  #4  
Old 06-26-2013, 03:45 PM
callimuc callimuc is offline
callimuc's Avatar
Join Date: Nov 2010
Location: Germany
Posts: 1,015
callimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to behold
Quote:
Originally Posted by defaultaccount View Post
I'm trying to get my weapons script to work, I tried searching how to make a rate of fire, but it doesn't work while walking it only works when im standing still to shoot.

NPC Code:
-code-


holding d on this makes me shoot when I change my direction i'm trying to make it shoot when im walking, it shoots when I move but only when I move
1st: style your code and please use PHP tags
2nd: you should always do '==' checks and not '=' checks within if() statements and such

now that we fixed this, your code will look like the following which makes it easier for us to read:

PHP Code:
function onKeyPressed() {
  if (
keydown(4)) {
    if (
this.equiped == true) {
      
player.weapon.trigger("WeaponFired");
    }
    else
      
onEquip();
  }
}

function 
onWeaponFired() {
  if (
this.fired) {
    if (
this.equiped == true) {
      if (
this.ammo 0) {
        
this.fired false;
        
setani(this.firenull);
        
this.ammo -= 1;
        
freezeplayer(this.freeze);
        
this.angle getangle(vecx(playerdir),vecy(playerdir));
        
setshootparams(player.account);
        
shoot(player.x+.3player.y+.3player.zthis.angle+random(this.bulletangleabs(this.bulletangle)), 0this.bspeed"pb_bullet1""pb_bullet.png");
        
triggerServer("gui"this.name"Shell");
      }
    }
  }
}

function 
onTimeout() {
  if (
this.fired == false) {
    
this.fired true;
    
setTimer(this.rof);
  }


personally I would do something like:

PHP Code:
/*
Flags I used:
  this.gunEquiped      - boolean - is the weapon currently equiped?
  this.gunShootTimer   - timer between each bullet
  this.gunCurrentShots - amount of bullets which have been fired
  this.gunMaxShots     - max bullets which can be used
  this.playerFreeze    - freezetimer for the player
  
Note:
  you would need to trigger the onEquipGun() and onUnEquipGun() depending on your system,
  this is just a very basic example and not completed. this is not being made secure but
  should work to understand the basics of such a set up and to learn from it
*/

//#CLIENTSIDE
function onCreated() {
    
//automatic gun or not?
  
this.gunAutomatic true;
    
//amount of bullets to shoot
  
this.gunMaxShots 20;
    
//timer between each bullet
  
this.gunShootTimer 0.25;
    
//freeze timer for the player
  
this.playerFreeze 0.1;
}

function 
onKeyPressed() {
    
//D button has not been pressed, so no need for further going
  
if (!keydown(4)) return;
  
    
//equip the weapon and stop going
  
if (this.GunEquiped == false) {
    
this.onEquipGun();
    return;
  }
    
//check if the D button has been pressed and if you shot less bullets then the clip size (in other words: if you can shoot)
    //check the canFireGun() function for the check
  
if (canFireGun() == false ) return;
  
    
//if the gun isnt automatic, shoot once and then stop the function
  
if (this.gunAutomatic == false) {
    
this.shootProjectile();
    return;
  }
    
//else keep going with the automatic shooting
  
while ( canFireGun() == true ) {
    
this.shootProjectile();
    
sleep(this.gunShootTimer);
  }
}

function 
shootProjectile() {
    
//I made it '>=' instead of '==' to have a small failsafe check in there
  
if (this.currentShots >= this.maxShots) {
    
this.onReloadGun();
    return;
  }
  
//your shooting setup in here
    //raise amount of shots being done
  
this.currentShots ++;
    
//freeze the player (based on default movement)
  
freezePlayer(this.playerFreeze);
}

function 
onReloadGun() {
  
//put in your reloading stuff in here
}

public function 
onEquipGun() {
  
//put in your equiping part here
    //set the flag to true
  
this.gunEquiped true;
}

public function 
onUnEquipGun() {
  
//put in your unequiping part here
    //your gun isn't equiped anymore
  
this.gunEquiped false;
}

function 
canFireGun() {
  
temp.toReturn = (keydown(4) == true && this.gunCurrentShots this.gunMaxShots);
  return 
temp.toReturn;


I would still do one main script in a class and then have the weapons linked to that class using this.join("classname"); so once you are doing a change, you dont need to update all weapons (depending on the change)
__________________
MEEP!

Last edited by callimuc; 06-26-2013 at 03:58 PM..
Reply With Quote