How do I make efficient player controls system?

  • Hey everybody,

    I have a question about controlling player character(s). Currently I’m creating couch co-op game. Game supports 1 - 4 players. I’m trying to figure out how to make controls to function with all the player characters.

    Currently my game setup is this: each players character is created from the same object (sprite). Object is called hitbox. This sprite has an instance variable called ID. On creation, first player will get ID nb 0, second player 1 and so on. ID is zero-based because gamepad id’s are also zero-based. Key bindings are stored in an array. Then, when gamepad id (in this case, 0) is used, hitbox with the same ID is picked and action is fired (move left, move right, jump etc). I hope screenshot below will help to understand the basic concept.

    https://www.dropbox.com/s/0n750btrpkbl7 ... 1.png?dl=0

    This method isn’t very effective because it requires that I copy-paste exactly same events for every characters. 4 times in my case. Not very convenient. So I want to make a system where controls events are set once (like in screenshot but without that PLAYER1 value) and then based on the gamepad id, matching hitbox is picked and movement/action is fired.

    For example, I press A button on any controller, then C2 could tell me that button A is pressed from xyz gamepad. Let’s take a look at event 117 (in screenshot) for example. “Gamepad PLAYER1 right analog stick…”, where PLAYER1 is replaced with correct gamepad id.

    So, is there a way in C2 to determine which gamepad is used?

    -M-

  • For Test gamepad events, like "Compare axis" you can test all 4 gamepads in a loop:

    For x from 0 to 3  
      Gamepad loopindex Right analog X>20   -> call some function and pass gamepad number (loopindex) as a parameter
    [/code:12k6k14p]
    
    For [i]triggered[/i] events like "on button pressed", I'm afraid you need to create 4 copies of such events. But don't do anything inside these events, move all your logic to a function, which will take gamepad ID and button code as parameters:
    [code:12k6k14p]
    On Gamepad 0 any button pressed -> Function Call "ButtonPressed" (0, Gamepad.lastButton(0))
    On Gamepad 1 any button pressed -> Function Call "ButtonPressed" (1, Gamepad.lastButton(1))
    On Gamepad 2 any button pressed -> Function Call "ButtonPressed" (2, Gamepad.lastButton(2))
    On Gamepad 3 any button pressed -> Function Call "ButtonPressed" (3, Gamepad.lastButton(3))
    [/code:12k6k14p]
  • For triggered events like "on button pressed", I'm afraid you need to create 4 copies of such events. But don't do anything inside these events, move all your logic to a function, which will take gamepad ID and button code as parameters:

    Hey dop2000!

    I've never actually used Gamepad, but, couldn't he just have an "or block" with all "any button" events?

    On Gamepad 0 any button pressed 
    or
    On Gamepad 1 any button pressed 
    or
    On Gamepad 2 any button pressed 
    or
    On Gamepad 3 any button pressed 
    
         Function Call "ButtonPressed" (Gamepad.GamepadID, Gamepad.lastButton(Gamepad.GamepadID))
    [/code:3cb9xmrx]
    
    Not even sure if that makes sense, actually, but it got me curious... 
    
    Cheers!
  • Nah, you can't combine two triggered events with "or"..

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thanks for clarifying...

    [EDIT] dop2000

    Actually, you CAN make an Or Block with Triggered Actions. Just tried it.

    But the Gamepad.GamepadID is not what I thought it was, so my idea is bogus anyway.

  • Mithrill ,

    I think you can also benefit from this plugin:

    I helps to optimize your code, makes it tidy and easier to read. Your "ButtonPressed" function may look like this:

    @brunopalermo , how did you do that? My C2 doesn't allow to combine 2 triggered events. You must be running an outdated version

  • That's good. Didn't know this plugin.

    I never understood why Construct doesn't support switch natively...

  • dop2000 Thank you very much. That was just what I was after

  • Mithrill ,

    I think you can also benefit from this plugin:

    I helps to optimize your code, makes it tidy and easier to read. Your "ButtonPressed" function may look like this:

    brunopalermo , how did you do that? My C2 doesn't allow to combine 2 triggered events. You must be running an outdated version

    If you right click on your event block and select "make OR block" then you can add several triggers to the same event which will fire off if any of the triggers triggers

  • Yeah. You must FIRST create an Or block, THEN add other triggers.

  • Thanks!

    This isn't very intuitive - when you create an Or block with one condition, nothing happens on the screen.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)