how to control multiple characters with one script?

Godot Version

4.3

Question

I want to make multiplayer minigames and write all the players actions in each level node script instead of characters script
Scene looks like this:

  • level scene (node2d)
    • Player_1 (CharacterBody2D)
    • Player_2 (CharacterBody2D)

So how can i write the code below in level scene (node2d) script but affect each of Players (CharacterBody2D):

if not is_on_floor():
velocity += get_gravity() * delta

Add a script to level_scene, then export player variables:

extends Node2D

@export var player_1 : CharacterBody2D = null
@export var player_2 : CharacterBody2D = null
# You get the idea.

Set player_1 and player_2 via the inspector.

Now you can process the players’ physics from the parent node:

func _physics_process(delta : float)->void:
	if not player_1.is_on_floor():
		player_1.velocity += get_gravity() * delta

	if not player_2.is_on_floor():
		player_2.velocity += get_gravity() * delta
	
	# Hopefully you get the idea.

That’s how you could do it. But I don’t understand why would want to. Could you share why you think this is necessary? I suspect there might be a XY problem situation going on here.

1 Like

i want to make a bunch of little minigames with different types of actions and controls (platformer, topdown, racing mechanics etc.) so i assumed that it would be more convenient to write each of levels features in level script instead of having all of this different control variations in players script