Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Cant Control OnKeyPressed (https://forums.graalonline.com/forums/showthread.php?t=82942)

thatdwarf 11-29-2008 08:13 AM

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

Tigairius 11-29-2008 09:44 AM

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.

DustyPorViva 11-29-2008 10:26 AM

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.

thatdwarf 12-02-2008 01:12 AM

Thanks tig <3

Ronnie 12-02-2008 06:24 PM

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

xXziroXx 12-02-2008 06:27 PM

Quote:

Originally Posted by ronnie (Post 1446270)
stuff

Absolutely NOT!

Ronnie 12-02-2008 08:16 PM

Quote:

Originally Posted by xXziroXx (Post 1446271)
Absolutely NOT!

Why not it works fine for me ?

xXziroXx 12-02-2008 08:31 PM

Quote:

Originally Posted by Ronnie (Post 1446295)
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.

thatdwarf 12-02-2008 09:34 PM

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);



DrakilorP2P 12-02-2008 11:03 PM

Quote:

Originally Posted by xXziroXx (Post 1446271)
Absolutely NOT!

Can someone explain why sleep() is so horrible it warrants such a huge text? Does it halt the entire machine or what?

Inverness 12-02-2008 11:33 PM

Quote:

Originally Posted by DrakilorP2P (Post 1446328)
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().

cbk1994 12-02-2008 11:36 PM

Quote:

Originally Posted by thatdwarf (Post 1446308)
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 (Post 1446328)
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.

Inverness 12-02-2008 11:37 PM

Quote:

Originally Posted by cbk1994 (Post 1446334)
It's normally best to work around it if possible

By using waitfor() as I said.

cbk1994 12-02-2008 11:49 PM

Quote:

Originally Posted by Inverness (Post 1446335)
By using waitfor() as I said.

I saw your post after I posted, sorry.

You beat me to it :[

thatdwarf 12-03-2008 06:15 AM

cbk, you rock. Rep+
<3


All times are GMT +2. The time now is 05:21 AM.

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