Download

Creating and registering multiple palyers with GameMode

Hi,

I don’t know if anyone could help. I was trying to work out the best way to create/register multiple players for a game. I haven’t got as far as getting to client server remote players, but simply as a game playing locally with for example one human player, and one or two AI players. At this point, the question stems from me wanting to pass the player start location from the level blueprint to the player controller, but I guess from here the question is going to come up a lot for me for different reasons. So the question is, what is the standard way of registering numerous players (AI or human) with GameMode, and what is the best way to set up communication between the game mode, level blueprint, and active players?

My first thought is to use a top down spawn structure, where the GameMode spawns all of the players to be in the game (e.g. 1 player, and 2 AI) using “Spawn actor/AI from class” (which one would be better to use?). GameMode will then have target references to all of the player instances, so communication between them would then be straight forward. I could use the “Get Game Mode” node for each of the players to get a reference to the game mode instance, or could create an initialise function for the players that the GameMode could call after spawning them that could pass all useful data (e.g. other players, level reference etc). Is there a standard way that most people go for when doing this, does the above sound ok, or is there a massive hole in the idea and my pc is going to chuck a cog out of the window?

As another related question, how can I get a reference to the level blueprint for the AI or player blueprints (e.g. if I want to get the player start location from the level blueprint?). And also, if I manually spawn a player controller, how will this affect the player controller that is auto-created by the engine at game start (will it be auto-added to the player index in get player controller)?

Any help would be great…

Thanks…

The standard way to register a player on the server is to connect to the server ip address from the client and the player will get registered by default with the default game mode. You can connect to the server by making a shortcut to the game editor and modifying the target. Your path will be slightly different:

When traveling to the server from the client within the game, you’ll use clienttravel() to the server’s ip address. Generally you’ll want to do all your multiplayer tests with -log. If you’re going to set anything in the level blueprint, the communication should be outward to the gamemode from the level blueprint but I would suggest not doing any communication between game mode and level due to the limiting factors of gametypes per level and issues to resolve NULL references. Active players are spawned from the gamemode and will be accessible from the gamemode. You can loop through all active player controllers from the gamemode or set references to the players at the time of spawning if you choose to do it that way. What you described in your second paragraph is a good approach, generally when your game expands you’ll find it useful to use player id’s and team id’s to identify players from one another.

Your third paragrah, I think you should stay clear away from the level blueprint as it’s not very dynamic. If you really must set a reference this way, use a “get game mode” from the level blueprint and cast to your game blueprint type. Then call a function that exists in your gamemode blueprint. You can create a new player with the “CreatePlayer” node or


        UGameplayStatics::CreatePlayer(this, -1);

The playercontroller that is autocreated is for player 1. If you create another playercontroller that will be for player 2 and so forth. When a player connects to the server a playercontroller will get auto created immediately for that person and be accessible from the gamemode. You can get a player start location from the game mode with “findplayerstart” or by using the tag associated with the player start:


	AActor* StartSpot;
	StartSpot = FindPlayerStart(NewPlayer, "player1Start");