Repository: godot
Issue: PackedScene API outside the editor overrides all child node properties
Labels: bug,topic:core

### Godot version

3.5

### System information

Linux Mint

### Issue description

Packing a scene via the PackedScene API in a running game produces an incorrect serialization of the scene which differs from saving a scene in the editor.

The incorrect serialisation does not properly compute what is overriden by instances and incorrectly writes every property into the .tscn. This makes it so changes to the scenes that are instanced in this incorrectly saved scene will not apply to this incorrect scene.

If you view this incorrect scene in the editor and then save it in the editor, the editor will repair this incorrect serialization.

Properties in a Child Scene which are not changed in a Parent Scene should not be present in Parent.tscn, or else it makes it so Parent is _overriding_ Child. The editor serializes correctly but the PackedScene api at runtime does not.

Note: This work is being done to create a level editor where players can make levels in game, using nodes that we provide. We need this code to work to allow us to update those nodes without having to reserialize every player created level.

### Steps to reproduce

Create a Scene, and set a property (like sprite.texture) call it Child in the editor and save it to the project

Write a script which creates a node called Parent, instances a Child and saves Parent via the PackedScene API

View Parent.tscn, note that all properties (eg: sprite.texture) are being saved into the .tscn. This is incorrect as some of these properties are defined in Child and are not overriden in Parent.

Change some of these properties in Child.

Load the parent via script, (as to be sure not to have the Editor reserialize Parent.tscn). Note that the instances of Child.tscn have not been updated to reflect the changed property.

Make a copy of Parent.tscn, open it in the editor and hit save in the editor and you will see how it _should_ be serialized. Specifically properties that are in Child.tscn but not overriden in the Parent scene are not present in Parent.tscn file and now properly reflect future changes made to Child.tscn

### Minimal reproduction project

[PackedSceneTest.zip](https://github.com/godotengine/godot/files/9863788/PackedSceneTest.zip)


Normal, correct scene created in the editor:

```
[gd_scene load_steps=3 format=2]

[ext_resource path="res://BasicScript.gd" type="Script" id=1]
[ext_resource path="res://NestedScene.tscn" type="PackedScene" id=3]

[node name="Node2D" type="Node2D"]

[node name="Node" type="Node" parent="."]
script = ExtResource( 1 )

[node name="NestedScene" parent="." instance=ExtResource( 3 )]
```

That same scene loaded and saved at runtime, with incorrectly computed overriden properties:
```
[gd_scene load_steps=4 format=2]

[ext_resource path="res://NestedScene.tscn" type="PackedScene" id=1]
[ext_resource path="res://icon.png" type="Texture" id=2]
[ext_resource path="res://BasicScript.gd" type="Script" id=3]

[node name="Node2D" type="Node2D"]

[node name="Node" type="Node" parent="."]
script = ExtResource( 3 )
some_property = 1

[node name="NestedScene" type="Sprite" parent="." instance=ExtResource( 1 )]
self_modulate = Color( 1, 0, 0, 1 )
texture = ExtResource( 2 )
```

The code to load and save the scene at runtime to produce the incorrect serialization:

```
var instanced_node = scene.instance()
get_tree().root.add_child(instanced_node)
var packed_scene := PackedScene.new()
packed_scene.pack(instanced_node)
ResourceSaver.save("res://scene_saved_at_runtime.tscn, packed_scene)
```