Area2D set position is reset each time it enters the scene

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

Okay… so I have released one game where-in, after a timer is complete, a random enemy instance is created and added to the scene.

The way this is accomplished is:

  • RANDOMIZE() the ENTIRE level on ready() each time the level is loaded
  • Grabbing nodes in a group with title “it_spawn”
  • Picking one of the random nodes from above ( it is an Area2D )
  • Getting the position of the Area2D CollisionShape and its extents
  • Generate a random X and Y position from the extents of the CollisionShape
  • Set the position, then add the instance to the scene under a Node2D with the named of “ItemsSpawned”

This is the code:

var spawn_boxes = get_tree().get_nodes_in_group("it_spawn")
var random_spawn_area = spawn_boxes[randi() % spawn_boxes.size()]
var random_spawn_holster = random_spawn_area
var random_spawn_node = random_spawn_area.get_node("SPAWNER")
var random_area_center = random_spawn_holster.position
var random_area_edges = random_spawn_node.shape.extents
var random_area_width = random_area_edges.x
var random_area_height = random_area_edges.y
var random_x_high = random_area_center.x + random_area_width
var random_x_low = random_area_center.x - random_area_width
var random_y_high = random_area_center.y + random_area_height
var random_y_low = random_area_center.y - random_area_height
spawn_item.position.x = rand_range(random_x_high, random_x_low)
spawn_item.position.y = rand_range(random_y_low, random_y_high)

Now, I KNOW this code works, as it has been released in a previous game that utilizes the same item spawn feature and it works like a charm. AND, the Enemy spawn is THE EXACT SAME CODE and works 100% of the time.

But every time an Item is spawned in, its position that I set it with is reset to (0,0) and it spawns in the exact same spot with every random item. The enemies spawn all over the place as they are supposed to, but the items don’t follow in kind and their global position is the exact same with each spawn;

Before it is added to the tree, the output prints this:

Random Set Position: -299.876, -11.543

And after its added to the scene tree, its position looks like this, and its Global Position is the same.

Random Set Position: 0, 0
Global Position: 516, 78 *<-- Not the exact numbers, but they appear the same everytime*

I am at a complete loss of what to do at this point and its brought things to a grinding halt. I have researched for a couple of days and nothing seems to alleviate the issue at all.

Again, the code WORKS 100% in an already released game ( same Godot 3.5 stable version ), and when I ported it over, the variable name adjustments were the only changes.

Much appreciated for any assistance!

:bust_in_silhouette: Reply From: HandiQuackStudios

Found the culprit! For future references:

Each item has its own AnimationPlayer, the thing that made it hover and spin and stuff. This animation player uses keyframes on specific aspects of any part of the tree ( everything that makes up the item ).

I was animating the WHOLE ITEM ( Area2D, CollisionShape2D, AnimationPlayer ) instead of just the sprite. Why is this bad?

The animation was starting the position of the Area2D node to (0, 0) for the idle animation…

So everything was working exactly as it should have. I doinked up and was animating the entire item scene, instead of just animating the sprite within the scene.