0 votes

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

in Engine by (22 points)

1 Answer

+1 vote
Best answer

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
by (1,193 points)
selected by

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

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...

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.