How do i change scene only if multiple player instances enter a "door"

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By sprengerjan

So i made my first game where i instanced my Player character so i got two of them with the same control. The goal is to enter a door and change to a new scene = level.
The code for my door is:

extends Area2D

func _physics_process(delta):
	var door = get_overlapping_bodies()
	for body in door:
		if body.name == "Player" and "Player2":
			get_tree().change_scene("res://World2.tscn")

How do i write this function to detect Player and Player2. Now it only detects Player and ignores my Player2.
Thanks a lot for your help,
Jan

:bust_in_silhouette: Reply From: rustyStriker

if you want only one of them you will need to change the if statement to

 if body.name == "Player" or body.name == "Player2

if you want only when both of them then you will need to do something like this:

 func _physics_process(delta):
      var door = get_overlapping_bodies()
      var isOneIn = false
      for body in door:
               if body.name == "Player" or body.name == "Player2":
                           if isOneIn:
                                     # Change scene
                           isOneIn = true

thanks alot, the second scipt handled my problem.

if you have the time and read this, can you explain to me how it works? Im a bit new and i can’t figure it out.
i understand that this

if body.name == "Player" or body.name == "Player2":

if statement is true when either Player or Player2 is true.
Now

if isOneIn:

is false so it won’t execute and in the next Line isOneIn is set to True.
when it loops over the if statement again isOneIn is True and it will change the scene.

I’m not finding the code where it checks if Player and Player2 's names are checked together.

Can you explain me why this one is not working?

if body.name == "Player" and "Player2":

And when i want to add new Player instances in the future (Player3 and so on…) how do i modify the code to only work when all Players collide with the “door”

thanks a lot. I really appreciate every answer and every help

sprengerjan | 2020-05-02 10:55

It enters the if’s body when either of the players is true, but since we are looping through it we can check for only one at a time, so when we loop over the first we detected, the set our isOneIn to true(as it is currently false), when we reach the second one, if inOneIn is true we know that the first one is already detected, so we can just move to the next level…

rustyStriker | 2020-05-02 11:25