Trying to prevent tweens from resetting when switching scenes

Godot Version

4.5.1.stable

Question

I have code that starts a tween. It shows a button with an airship icon that goes from one city to another. I have a button moving, because I eventually want to connect this button to a method someday.
I noticed that when the button is shown, it starts moving as intended. However, when I switch to a different scene, then go back to the world map, the tween doesn’t show anymore. I heard PathFollow2D is a better method, but I do not know how to assign the origin coordinate and the destination coordinate to the PathFollow2D for the button to follow it. I also do not know how to write the code where multiple PathFollow2D nodes can be made, in case the player wants to “launch” multiple airships.

Below is the code I have so far:

func create_airship_icon():
	var airship = Button.new()
	
	#Spawns new airship button in World Map
	airship.icon = load("res://sprites/Airship_icon.png")
	airship.flat = true
	var origin_vector = Vector2(map_to_local(Globals.cities[Globals.selected_city][0]))
	airship.position = origin_vector
	add_child(airship)
	
	#Replace the random selecting of cities with a menu of available cities the player can choose from
	var destination_vector = Vector2(map_to_local(Globals.cities[Globals.selected_destination_city][0]))
	var final_distance = origin_vector.distance_to(destination_vector)
	
	#Moves the spawned airship button from the origin to the destination cities
	var tween = airship.create_tween()
	tween.tween_property(airship, "position", destination_vector, final_distance * speed_multiplier)
	await tween.finished
	
	airship.queue_free()

I’m guessing the tween code has to be replaced, but I don’t know what code would work as an alternate solution?

When you switch a scene, everything from the previous scene will be gone. If you switch back, the scene will be reloaded and its state will reset back to its initial state in the tscn file.

Ah, well that…complicates things.

Is there an alternative method? I thought about using the PathFollow2D node, but I don’t know how to put the origin and destination coordinates via gdscript on that node.

You’ll need to save the relevant state either in global variables or in a file, and then re-create that state when the scene is re-loaded. Alternative is to re-pack the current state of the scene, save it as a new tscn and reload that, although tweens will probably be destroyed there as well as they are not really part of the scene, they are managed by the scene tree. Another alternative is to not swap the scene. Keep both scenes going and hide/disable one or another as needed.

You could perhaps just pause the scene and keep it open in the background. It sounds like some kind of map scene that you keep returning to as an overworld or between stages? In that case I’m not even sure it is a lazy idea - you could even be saving some computing power compared to constantly saving and reloading the same scene.

The hide/disable scene seems more straightforward to me. I updated my game by adding new nods in the main “world_map” scene and added code that toggles the visible property for the relevant nodes. The issue now however, is that I’m not seeing the new nodes when I should when I tested the game. I tried to adjust the z-index, but that didn’t seem to work for some reason.

Found the solution. Having the nodes under the same canvas layer fixed it. Then I would use gdscript to toggle visibility of each node.