Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Ideas for making user defined controls?  (Read 3665 times)

0 Members and 1 Guest are viewing this topic.

Pixel_Outlaw

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Ideas for making user defined controls?
« on: January 09, 2011, 03:57:28 am »
Within the limits of C++ what would be a good way to give each instance of a Player type object configurable controls? This question has come while designing my video game because sometimes a player might want to use a joypad and other times they might want to use a keyboard. If I create a two player game and only one player has a joystick one will be forced to use the keyboard and this needs to be dynamic because input hardware changes from computer to computer.

So given a basic player class how should they store and react to custom control schemes? I certainly cannot have the same input methods for players 1 and 2 even though they both control an instance of a Player object.

ZenonSeth

  • Newbie
  • *
  • Posts: 19
    • View Profile
Ideas for making user defined controls?
« Reply #1 on: January 09, 2011, 04:04:02 pm »
Hey, I too have thought about this.
I came up with a solution, though long before I swtiched to SFML.

Here's the basic idea (adjusted to use SFML input style):

Have a variable in the player class for each possible command, i.e. MoveForward
Declare them of type sf::Key::Code, like so:
Code: [Select]
sf::Key::Code moveForwardKey;
sf::Key::Code moveBackwardKey;

Then, at runtime, read a settings file, or have a settings screen at which you pick the input. Say, I want the 'W' key to be my move forward. Then, after figuring that out, you would make moveForwardKey equal to W (from the sf::key:: namespace), like so:
Code: [Select]
moveForwardKey = sf::Key::W;
And finally, when handling the input, simply check the input key vs your variable, like this:
Code: [Select]
sf::Event Event;
while (App.GetEvent(Event))
{
    // If a key is pressed
    if (Event.Type == sf::Event::KeyPressed)
    {
        // and that key code is equivalent to whatever we wanted our MoveForwardKey to be
        if (Event.Key.Code == Player1.moveForwardKey)
        {
            // call the function to move player 1 forward here
        }
        if (Event.Key.Code == Player2.moveForwardKey)
        {
            // call the function to move player 2 forward here
        }
    }
}

That's the method I've used in the past.
Please note that each Player instance has all the variables (MoveForwardKey, MoveBackwardsKey etc), but they each can be assigned different values at runtime to handle differently.
I'm sort of assuming an OOP approach here, but it's doable without OOP as well.
I hope this answers your question.
- Zenon

Wafthrudnir

  • Newbie
  • *
  • Posts: 26
    • View Profile
Ideas for making user defined controls?
« Reply #2 on: January 09, 2011, 07:59:10 pm »
Just my thoughts:

Abstract class Controls
with Methods like isKeyUp() , isKeyFire(), isKeySpecialMove() (pressed)
setKeyUp() etc...

Then inherited classes:

class KeyboardControl : Controls
class JoystickControl : Controls

overriding the virtual methods of the abstract class.

Both classes handle the input, one for Keyboard and the other for a Joystick. Each class has the setKeyXXX() method. I think because SMFL has Codes for Keyboard and Joystick, this would be a good solution.

You can change the Control while the game is running, just by replacing the Control object. :D

So, in game it would look like this:
Code: [Select]

JoystickControls* joystick = new JoystickControls();
Player p1("Name",joystick);
//playing game...etc...but then user goes to options and chooses keyboard...
KeyboardControls* keyboard = new KeyboardControls();
p1.setControl(keyboard);


Regards,

Wafthrudnir
Black Metal + Coding = Win!

Pixel_Outlaw

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Ideas for making user defined controls?
« Reply #3 on: January 09, 2011, 11:43:03 pm »
Thanks for the ideas. Things get a little tricky with this kind of stuff.