How do I make efficient player controls system?

Get help using Construct 2

Post » Mon Sep 11, 2017 3:02 pm

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-
B
16
S
2
Posts: 60
Reputation: 984

Post » Mon Sep 11, 2017 4:00 pm

For Test gamepad events, like "Compare axis" you can test all 4 gamepads in a loop:
Code: Select all
For x from 0 to 3 
  Gamepad loopindex Right analog X>20   -> call some function and pass gamepad number (loopindex) as a parameter


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:
Code: Select all
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))
Check out my awesome game Doptrix
B
17
S
8
G
8
Posts: 161
Reputation: 5,809

Post » Mon Sep 11, 2017 4:13 pm

dop2000 wrote: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?

Code: Select all
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))


Not even sure if that makes sense, actually, but it got me curious... :)

Cheers!
Bruno

I like to eat, but I'm no cook. Why liking to play games would make you a game designer?
B
16
S
4
G
1
Posts: 408
Reputation: 2,297

Post » Mon Sep 11, 2017 4:18 pm

Nah, you can't combine two triggered events with "or"..
Check out my awesome game Doptrix
B
17
S
8
G
8
Posts: 161
Reputation: 5,809

Post » Mon Sep 11, 2017 4:23 pm

Thanks for clarifying... :) Then, it seems he can't do much about it... :(

[EDIT] @dop2000
Actually, you CAN make an Or Block with Triggered Actions. Just tried it.

Image

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

I like to eat, but I'm no cook. Why liking to play games would make you a game designer?
B
16
S
4
G
1
Posts: 408
Reputation: 2,297

Post » Mon Sep 11, 2017 4:34 pm

@Mithrill ,
I think you can also benefit from this plugin:
viewtopic.php?t=64458&start=0

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


@brunopalermo , how did you do that? My C2 doesn't allow to combine 2 triggered events. You must be running an outdated version :)
Last edited by dop2000 on Mon Sep 11, 2017 4:46 pm, edited 2 times in total.
Check out my awesome game Doptrix
B
17
S
8
G
8
Posts: 161
Reputation: 5,809

Post » Mon Sep 11, 2017 4:39 pm

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

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

I like to eat, but I'm no cook. Why liking to play games would make you a game designer?
B
16
S
4
G
1
Posts: 408
Reputation: 2,297

Post » Tue Sep 12, 2017 2:27 pm

@dop2000 Thank you very much. That was just what I was after :)
B
16
S
2
Posts: 60
Reputation: 984

Post » Tue Sep 12, 2017 2:43 pm

dop2000 wrote:@Mithrill ,
I think you can also benefit from this plugin:
viewtopic.php?t=64458&start=0

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


@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 :P
Last edited by Anonnymitet on Tue Sep 12, 2017 3:22 pm, edited 1 time in total.
B
54
S
23
G
12
Posts: 746
Reputation: 11,908

Post » Tue Sep 12, 2017 2:48 pm

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

I like to eat, but I'm no cook. Why liking to play games would make you a game designer?
B
16
S
4
G
1
Posts: 408
Reputation: 2,297

Next

Return to How do I....?

Who is online

Users browsing this forum: Lancifer, Nicolas951 and 20 guests