Local Multiplayer Help

Bulldrome

Member
Hello I'm nearly finished with this little space ship arena game I've been working on and I just need to implement local multiplayer and I'll be just about done.
I currently have 4 ships each with 4 colors set up on a character select screen of sorts but I need help setting up the controls for player 1 and player 2.
How do I have it so that the ship player 1 selects gets one control scheme while players 2 gets the other control scheme?
 

Slyddar

Member
So are you wanting to setup 2 gamepads, or 2 controllers, or a mix of both?

I recently did this for my tank drag and drop game. Maybe worth watching as I discuss the controls a little -

Either way, I found it useful to have one player object only, with a player_id variable uniquely assigned to each player instance. 0 = player 1, 1 = player 2.
How I did it was have an option for 2 players on the keyboard, by setting up an array for each key
Code:
global.k_up[0] = ord("W");
global.k_up[1] = vk_up;

global.k_down[0] = ord("S");
global.k_down[1] = vk_down;

etc...
Then you can use the player_id variable to check for key presses when capturing, and use those captured variables to run the same code for both players.
Code:
if keyboard_check(global.k_up[player_id]) up = true;
 
Last edited:

Bulldrome

Member
Thank you for the reply.
I want both players to use the same keyboard to control their respective ships. Right now, I have something a little similar to what you proposed, I have a global variable for player 1 when they select their ship that is set to true when selected. Then in all the ships code, I have
if (global.player1 = true)
{
controls for player 1
}
else
{
controls for player 2
}

but that doesnt seem to work, would setting a specific global variable for player 2 help?
 

Slyddar

Member
"Then in all the ships code"... so you only have 1 ship/player object right?

Capture the inputs once at the start of player object step event, using each players respective inputs, then the rest of the code for the ship is exactly the same for both players.
 

Bulldrome

Member
I'm sorry, I'm not 100% how to do what you're saying, my GMS2 knowledge is limited.
Each ship is a player object that have similar code, the only differences are their weapons and attacks.
I don't know exactly how to capture the inputs at the start of the step event.

With your example above (global.k_up[1] = vk_up; etc), would I create those variables in the ship selection screen and then in the players step event do the controls like

if keyboard_check(global.k_up[1])
{
//code for vk_up here
}
 

woods

Member
couldnt you just have something like WASD and SPACE for P1 and ARROW KEYS and ENTER for P2?
then just assign the ships to each player at the selection screen?

ofc this would need seperate objects for each player
 

Bulldrome

Member
That's pretty much what I ended up doing, but it messed with my color selection screen where players could select the color of their ship.
I'm gonna return back to the color selection screen later and tackle it then
 

Jman

Member
I think you almost had it working with what you were going for with the global.player1 variable. Because it is a global variable all ships see it and think they are player 1. If you change it to an instance variable then you can set it to true for a specific instance while at the same time keeping it false for the other ships.
 
Top