I asked this question on GodotEngine QA and 20 hours later and my question still has not been pushed through, so I have come to this outstanding community! :)
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)
#spawn_item.add_to_group("spawned_items")
get_node("ItemsSpawned").add_child(spawn_item)
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, in this new game with the SAME GODOT VERSION, an Item is spawned in, the random position it is set 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 the item is added to the tree, this is the output I printed with the random position:
Random Set Position: -299.876, -11.543
Then, after it enters the scene tree, its position is reset and the global position is the same:
Random Set Position: 0, 0
Global Position: 516, 78 <-- Not the exact numbers, but they appear the same everytime and right around here
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!
Godot version 3.5 Stable
UPDATE::
After more checking this morning and adjusting where scenes and nodes are loaded, I am still faced with the issue. Below, I have taken the opportunity to print the random coordinates that are generated for the item BEFORE it spawns, and the position AFTER it is added to the tree:
NOTE -- Each entry is a different random item. I know they are different as the Area2D name is different, and the sprites are all different when added to the tree.
Set Position: (155.005737, -191.537476)
Added Position: (0, 0)
Set Position: (76.335258, 356.229767)
Added Position: (0, 0)
Set Position: (106.081261, 126.82753)
Added Position: (0, 0)
Set Position: (-310.06958, -194.428818)
Added Position: (0, 0)
Set Position: (132.235092, -145.846939)
Added Position: (0, 0)
Set Position: (13.039706, 332.876984)
Added Position: (0, 0)