Getting player coordinates from different index

Godot Version

4.3

Question

Hi, I am trying to create a 2 player local multiplayer game where one player controls using wasd and the other uses arrow keys. I am trying to have player movements for both players in one script and am having issues using separate data from each player.

How can I separate data from each player such as Player 1 coordinates vs Player 2 coordinates. I am using @export var player_index := 0, and setting a number to each 2d Player character to separate them but it doesn’t do anything in code. My end goal right now is to find the distance between both characters at all times, but cannot when the coordinates come together, unless I am overlooking something. I want to have player 1 and player 2 separate to call data from, but cannot seem to do this when using the same script.

help

Can you show your script?

Properties should be separate between instances, especially position. References to Resources usually trick new developers but again position isn’t a Resource.

For getting the distance between players I’m not sure why you would need a player_index, I would use an @export to get the other player and calculate distance from that

extends CharacterBody3D
class_name Player

@export var other_player: Player

func _ready() -> void:
    var distance: float = global_position.distance_to(other_player.global_position)
    print("Players are %d meters apart" % distance)

Thank you. I was just getting confused with some yt tutorials and other examples I had seen that used indexes in their scripts