What is the status of objects outside of the viewport?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Nichefect
:warning: Old Version Published before Godot 3 was released.

I’m building an automatic side-scrolling arcade style 2D shooter much akin to Section Z from the NES. The viewport scrolls horizontally through the map and only a small portion of the map is ever in the viewport at time. I’m wondering if there is any type of memory management going on for the objects not currently in the viewport, or if there is something I should be doing to address those objects until they enter the screen.

Both are very helpful answers from Zylann and Ray Koopa. Based on them I came up with this as a strategy:

Objects that haven’t arrived in the viewport yet could be locked by a conditional like

  if(isWithinViewport):  
      Do everything
  Else: 
      Do nothing but exist

And objects that have passed by the viewport can be released with

 if(hasPassedViewport):
      hide()
      queue_free()

Nichefect | 2016-05-01 20:29

An update on what I came up with. Within my Main scene container I have different sub-containers to hold instances of their respective enemies. The sub containers script contains these 2 functions executed in _process

#Removes children who have passed the viewport
for i in range(get_child_count()):
if(get_child(i).get_pos().x < -get_viewport().get_canvas_transform()[2].x):
get_child(i).queue_free()
print(“ENEMY REMOVED”)

#Activates children that enter the viewport
for i in range(get_child_count()):
var viewportX = -get_viewport().get_canvas_transform()[2].x
var viewportWidth = get_viewport().get_rect().size.width
var childX = get_child(i).get_pos().x
if(childX < viewportX + viewportWidth and !get_child(i).is_fixed_processing()):
get_child(i).set_fixed_process(true)
print(“ENEMY ENTERS THE VIEWPORT”)

Performance has improved significantly.

Nichefect | 2016-05-02 17:06

:bust_in_silhouette: Reply From: Zylann

Objects that are not visible will not be drawn, however they still exist and are _process()-ed if requested in GDScript.
If performance is an issue (FPS drop, CPU load…), I would recommend removing portions of the scene that are no longer in use, or deactivate them somehow.

:bust_in_silhouette: Reply From: sergicollado

I usually use visibilityNotifier node, you can connect this one to enter_screen signa l and then activate process http://docs.godotengine.org/en/latest/classes/class_visibilitynotifier2d.html