Switching between players

In this tutorial, we'll create a button in our UI that allows us to switch between two player characters, who begin in different scenes but can also meet and interact with each other through gameplay.

First, enable player-switching by going to the Settings Manager and changing the Player switching field to Allow. Then assign two Player prefabs, created using either the Character Wizard or by following these tutorials. Here, we have two Players: Tin Pot and Brain.

These characters are used by the included demo games, and can be found in /AdventureCreator/Demo/Resources, and /AdventureCreator/2D Demo/Resources, respectively. The techniques described in this tutorial can be applied to your own characters, however.

As only one Player can be present in the scene at any one time, we'll need to rely on "stand-in" NPCs so that the inactive Player is replaced by an interactive NPC that is used in his place. For example, if the Tin Pot is the active player, then we'll be able to see and interact with "Brain NPC".

These NPCs will need to appear identical to their respective Players to make the transition seamless. To aid this, we can convert copies of our existing Player prefabs by dragging them into the scene, and clicking Convert to NPC via the cog Menu in their Inspector:

Do this for both Players to create two new NPCs, "Brain NPC" and "Tin Pot NPC". Attach Remember NPC and Remember Transform components to both of them, and check Retain in prefab? in both to ensure that their ID numbers are persistent. In the Remember Transform component, also check Save scene presence? - this will be necessary because these NPC will be spawned and removed at runtime.

These NPCs will need to be prefabs - and placed in a Resources subfolder in your Project. For more information on this, see the Manual's "Saving asset references" chapter.

NOTE: To have the NPCs run the same Interactions regardless of which scene they're in, set their Hotspots' Interaction source fields to Asset file so that Interactions will rely on ActionList assets instead of scene-based actionlists.

Next, select each Player prefab, and assign their equivalent NPC prefab into their Associrated NPC prefab field, at the bottom:

For our non-default Player character, place an instance of their associated NPC in the scene that they first appear in. For example, if we begin the game as Tin Pot, place an instance of Brain NPC down in his own starting scene.

Next, we'll amend our UI by creating a button that can switch the active player. In the Menu Manager, select the InGame menu and create a new Button element:

Name this Button SwitchPlayer, and set its Button text to "Switch". Then set its Click type to Run Action List, and click Create beside it to automatically create and assign a new ActionList asset that will run when the button is clicked on.

Double-click on this asset to open it in the ActionList Editor. Change the first Action's type to a Player: Check Action, and connect it up to two Player: Switch Actions. Set the Current Player is and New Player fields up so that if Tin Pot is controlled, Brain will be switched to - and vice-versa. Because Brain is going to start off in another scene, set his New Player position field to Appear in other scene and configure the fields as appropriate:

However, when we switch back to a previously-active Player, we want their last state to be restored. To do this, check Restore position? in both Player: Switch Actions. The final ActionList should look like this:

When the game is played, the new button will appear in the lower-left corner - but can be restyled or moved to another Menu to suit. You can alternatively rely on separate buttons for each character.

Adventure Creator and this website are copyright Chris Burton, ICEBOX Studios

Go to top