How to keep character's logic process when it's off-screen

Godot Version

4.2.2

Question

I designed the behavioral tree for my enemy, designed its death logic.

var disappear: bool = false 

func tick(_delta: float) -> BehaviorNodeState:
	if character.status.health <= 0:
		if disappear:
			character.queue_free()
		return BehaviorNodeState.RUN
	return BehaviorNodeState.SUCCESS

func _on_visible_on_screen_enabler_2d_screen_exited() -> void:
	disappear = true

The tick() will be called in _physics_process(). When the return state is RUN, the next frame will continue to execute this tick(), otherwise the next logic is executed.

In my design, the death of a character removes its collision, allowing it to fall out of the screen, it will be removed after it disappears from the screen.

But I found that after leaving the screen, the character’s logic is suspended and quene_free() doesn’t trigger as expected.

This also happens with other logic, such as patrolling, where if it leaves the screen, it freezes in place and won’t return until the camera captures it again.

How do I get it to work even when I’m away from the screen? If this is an important way for Godot to optimize for performance, are there other elegant solutions?

Are you removing the Node using remove_child? Because _physics_process callback only works if the Node is not orphan (is a child of another node / is inside the scene tree)

Just in case, can you check that the tick() method is called as expected when the character is off-screen ? By printing, or debugging with a breakpoint or whatever…

EDIT : With your message, I don’t see why the character being off-screen would make it stop physics processing

Why are you using a VisibleOnScreenEnabler ? If you want to handle yourself the behaviour when the character is off-screen, don’t you want to use a VisibleOnScreenNotifier ?

I think the VisibleOnScreenEnabler is responsible for suspending your whole character logic (by modifying the node’s process mode).

1 Like