Page 1 of 1

How to save a scene ?

Posted: Mon Jul 24, 2023 8:44 am
by EricRogerGarcia
Hello,

In my project, the main scene is made by code.

To make some tests, I would need to be able do open the scene made in the game so, I would like to know how I could save it by code (to be able to open it in the editor later).

:)

Re: How to save a scene ?

Posted: Mon Jul 24, 2023 9:17 am
by EricRogerGarcia
I tried this :

var sceneLevel1 = self.duplicate()
print(ResourceSaver.save(sceneLevel1,"res://sceneLevel1.tscn"))

It doesn't work and print : 31

Re: How to save a scene ?

Posted: Mon Jul 24, 2023 3:26 pm
by stayathomedev
EricRogerGarcia wrote: Mon Jul 24, 2023 8:44 am In my project, the main scene is made by code.
Could you describe this process a little more? What is being made in the scene?

My first thought was if you could run the scene scripts you need as @tool to test it that way, rather than running the game and trying to grab the scene.

Re: How to save a scene ?

Posted: Mon Jul 24, 2023 3:46 pm
by EricRogerGarcia
I'm making a pac man of sort
the levels are made with an external csv file
So the map nodes are made from existing scenes (walls, players, enemy and so on)
(details on my homepage supra)

I'd like to grab the scene made to make some tests in the editor
I'm sure it's possible.
It'd be useful for any rogue like game...

I found this : https://docs.godotengine.org/en/4.1/cla ... escription
but I didn't manage to understand it all yet !
:)

Re: How to save a scene ?

Posted: Tue Jul 25, 2023 9:34 am
by svdragster
EricRogerGarcia wrote: Mon Jul 24, 2023 3:46 pm
I found this : https://docs.godotengine.org/en/4.1/cla ... escription
but I didn't manage to understand it all yet !
:)
Hi, could you please explain what parts of it don't you understand?
The following code is untested. However, for example if you have a node called "TheLevel", it will pack it and save it to a level1.tscn file. Then you can open the level1.tscn file in the Godot editor.

Code: Select all

var node = get_node("/root/TheLevel")
var scene = PackedScene.new()

var result = scene.pack(node)
if result == OK:
    ResourceSaver.save(scene, "res://level1.tscn")

Re: How to save a scene ?

Posted: Tue Jul 25, 2023 10:05 am
by EricRogerGarcia
Thanks a lot
It works... and it doesn't ! :(

something strange happens : I can open the scene just recorded in the editor but... the nodes added by the program code are not in it !
I have the same sceneTree than in the original scene

I didn't expect than !!!!

well, I'm going to make my test scene the old fashioned way !!!

Re: How to save a scene ?

Posted: Tue Jul 25, 2023 10:10 am
by svdragster
Maybe you have to set the owner of each node when creating them.

Code: Select all

node.owner = your_root_node
It could help if you show us the code where you create the nodes.

Re: How to save a scene ?

Posted: Wed Jul 26, 2023 9:00 pm
by stayathomedev
svdragster wrote: Tue Jul 25, 2023 10:10 am Maybe you have to set the owner of each node when creating them.

Code: Select all

node.owner = your_root_node
It could help if you show us the code where you create the nodes.
Ditto on this...had to do this in my Prototype plugin to get the nodes to show up in the tree.

Re: How to save a scene ?

Posted: Wed Jul 26, 2023 9:52 pm
by EricRogerGarcia
( I haven’t had much time these last few days)

The last version code :

Code: Select all

extends Node

func _ready():
	var data = preload("res://data/csv/level1.csv")
	var position : Vector2 = GlobalGame.POSITIONHOME
#	data.records : array of data
	var wallPackedScene = load("res://scenes/objets/wall.tscn")
	var playerPackedScene = load("res://scenes/player/player.tscn")
	var pointPackedScene = load("res://scenes/objets/point.tscn")
	var groundPackedScene = load("res://scenes/objets/ground.tscn")
	var pesticidePackedScene = load("res://scenes/pesticides/pesticide.tscn")
	for x in data.records : # lines
		for y in x: # columns
			# placement of object to position: position
			if y.contains("X") or y.contains("D") or y.contains("G"):
				# wall placement
				var wallScene = wallPackedScene.instantiate()
				$NavigationRegion2D/walls.add_child(wallScene)
				wallScene.owner = $NavigationRegion2D/walls
				wallScene.position = position
			else:
				if y !="" and y!="B":
					var groundScene = groundPackedScene.instantiate()
					$NavigationRegion2D/ground.add_child(groundScene)
					groundScene.owner = $NavigationRegion2D/ground
					groundScene.position = position
				if y.contains(".") :
					var pointScene = pointPackedScene.instantiate()
					$NavigationRegion2D/points.add_child(pointScene)
					pointScene.owner = $NavigationRegion2D/points
					pointScene.position = position
				if y.contains("F") :
					var pesticideScene = pesticidePackedScene.instantiate()
					$NavigationRegion2D/pesticides.add_child(pesticideScene)
					pesticideScene.owner = $NavigationRegion2D/pesticides
					pesticideScene.position = position
			if y.contains("P") : # player
				var playerScene = playerPackedScene.instantiate()
				$NavigationRegion2D/player.add_child(playerScene)
				playerScene.owner = $NavigationRegion2D/player
				playerScene.position = position
				
			position += Vector2(GlobalGame.WIDTH,0)
		position.x = GlobalGame.POSITIONHOME.x
		position += Vector2(0,GlobalGame.WIDTH)
	GlobalGame.myReady = true
	
	var rootnode = self # level1 the root node
	var scenetoSave = PackedScene.new()
	var result = scenetoSave.pack(rootnode)
	if result == OK:
		ResourceSaver.save(scenetoSave, "res://level1-full.tscn")
The sceneTree of "level1" (the main scene)

Image

level1-full.tscn is like level1.tscn !

I miss something !!!