playMaker

Author Topic: Detect if a key is still pressed? [SOLVED]  (Read 852 times)

NewbNinja

  • Playmaker Newbie
  • *
  • Posts: 15
Detect if a key is still pressed? [SOLVED]
« on: May 22, 2022, 06:33:21 PM »
Hey guys,

I'm pretty new to playmaker I'll admit it but Ive spent days looking for a way to detect if a key is still pressed.   I don't mean Get Key Up / Get Key Down because these only check the initial press/release .... this is what I'm trying to do.

I want my weapon to fire when pressed but continue firing until I release the mouse button.

I'm using the following format:

WAIT FOR KEY PRESS
Get Key Down ->  Mouse0 / Fire1 -> if pressed then SHOOT

SHOOT
Spawn emitter, fire once -> FINISHED -> return to WAIT FOR KEY PRESS


However, I want the emitter to keep firing every 1 second until the key is released.   I can't run a while loop or something like that can I?   If so, how do I do that?

Any ideas please people?   This is driving me insane.  I can code this but I started using playmaker and surely there's a function built in that Im overlooking that does a simple while(keyPressed) { FIRE, WAIT 1 SEC }    ?

Many Thanks!
NewbNinja
« Last Edit: May 26, 2022, 01:15:00 AM by NewbNinja »

NewbNinja

  • Playmaker Newbie
  • *
  • Posts: 15
Re: Detect if a key is still pressed?
« Reply #1 on: May 22, 2022, 06:58:24 PM »
So I actually found it myself through playing around.   I fixed this by creating 2 FSMs on the same object.

FSM
Check the key press UP / DOWN

If UP / DOWN -> set variable isFiring to TRUE/FALSE  in FSM2


FSM2
Start firing if isFiring == TRUE
Stop firing is isFiring == FALSE

I hope this helps someone ... this had my head in bits for at least 4-5 hours!


NewbNinja

  • Playmaker Newbie
  • *
  • Posts: 15
Re: Detect if a key is still pressed?
« Reply #2 on: May 22, 2022, 07:21:27 PM »
Maybe this helps:


djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Detect if a key is still pressed?
« Reply #3 on: May 22, 2022, 09:23:00 PM »
Hi.
You can also use a Get Key and a Bool Test

NewbNinja

  • Playmaker Newbie
  • *
  • Posts: 15
Re: Detect if a key is still pressed?
« Reply #4 on: May 23, 2022, 04:08:18 PM »
The problem we have is that this seems like 2nd nature to you, but I just cant visualize how to set that up.  Playmaker was meant to make things easier, I just can't help thinking programming this would've been easier lol.

It's very complicated for a "drag and drop" system.  The learning curve is higher than I expected.


Twood

  • Junior Playmaker
  • **
  • Posts: 76
Re: Detect if a key is still pressed?
« Reply #5 on: May 23, 2022, 06:03:29 PM »
Keep in mind you can use 'compare' or' test' variable actions in a state to transition out at any time, if you use 'every frame' on them. So that's similar to making loops and if/while and so on in coding.

eros

  • Playmaker Newbie
  • *
  • Posts: 9
Re: Detect if a key is still pressed?
« Reply #6 on: May 25, 2022, 02:37:10 AM »
Here's a simple implementation of what djaydino and Twood are describing:



The listener simply gets mouse button down and stores the result in the isFiring bool (but all you really need to do is fire the event, state 2 does all the heavy lifting).



The second state runs Get Mouse Button every frame and isFiring will return false when the mouse button is released. In my project, I have additional criteria that must be satisfied in order to shoot so I'm using Bool All True, but I stuck the Bool Test in there at the bottom to show you what it would look like in your case.



The final state just spawns my shot from the pool and I'm using the Wait action to determine fire rate, after which it returns to the Bool Op state and continues to fire through that little loop every 0.4 until the mouse button is released and isFiring returns False. PEW PEW PEW!
« Last Edit: May 25, 2022, 03:40:44 AM by eros »

NewbNinja

  • Playmaker Newbie
  • *
  • Posts: 15
Detect if a key is still pressed? [SOLVED]
« Reply #7 on: May 26, 2022, 01:14:45 AM »
Thanks for your help guys! <3