How to recycle objects

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

I am developing a 2d side scrolling game. when the game starts I put two instance of platform.tscn so the player can run on it. What I want to do is, when player reaches 30% of the second platform I want to re-position the first platform next to second platform and so on.
In c# (i am basically from .net background) I would create a queue and put these two platform in it and recycle them using enqueue and dequeue operations.
How to implement a recycling mechanism in gdscript?

:bust_in_silhouette: Reply From: Warlaan

I am no Godot expert (yet), but here are some suggestions:

  • Use a node to store the platforms as subnodes. Using move_child() you can rearrange the subnodes so the first node moves to the end.
  • Use the Array class, which in gdscript is basically a deque (double-ended queue, it has both pop_front()/push_back() and push_front()/pop_back()).
  • Use an Array but instead of pushing and popping entries just store an index that points to the next platform to be used, effectively turning the Array into a ring buffer.
  • Why store the platforms in a list? Just place a despawn-area to the left of the screen and move any platform that touches it a certain distance to the right. “reposition the first platform” is one way of putting it, but “reposition any platform that has left the screen” is another one that should work equally well.