Weird behaviour when setting position of object after changing scene by removing it

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

In my 2D space game I’m instanciating a generated system-scene when the player-ship enters a specific area and add it to the world node. Once the player leaves this scene by moving to a specific distance I remove the scene and set the player’s position to it’s entering position (previously saved by a singleton).

(this way I can open and close systems from the game-world pretty easy without losing important references)

There are 2 possible outcomes:
→ when I add print(String(singleton.player_ship.global_position)) after setting it’s position it works

→ when I don’t add it, setting it’s position to it’s entering position doesn’t work

This kinda confuses me a lot :confused:
Does anyone know why this problem might occures?

A print statement shouldn’t affect your code. Can we see the rest of your code?

exuin | 2021-04-17 22:00

Sure :smiley:
I might have overused singletons (GameManager,ObjectManager and SceneManager)
So when I comment out the last print statement the ship stays at it’s position

func _process(delta):

if (ObjectManager.player_ship.position-GameManager.selected_system.rect_position).length() > world_length and !exit:
	print("exiting system")
	exit = true
	
	ObjectManager.player_ship.global_position = GameManager.ship_entering_position
	print("ship position: %s" % String(ObjectManager.player_ship.global_position))
	SceneManager.close_system()

I use exit as bool so I’m sure it only get’s called once,world_lenght is just an int and SceneManager.close_system() calls remove_child(system_scene)

Panzer0312 | 2021-04-17 22:18