How to keep player character from disappearing in first scene when going to second scene?

Godot Version

Godot v4.2.2

Question

Hi, I am new to Godot and I am currently designing a 2d game that I plan to turn into a multiplayer game. One thing I need is to keep the player’s character from disappearing from the first scene (scene A) when changing the scene in the player’s perspective to the second scene (scene B).

My intention is to make a multiplayer game where a person who changes from scene A to scene B is looking at scene B but his character still remains immobile in scene A. Many thanks for reading.

Per ChatGPT…

1. Set Up Your Player Character

First, make sure you have a player character that can be managed independently of scenes. This typically means creating a player character in a way that its state and position can be maintained separately from the scenes it resides in.

2. Use an Autoload Script for Player Management

Godot’s autoload feature allows you to create a singleton script that can manage the player character across different scenes. This ensures the player data persists between scene changes.

  1. Create a new script for managing player data. Let’s call it PlayerManager.gd.
extends Node

var player_data = {}

func set_player_position(id, position):
    if not player_data.has(id):
        player_data[id] = {}
    player_data[id].position = position

func get_player_position(id):
    if player_data.has(id):
        return player_data[id].position
    return null
  1. Add this script to Autoload:
    • Go to Project -> Project Settings -> Autoload.
    • Add the PlayerManager.gd script as a singleton by giving it a name (e.g., PlayerManager) and pointing it to the script file.

3. Update Player Position

Whenever the player moves, update their position in the PlayerManager.

# Player script
extends KinematicBody2D

var player_id = "player1"  # Unique ID for each player

func _process(delta):
    var new_position = position
    # Update position based on input or other logic
    # For example:
    new_position.x += Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
    new_position.y += Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
    
    # Set the player position in the PlayerManager
    PlayerManager.set_player_position(player_id, new_position)

4. Handle Scene Changes

When changing scenes, ensure the player character’s data remains intact in the PlayerManager.

  1. Store Player Position Before Scene Change:
# Example function to change scenes
func change_scene(new_scene):
    PlayerManager.set_player_position(player_id, position)
    get_tree().change_scene(new_scene)
  1. Restore Player Position in New Scene:
# In the new scene's script or player script
func _ready():
    var saved_position = PlayerManager.get_player_position(player_id)
    if saved_position:
        position = saved_position

5. Multiplayer Considerations

For a multiplayer game, you need to ensure that player data is synchronized across clients. This involves using a network protocol to update player positions on all connected clients.

  1. Set Up Networking:

    • Use Godot’s High-Level Multiplayer API for synchronization.
    • Define how player positions are shared between clients (e.g., via RPCs).
  2. Synchronize Player Positions:

    • Send position updates from each client to the server.
    • The server then broadcasts these updates to all clients.

Here’s a simplified example:

# On the client side
func _process(delta):
    var new_position = position
    # Update position based on input or other logic
    # ...
    # Send position to server
    rpc_id(1, "update_player_position", player_id, new_position)

# On the server side
remote func update_player_position(id, new_position):
    PlayerManager.set_player_position(id, new_position)
    # Broadcast the new position to all clients
    rpc("update_player_position_on_clients", id, new_position)

# Update position on all clients
remote func update_player_position_on_clients(id, new_position):
    if id == player_id:
        return  # Ignore own position update
    # Update position of the other player
    PlayerManager.set_player_position(id, new_position)

Summary

By using an autoload script to manage player data and handling scene changes appropriately, you can ensure the player character persists across scenes. For multiplayer functionality, synchronize player positions across clients using Godot’s networking features. This setup allows one player to be in scene B while their character remains visible in scene A to other players.

1 Like