How do I reset an Enemy position when using a screen transition to scroll through the scene?

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

#Basically, I am attempting to reset an Enemy’s position when the camera moves away from the enemy. i.e (on body_exited). Going off of FORNCLAKE’s tutorial series: Godot 3.0 Zelda-like Tutorial [7] Basic Screen Scrolling.

extends Camera2D

func _ready():

$area.connect("body_entered",self,"body_entered")
$area.connect("body_exited",self,"body_exited")

func _process(delta):

var pos = get_node("../Player").global_position - Vector2(0,32)
var x = floor(pos.x / 320) * 320
var y = floor(pos.y / 256) * 256
global_position = Vector2(x,y)

func body_entered(body):

if body.get("TYPE") == "ENEMY":
	body.set_physics_process(true)
	

func body_exited(body):

if body.get("TYPE") == "ENEMY":
	body.set_physics_process(false)
:bust_in_silhouette: Reply From: aengus126

If you’re looking to detect when a node is on or off the screen, then you should use VisibilityNotifier instead. Basically it detects when a node is off or on the screen. Just add a VisbilityNotifier as a child of the enemy, connect the VisibilityNotifier’s signal, screen_exited(), with the enemy, and tell the enemy to go to the desired position when the signal is emitted.

In order to get the “desired position”, in your case, it’s original starting point, then you should create an onready var equal to the enemies position like onready var start_pos = position. Basically what that does is, as soon as the scene starts, it’s current position (also its starting position because the scene just started) is kept in a variable, which you can use later when the screen_exited() signal is emitted, like

func _on_VisibilityNotifier2D_screen_exited():
    position = start_pos

Hello! Thanks for your answer. This solution definitely has the intended effect, however there’s some sort of issue with when the VisibilityNotifier sends it’s signal. It feels like it is going off of a different set of parameters to determine what the “screen” is. Do you have any idea what might be causing that?

Meyerjc | 2022-01-04 09:51