Graal Forums  

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

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 11-29-2008, 08:13 AM
thatdwarf thatdwarf is offline
Former UN Dev Admin
Join Date: Nov 2005
Posts: 42
thatdwarf is on a distinguished road
Cant Control OnKeyPressed

Alright, right now I'm having a problem with a gun script.
The basic idea is that onKeyPressed() is calling the gun as if it were automatic (meaning you hold down the fire button, and tons of bullets fly out)

I need this to be limited to one shot per X seconds. Any suggestions for methods?





Note: to all of you just looking for a chance to blast a thread, I'm asking for method suggestions, not full code
Reply With Quote
  #2  
Old 11-29-2008, 09:44 AM
Tigairius Tigairius is offline
The Cat
Tigairius's Avatar
Join Date: Jan 2007
Location: Missouri, USA
Posts: 4,240
Tigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant future
Hmm onKeyPressed can be called over and over if you hold down the key I believe. If you don't want the brief wait after first key strike then you'll have to do some sort of timeout loop detecting if the key is pressed. This can be done by doing
PHP Code:
function onTimeout() {
  if (
keydown2(getkeycode("g"), true)) {
    
this.fire ++;
  }
  
setTimer(0.1);

Or similar. "this.fire" would increase while G is pressed.
__________________


“Shoot for the moon. Even if you miss, you'll land among the stars.”
Reply With Quote
  #3  
Old 11-29-2008, 10:26 AM
DustyPorViva DustyPorViva is offline
Will work for food. Maybe
DustyPorViva's Avatar
Join Date: Sep 2003
Location: Maryland, USA
Posts: 9,589
DustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond repute
Send a message via AIM to DustyPorViva Send a message via MSN to DustyPorViva
Don't rely on onKeyPressed() it relies on the OS setting of the player's keyboard, in which the repeat can be slow or fast... which would of course vary between one player and another.
Reply With Quote
  #4  
Old 12-02-2008, 01:12 AM
thatdwarf thatdwarf is offline
Former UN Dev Admin
Join Date: Nov 2005
Posts: 42
thatdwarf is on a distinguished road
Thanks tig <3
Reply With Quote
  #5  
Old 12-02-2008, 06:24 PM
Ronnie Ronnie is offline
Registered User
Join Date: Jun 2008
Location: Nj, USA
Posts: 48
Ronnie is on a distinguished road
Send a message via AIM to Ronnie
Since I hate Clientside timeouts and this should work also:

PHP Code:
function onKeyPressed() {
 if (
keydown2(getkeycode("g"), true))  { 
   if (
this.delay == NULL) {
     
this.delay .5;
       
//SHOOT STUFF
     
sleep(this.delay);
     
this.delay 0;
     }
    }
   } 
-Ronnie
Reply With Quote
  #6  
Old 12-02-2008, 06:27 PM
xXziroXx xXziroXx is offline
Malorian
xXziroXx's Avatar
Join Date: May 2004
Posts: 5,289
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
Quote:
Originally Posted by ronnie View Post
stuff
Absolutely NOT!
__________________
Follow my work on social media post-Graal:Updated august 2025.
Reply With Quote
  #7  
Old 12-02-2008, 08:16 PM
Ronnie Ronnie is offline
Registered User
Join Date: Jun 2008
Location: Nj, USA
Posts: 48
Ronnie is on a distinguished road
Send a message via AIM to Ronnie
Quote:
Originally Posted by xXziroXx View Post
Absolutely NOT!
Why not it works fine for me ?
Reply With Quote
  #8  
Old 12-02-2008, 08:31 PM
xXziroXx xXziroXx is offline
Malorian
xXziroXx's Avatar
Join Date: May 2004
Posts: 5,289
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
Quote:
Originally Posted by Ronnie View Post
Why not it works fine for me ?
To begin with, sleeps are the devil and should be avoided at all costs, if possible. My old computer works too, doesn't mean I'm supposed to use it.
__________________
Follow my work on social media post-Graal:Updated august 2025.
Reply With Quote
  #9  
Old 12-02-2008, 11:03 PM
DrakilorP2P DrakilorP2P is offline
Registered User
DrakilorP2P's Avatar
Join Date: Apr 2006
Posts: 755
DrakilorP2P is just really niceDrakilorP2P is just really nice
Quote:
Originally Posted by xXziroXx View Post
Absolutely NOT!
Can someone explain why sleep() is so horrible it warrants such a huge text? Does it halt the entire machine or what?
__________________
Testbed user: I figured since I can never find any scripters it was time to take desperate measures...and...TEACH MYSELF 0.0
Reply With Quote
  #10  
Old 12-02-2008, 11:33 PM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
Quote:
Originally Posted by DrakilorP2P View Post
Can someone explain why sleep() is so horrible it warrants such a huge text? Does it halt the entire machine or what?
Lol, no, but it does mess with timeouts. I don't know the exact extent of what sleep does, but considering that it was made before functions could wait I think it does a bit more than simply halting execution for the current thread. When I want a function to pause and not interfere with anything else I use waitfor().
__________________
Reply With Quote
  #11  
Old 12-02-2008, 11:36 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by thatdwarf View Post
PHP Code:
function onWeaponFired() {
  
this.on true;
  
doGani();
  
onTimeout();
}

function 
onTimeout() {
  if (
keydown2(getkeycode("f"), true)) {
    
this.delay++;
  }
  if(
this.delay 1) {                //check if semi or auto
    //ammo check here
    
freezeplayer(.1);
    
setani("gunganishere""");
    
temp.angle getangle(vecx(player.dir), vecy(player.dir));
    
setshootparams(STUFF GOES HERE); 
    
setShootCoords();
    
shoot(this.shootcoords[0],this.shootcoords[1], player.zrandom(temp.angle, (temp.angle this.accuracy) * .4),NULLNULL"blueball3""");
    
this.delay 0;
  }
  
setTimer(.1);

The problem is that you're detecting key presses wrong. Try something like this:

PHP Code:
function onTimeOut() {
  
this.delay -= 0.05;
  
  
temp.key_f keydown2(getkeycode("f"), false); // F key is pressed
  
if (key_f && ! this.keydown_f) {
    
this.mode this.mode// Changes mode between 0 and 1
    // player.chat = "Mode:" SPC (this.mode == 0 ? "Semi-Automatic" : "Automatic");
    // The above line would also work instead of the if statements
    
if (this.mode == 0) {
      
player.chat "Mode: Semi-Automatic";
    } else if (
this.mode == 1) {
      
player.chat "Mode: Automatic";
    }
  }
  
this.keydown_f key_f;
  
  
temp.key_s keydown2(getkeycode("s"), false); // S key is pressed
  
if (key_s && (this.mode == || ! this.keydown_s) && this.delay <= 0) {
    
// shoot
    
this.delay 1// replace the one with the shooting speed (seconds)
  
}
  
  
this.keydown_s key_s;
  
setTimer(0.05);

S to shoot, F to change modes. It should be simple to change the controls to your liking. Let me know if that doesn't work or you need it explained better, or need additional help.

Quote:
Originally Posted by DrakilorP2P View Post
Can someone explain why sleep() is so horrible it warrants such a huge text? Does it halt the entire machine or what?
Sorta, it can stop loops/timeouts, and throw stuff out of scope. It's normally best to work around it if possible.
__________________
Reply With Quote
  #12  
Old 12-02-2008, 09:34 PM
thatdwarf thatdwarf is offline
Former UN Dev Admin
Join Date: Nov 2005
Posts: 42
thatdwarf is on a distinguished road
Mk, so Tig's example didnt work as well as I would have hoped, might just be me. Here is what I'm working with:

Initially this.on = false and this.delay = 1;

PHP Code:
function onWeaponFired() {
  
this.on true;
  
doGani();
  
onTimeout();
}

function 
onTimeout() {
  if (
keydown2(getkeycode("f"), true)) {
    
this.delay++;
  }
  if(
this.delay 1) {                //check if semi or auto
    //ammo check here
    
freezeplayer(.1);
    
setani("gunganishere""");
    
temp.angle getangle(vecx(player.dir), vecy(player.dir));
    
setshootparams(STUFF GOES HERE); 
    
setShootCoords();
    
shoot(this.shootcoords[0],this.shootcoords[1], player.zrandom(temp.angle, (temp.angle this.accuracy) * .4),NULLNULL"blueball3""");
    
this.delay 0;
  }
  
setTimer(.1);

Reply With Quote
  #13  
Old 12-03-2008, 06:15 AM
thatdwarf thatdwarf is offline
Former UN Dev Admin
Join Date: Nov 2005
Posts: 42
thatdwarf is on a distinguished road
cbk, you rock. Rep+
<3
Reply With Quote
Reply


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 09:08 PM.


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