Jump to content

Recommended Posts

Posted

First, how do you detect a player holding down a keybind, and constantly perform an action?

 

 

Second, my keybind is running all of its code twice.  I'm using KeyInputEvent, and its getPhase() returns an EventPriority (?!) so...how do I fix that?

 

 

Keybind

public class GatherManaKeybind extends KeyBinding
{
   public GatherManaKeybind()
   {
      super("key.gatherMana", Keyboard.KEY_G, "key.categories." + AAReference.MODID);
   }


   @SubscribeEvent(priority = EventPriority.HIGHEST)
   public void onKeyInput(InputEvent.KeyInputEvent event)
   {
      if(this.isPressed())
      {
         ~do it to it, but not twice~
      }
   }
}

 

 

ClientProxy

   @Override
   public void preInit(FMLPreInitializationEvent event)
   {
      super.preInit(event);
      GatherManaKeybind gatherKBD = new GatherManaKeybind();
      FMLCommonHandler.instance().bus().register(gatherKBD);
      ClientRegistry.registerKeyBinding(gatherKBD);
   }

Posted

For holding, no, I want the player to be able to hold the key down and have the code constantly run.  So moving to a ClientTickEvent might fix that?  With KeyInputEvent, holding the key only made the code run once. (er, twice, just, it wasn't continuous)

 

(What is WITH this forum's formatting going wonky randomly? <__<)

 

Edit: I changed it to a ClientTickEvent, but it's still not detecting the key is being held down, is there a different method I have to check for in KeyBinding?

 

public class GatherManaKeybind extends KeyBinding
{
   public GatherManaKeybind()
   {
      super("key.gatherMana", Keyboard.KEY_G, "key.categories." + AAReference.MODID);
   }


   @SubscribeEvent()
   public void onKeyInput(TickEvent.ClientTickEvent event)
   {
      if(event.phase == TickEvent.Phase.END && this.isPressed())
      {
         ~snip~
      }
   }
}

Posted

For holding, no, I want the player to be able to hold the key down and have the code constantly run.  So moving to a ClientTickEvent might fix that?  With KeyInputEvent, holding the key only made the code run once. (er, twice, just, it wasn't continuous)

 

(What is WITH this forum's formatting going wonky randomly? <__<)

 

Edit: I changed it to a ClientTickEvent, but it's still not detecting the key is being held down, is there a different method I have to check for in KeyBinding?

 

Here is just a bit of java info.

 

Java never "constantly" runs. Never. You can't program that (well you probably could but it's not how it works). WHat happens is you set the Boolean pressed to true, so it runs a class or etc. and then you check the Boolean throughout the code to make sure it is true, if it isn't, program the code to disregard the events that took place and to start over at Boolean pressed true when the keybind is pressed.

public class GatherManaKeybind extends KeyBinding
{
   public GatherManaKeybind()
   {
      super("key.gatherMana", Keyboard.KEY_G, "key.categories." + AAReference.MODID);
   }


   @SubscribeEvent()
   public void onKeyInput(TickEvent.ClientTickEvent event)
   {
      if(event.phase == TickEvent.Phase.END && this.isPressed())
      {
         ~snip~
      }
   }
}

Posted

For holding, no, I want the player to be able to hold the key down and have the code constantly run.  So moving to a ClientTickEvent might fix that?  With KeyInputEvent, holding the key only made the code run once. (er, twice, just, it wasn't continuous)

 

(What is WITH this forum's formatting going wonky randomly? <__<)

 

Edit: I changed it to a ClientTickEvent, but it's still not detecting the key is being held down, is there a different method I have to check for in KeyBinding?

 

public class GatherManaKeybind extends KeyBinding
{
   public GatherManaKeybind()
   {
      super("key.gatherMana", Keyboard.KEY_G, "key.categories." + AAReference.MODID);
   }


   @SubscribeEvent()
   public void onKeyInput(TickEvent.ClientTickEvent event)
   {
      if(event.phase == TickEvent.Phase.END && this.isPressed())
      {
         ~snip~
      }
   }
}

 

Here is just a bit of java info.

 

Java never "constantly" runs. Never. You can't program that (well you probably could but it's not how it works). What happens is you set the Boolean pressed to true, so it runs a class or etc. and then you check the Boolean throughout the code to make sure it is true, if it isn't, program the code to disregard the events that took place and to start over at Boolean pressed true when the keybind is pressed again.

 

NOTE: When I say disregard, I don't mean erase what happened due to the result of pressing the keybind, I mean to literally pretend it didn't happen (even though it did happen) and start over.

 

All of this is a bit hard to explain, but the point stand (if you can understand my mumbo-jumbo).

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.