Not logged inOpenClonk Forum
Up Topic Development / Scenario & Object Development / [QUESTION] Hotkeys for ingame functions
- - By Sayned [kz] Date 2015-02-08 06:31
Not sure if topic's name is right, but I hope it is.

I always was wondering how is it possible to make hotkeys for some special functions ingame. For example in my most recent map called Arcane Rhapsody there's a system that could become much easier to use for regular players (and especially for those ones who are not used to OC controls yet) to play.

So the idea is to make an ingame function binded to special key. In Arcane Rhapsody I need to make a hotkey for let's say Q and W to add effect called "Octave" to your Clonk. So when we press Q game adds 1 "Octave" effect to the clonk, when we press W it removes 1 "Octave" effect from the Clonk.

Similar hotkeys were used on FlightFight map (which unfortunately doesn't work anymore :c). There if you pressed Q your plane would get knocked back, while C activated a turbo boost
Parent - - By Maikel Date 2015-02-08 10:10
The Flightfight is a good example where Q and C are self-defined controls, if you unpack it with the command c4group FlightFight.ocs -x you can have a look.

The relevant scripts are in System.ocg/PlayerControls.txt and System.ocg/Control.c some information is also in the documentation. The normal game content processes all the controls in System.ocg/PlayerControls.c so you need to overload one of these functions and process your new control and then return _inherited(.......); as is done for Control2Player in FlightFight.ocs but if could as well be the function PlayerControl or some other depending on your needs.

I hope this helps, let me know if you have more questions.
Parent - By Sayned [kz] Date 2015-05-09 13:40 Edited 2015-05-10 14:14
I'm really sorry for answering so late, unfortunately, I couldn't work on mod before. I've tried to understand how the whole hotkey system works, and, well I didn't manage to understand how to work with it anyways.

Even though I've tried to understand how to work right with this system, I still didn't manage to get it right.
So I'd like to know what's wrong (I'm pretty sure i did something wrong) with things I've done in PlayerControls.txt file:

[ControlDefs]
  [ControlDef]
  Identifier=IncOctave
  GUIName=IncOctave
  GUIDesc=Increases Octave
  SendCursorPos=1
  CoordinateSpace=Game
 
  [ControlDef]
  Identifier=DecOctave
  GUIName=DecOctave
  GUIDesc=Decreases Octave
  SendCursorPos=1
  CoordinateSpace=Game
 

[ControlSets]
  [ControlSet]
  Name=WASD_Hotkeys_IntQueue_MouseCon_
  GUIName=Keyboard and Mouse
  Keyboard=1
  Mouse=1
  Gamepad=0
 
    [Assignment]
    Key=Q
    Control=OctaveUp

    [Assignment]
    Key=C
    Control=OctaveDown


And the file where I do all the coding should be PlayerControls.c or Controls.c? In FlightFight Controls.c was used, while for me it appears like PlayerControls.c should be used.
How sould I work in PlayerControls.c/Controls.c?

Also I've got another little question. What do I have to write in PlayerControls.c/Controls.c? I've got such code:
global func Control2Player(int plr, int ctrl, int x, int y, int strength, bool repeat, bool release)
{
  if(ctrl == CON_IncOctave || ctrl == CON_DecOctave)
    DoOctave(plr, ctrl, x, y, strength, repeat, release);

  return inherited(plr, ctrl, strength, repeat, release);
}


I've also created DoOctave function (should it be global or not?) which adds effect to a Clonk depending on valur of ctrl variable

One last question: What should I do if I want this hotkey to require pressing two buttons at the same moment? For example combination of "Q" and "+" or even "Q" and "  Mouse1Wheel1Up"
Parent - - By Sven2 Date 2015-05-13 12:12
Sorry, don't have much time atm. I don't see a problem with your control set at the moment. Can you upload a test scenario where you define keys and it doesn't work?
Parent - By Sayned [kz] Date 2015-06-01 08:49
Uploading the test scenario. Q and C were supposed to change first 7 items' effects, but nothing actually happens. Yet it's possible to manually change their effect by using 8th and 9th items.
Attachment: ArcaneRhapsody.ocs (2840k)
Parent - - By Sayned [kz] Date 2017-01-17 13:04
Didn't feel like creating a new thread because I need a bit of help with a really similar thing:

Setting hotkeys that would function pretty much like a macro (in a way, I don't need to create actual macro). More specifically, I want certain buttons, like E and R for example, to activate items on 1st and 2nd inventory slot respectively. For example, I have a sword in my first slot and a bow in second slot, whenever I press E, clonk swings his sword and whenever I press R, clonk shoots with his bow. This is a rather rough example, because I'm planning on using such hotkeys with spells in my mod and spells there don't have any cast animations.

The way I imagine it: When player presses E key, game calls ControlUse function from the item (with all of the parameters the function has) in specified slot, if that makes sense
It doesn't have to work exactly like that, but I need a very similar effect.
Parent - - By K-Pone [de] Date 2017-01-17 14:07
Knüppeln does something like that. It's in a System.ocg script that appends to Clonk. It looks like this:

...
public func ObjectControl(int plr, int ctrl, int x, int y, int strength, bool repeat, bool release)

  if (ctrl == CON_Interact && !Contained())
  {
    if(!GetEffect("BlockingCD", this))
    {
      AddEffect("Blocking", this, 1, 1, this, GetID());
      AddEffect("BlockingCD", this, 1, BLOCK_CD);
    }
   
    return true;
  }
 
  if (ctrl == CON_InventoryShiftBackward || ctrl == CON_InventoryShiftForward)
    return 1;
 
  if (ctrl == CON_Throw && !IsCarryingHeavy() && !release)
  { 
    var item = GetItem(1);
    if (item)
    {
      if(item->~IsKnueppelItem())
      {
        item->~ControlUse(this, x, y);
      }
      else
      {
        ObjectCommand("Throw", item, x, y);
      }
      return true;
    }
  }
...


ctrl contains the key which is pressed, so you can use this to check which key was pressed (or released as well). Since this uses existing keys you'll also be able to remove default functionality of these keys which can be a nice side effect of this. Anyway, this function should end with

return _inherited(plr, ctrl, x, y, strength, repeat, release);

so other keys are still working though.

Also, as seen in the CON_Trow part of the script snippet, you can use GetItem() to get a specific itemslot's item from the clonk and call ControlUse for it, as it is done here.
Parent - - By Sayned [kz] Date 2017-01-18 16:14
Ok, so I've actually decided to take a look into Knüppeln files (great mod, btw! too bad I couldn't give it a try with friends because nobody's playing OC with me :( ) and I've noticed that there are CON_QuickSwitch, CON_Contents and CON_NextCrew. So these are Q, E and R respectively, right? Basically, if I do something like:
...
  if (ctrl == CON_QuickSwitch)
  {
    var item = GetItem(1);
    if (item) item->ControlUse(blah,blah,blah);
    return true;
  }
...

it should make it so whenever player presses Q, item in his first slot is activated and original Q functionality is disabled, right?
Parent - - By K-Pone [de] Date 2017-01-18 16:28
Yes, exactly. If you want to keep the original functionality of any of these keys, you can use return _inherited(plr, ctrl, x, y, strength, repeat, release); to do so. For example

...
  if (ctrl == CON_QuickSwitch)
  {
    if (specialusecondition)
    {
      var item = GetItem(1);
      if (item) item->ControlUse(blah,blah,blah);
      return true;
    }
    else
    {
      return _inherited(plr, ctrl, x, y, strength, repeat, release);
    }
  }
...


specialusecondition is just a variable in this case which you could define somewhere else. Just replace it by any code you need. So when specialusecondition returns true or something else non-zero, you have the special behaviour, else you get the default behaviour (in this case quickswitching items).
Parent - - By Sayned [kz] Date 2017-01-18 17:56 Edited 2017-01-18 18:15
Ok, so I got everything working semi correctly. It does indeed use item when I press certain key, however I ran into two problems:
1. Cursor position doesn't update correctly, therefore items are used in random direction. It's very incosistent so I can't even guess what cursor position it usually used whenenver a key is pressed
2. Item is used twice, when the key is pressed and when it's released. This is alright with many spells since they go on cooldown and can't be used after, however spells that can be reactivated after being casted are screwed over and are really hard to use
Parent - By Luchs Date 2017-01-18 19:02
1. It's expensive to always transfer the cursor position over the network, so it's usually only updated on mouse clicks. Use SetPlayerControlEnabled(player, CON_Aim, true/false); to enable/disable cursor position updates without clicks.

A better fix for your usecase (in the engine) would be to also update the cursor position on keyboard input.
Up Topic Development / Scenario & Object Development / [QUESTION] Hotkeys for ingame functions

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill