0 votes

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)
in Engine by (18 points)

1 Answer

0 votes

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
by (112 points)

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?

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 Frequently asked questions and 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 [email protected] with your username.