Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 06-24-2013, 11:23 PM
defaultaccount defaultaccount is offline
Cassini
Join Date: Feb 2006
Location: HeLLifAX
Posts: 299
defaultaccount is on a distinguished road
Triggeraction on classes

I need help on a script I'm making. I'm trying to destroy a npc by trigger the server to destroy the npc, but it's not working for me I searched for triggeractions but I think mines set up correctly. its from a weapons script that places a class npc, im trying to get it to delete itself, on clientside it only deletes until you walk in the level again.

this is the class script

NPC Code:
function onActionTrigger() {
destroy();
this.destroy();
}

//#CLIENTSIDE

function onCreated() {
setShape(1, 32, 32);
setimg("pb_bmp.png");
drawunderplayer;
dontblock;
setcharani("pb_bmp-clip", null);
this.dir = (player.dir) % 4;
settimer(1.00);
}

function onTimeout() {
triggeraction(x, y, "Trigger");
}

Reply With Quote
  #2  
Old 06-24-2013, 11:36 PM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
PHP Code:
function onCreated() {
  
setTimer(1);
}

function 
onTimeout() {
  
destroy();
}

//#CLIENTSIDE

function onCreated() {
  
setshape(1,32,32); 
  
this.ani "pb_bmp-clip";
  
this.dir player.dir;
  
drawunderplayer();
  
dontblock();

__________________
Quote:
Reply With Quote
  #3  
Old 06-26-2013, 12:11 AM
defaultaccount defaultaccount is offline
Cassini
Join Date: Feb 2006
Location: HeLLifAX
Posts: 299
defaultaccount is on a distinguished road
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:

function onKeypressed() {
if (keydown(4)) {
if (this.equiped = true) {
player.weapon.trigger("weaponFired");
} else if (this.equiped = false) {
onEquip();
}
}
}

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

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



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

Last edited by defaultaccount; 06-26-2013 at 12:23 AM..
Reply With Quote
  #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
  #5  
Old 06-26-2013, 04:11 PM
Chompy Chompy is offline
»\(║_o)/»
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Quote:
Originally Posted by callimuc View Post
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:
... 

personally I would do something like:

PHP Code:
... 

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)

Just a small addition to what callimuc posted, you do not need to check if something equals to true or false, as the if statement will go through if the value given equals true or false itself.

For example
NPC Code:

if (keydown(4) == true)



is the same as doing
NPC Code:

if (1 == 1) // keydown(4) returns true since they key was held down



the keydown() function returns a value of true or false depending on if the button specified is held down or not, and since booleans in graal basically converts into 1(true) and 0(false), we can use that!

Also, doing if (1 == 1) is in theory slower than doing if (1)

The speed difference isn't really noticed though, but here I am, saying it anyways

Also another edition, the ! operator functions in the way that it inverses the bit. Basically 0 would become 1 and 0 would become 1.

This is the reason you can do if (!keydown(4)) and it would fall through since the inverse of 0(false) is 1(true).
__________________
Reply With Quote
  #6  
Old 06-26-2013, 06:22 PM
defaultaccount defaultaccount is offline
Cassini
Join Date: Feb 2006
Location: HeLLifAX
Posts: 299
defaultaccount is on a distinguished road
Quote:
Originally Posted by callimuc View Post
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)
I tried updating the whole script to what you said, but it doesn't work now. I'm going to just try and add a sleep timer to the original script because it checks for holding d, the script I posted works, but it just doesn't shoot when you hold d. I'd post the whole script the way I updated it but I think someone would use it but look at my post in php by calliumuc

I basically set it to exactly what you posted calliumu but it doesn't shoot, reload or unequip. my old script works for everything except holding d , help me fix it by looking at my first post. It shoots fine it just doesn't work for holding shoot. Sorry but I don't want to completely have to reedit it I just need to add a sleep for the rate it fires, everything else works the way I made it orgionally unequip, shell and clip triggers, fire and the ganis work

I see what your saying about how I made it, the automatic works, but its very unresponsive. I need the wording to make it if D is held.

Last edited by defaultaccount; 06-26-2013 at 08:17 PM..
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 11:27 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.