2 player on one screen

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ArKeid0s

Hello I’m creating a multiplayer platformer but on one single screen. I would like to know how to create 2 player but with independant controls and only one camera that is fixed.


:bust_in_silhouette: Reply From: hilfazer

I used to make a BattleCity project for Godot 2.1. It’s very unfinished but movement control is done. It’s there:

First thing to do is go to Project/Project Settings/Input Map and add actions for 2nd player (or both).

units/Tank.tscn is a unit that is used by all players as well as AI. There is no input code there. Ignore this file for now.

In PlayerAgent.gd is PlayersActions constant. Put your actions from Input Map here. When you create PlayerAgent.gd node you use this constant in setActions() method like this:

var playerAgent = PlayerAgentGd.new()
playerAgent.setActions( PlayerAgentGd.PlayersActions[playerId - 1] )

One agent per player and remember to set different actions for them!
Now you need to assign agents to players’ units.

playerAgent.assignToTank( playerTank )

assingToTank() is defined in Agent.gd - base class for PlayerAgent.gd. It removes previous Agent from a unit and adds itself as a child. You probably won’t have tanks so you’ll need a different function name.

Have a look at processMovement() in PlayerAgent.gd. Reading input is done here. I set tank into motion by calling its setDirection() method. You’ll have your own code there.

This is Object Oriented Analysis and Design in practice. You won’t see it in any Godot tutorial. Player IS NOT player’s unit just like a driver IS NOT a car.
It’s not neccesarily better approach but it’s object oriented and since Godot is used in OO fashion i think it fits.