How do I reuse gamepad events for multiple players?

Get help using Construct 2

Post » Tue Nov 04, 2014 3:11 pm

I'm trying to implement a multiplayer aspect to my game and I keep getting tripped up for a reason I hope can be remedied.

As it stands, I have two event groups: the original player's gamepad events and a copy of the original player's gamepad events, which I tweaked to work with controller 2 and player 2 instead. This is fine and functional until I change player 1's events and forget to do the same with player 2.

My question is: Is there a way I could wire this up so I only use one event group to control all players? I have the both players in a family, so replacing all the player 1 objects would be no problem, but I don't know how to make sure the controller event moves the right one (controller 0 only moves player 1, controller 1 only moves player 2).

Thoughts?
B
5
S
2
Posts: 37
Reputation: 699

Post » Tue Nov 04, 2014 3:40 pm

One of the easiest way to do that would be to do all sprite/family actions with functions, and hook those functions to your gamepads events, and among the parameters, you'd send which player each function call should affect.
B
67
S
28
G
24
Posts: 970
Reputation: 18,675

Post » Wed Nov 05, 2014 12:20 am

Would I still need multiple event groups for each player, then, to do four controllers? The way it works in my head is this:

On gamepad (any number 0-3) A button pressed -> Call "jumpFunction" (gamepad.GamepadID)

Instead of creating this event four times with gamepad numbers 0-3, I could do just the one and it would pass the gamepad number to the function and perform the action on whichever character has the matching gamepad ID. However, I don't know how to put "any" in the trigger event and it seems like I can't grab the current gamepad ID to pass to the function (gamepad.GamepadID needs a parameter).

That's all totally theoretical and it's possible it just can't be done this way, but I thought I'd ask.
B
5
S
2
Posts: 37
Reputation: 699

Post » Wed Nov 05, 2014 12:30 am

I don't do input in the way you do it, but perhaps if you check if the gamepad's ID/number is the same as the player ID/number like this condition: Gamepad.GamepadID(player.ID)

You could always have at the top of an event, or group of events, a "For Loop" that runs from 0 to 3 (or whatever), and the check if the player ID and gamepad ID = loopindex("for loop")
B
147
S
73
G
20
Posts: 1,785
Reputation: 22,420

Post » Wed Nov 05, 2014 5:49 am

Wrap your button pressing / axis testing in a for loop, starting from 0 to Gamepad.GamepadCount - 1, and use loopindex as your gamepad index.
On gamepad loopindex A button pressed -> Call "jumpFunction" (loopindex)

On "jumpFunction"
+Pick player with instanceVar = Function.Params(0) --> Player jump
B
67
S
28
G
24
Posts: 970
Reputation: 18,675

Post » Wed Nov 05, 2014 4:25 pm

I ended up making a group of events with all the controller triggers set to "gamepad contID" and the functions pass contID, where contID is a local variable in the group with the gamepad ID assigned to it. I still had to make three copies for the other three players, but I only had to change that one local variable for them all to work.

Is there any advantage (performance or otherwise) to using your method over mine? I almost think mine might be better in the way that I could easily tweak it to allow each player to customize their controller mappings.
B
5
S
2
Posts: 37
Reputation: 699

Post » Wed Nov 05, 2014 4:45 pm

You can allow customization using my method. At the end of the day, it's only a matter of preference. Personnally, I prefer to go as generic as I can, much less of a hassle when refactoring is needed.

Here's how I would do it using a dictionnary (LocalStorage could also be used for persistence) :
genericGamepads.PNG


edit: I used triggers in this screenshot, I should have used the "is button index down" version instead, my bad !
You do not have the required permissions to view the files attached to this post.
B
67
S
28
G
24
Posts: 970
Reputation: 18,675

Post » Sun Nov 09, 2014 12:10 am

Cool, I'll keep this bookmarked in case I decide to come back around to it. I'm just happy to have it functional for right now.

Thanks for all the help!
B
5
S
2
Posts: 37
Reputation: 699


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 14 guests