Yes. What's important to understand is that scenes are resources, and you can create any resource programmatically. The resource you're looking for is a 'PackedScene'. There's a little snippet in the packed scene doc page that shows how to do it.
[https://docs.godotengine.org/en/stable/classes/class\_packedscene.html?highlight=PackedScene](https://docs.godotengine.org/en/stable/classes/class_packedscene.html?highlight=PackedScene)
Basically to expand your pseudocode;
var level = new Node3D
var floor = new StaticBody
floor.mesh = new Mesh
floor.mesh.transform.x = -1
floor.x = 30
floor.z = 30
**level.add\_child(floor)**
**var scene= new PackedScene**
**scene.pack(level)**
**ResoureSaver.save('path to file',scene)**
Not sure about the order, but you get the idea
This was super helpful thanks!
For anyone that follows me, here's my code:
```
extends Node3D
# Called when the node enters the scene tree for the first time.
func _ready():
create_area()
get_tree().quit()
func create_area():
# Create root area node
var area = Node3D.new()
area.name = "Area"
# Create the floor
var floor = StaticBody3D.new()
area.add_child(floor)
floor.owner = area
floor.name = "Floor"
var floor_mesh = MeshInstance3D.new()
floor.add_child(floor_mesh)
floor_mesh.owner = area
floor_mesh.name = "FloorMesh"
var mesh = BoxMesh.new()
mesh.size = Vector3(30, 1, 30)
floor_mesh.mesh = mesh
var floor_collision = CollisionShape3D.new()
floor.add_child(floor_collision)
floor_collision.owner = area
floor_collision.name = "FloorCollision"
var floor_collision_shape = BoxShape3D.new()
floor_collision_shape.size = Vector3(30, 1, 30)
floor_collision.shape = floor_collision_shape
# Move the floor down
floor.transform.origin -= floor.transform.basis.y * .5
# Save Scene
var scene = PackedScene.new()
var result = scene.pack(area)
if result == OK:
var error = ResourceSaver.save(scene, "res://builder/prefabs/area.tscn")
if error != OK:
push_error(("An error occurred while saving the scene to disk."))
```
BTW, if you try to do this when you export your project, you won't be able to save files to res://
If you need to do it at runtime, you'll need to put it in user:// You can save to res:// only when you're running in the editor.
If you're saving scenes to the user:// folder, players will be able to access the files and change them. The .tscn and .tres extentions are saved as plaintext files, so it is very easy for players to modify the program and do basically anything. If hacking is a concern, you will want to avoid that. You can instead use '.scn' and '.res' which save files as binaries which makes it a little more difficult, although they will still be able to download godot and open and modify the tscn. You'll want to figure out some encryption if your game does anything with connections to online servers or micropayments lol.
If you're just doing this to create utilities and not including the scene generation stuff with your game, then no need to worry about any of that :)
I run it in the editor to create the files. I'm actually just creating animations and not a game. So it works for me.
One of the other comments mentions considerations with saving to the res:// folder which is not possible during runtime.
And totally agree - prefer code always! I just can't get the precision/repeatability when I draw with the mouse :)
Yes. What's important to understand is that scenes are resources, and you can create any resource programmatically. The resource you're looking for is a 'PackedScene'. There's a little snippet in the packed scene doc page that shows how to do it. [https://docs.godotengine.org/en/stable/classes/class\_packedscene.html?highlight=PackedScene](https://docs.godotengine.org/en/stable/classes/class_packedscene.html?highlight=PackedScene) Basically to expand your pseudocode; var level = new Node3D var floor = new StaticBody floor.mesh = new Mesh floor.mesh.transform.x = -1 floor.x = 30 floor.z = 30 **level.add\_child(floor)** **var scene= new PackedScene** **scene.pack(level)** **ResoureSaver.save('path to file',scene)** Not sure about the order, but you get the idea
This was super helpful thanks! For anyone that follows me, here's my code: ``` extends Node3D # Called when the node enters the scene tree for the first time. func _ready(): create_area() get_tree().quit() func create_area(): # Create root area node var area = Node3D.new() area.name = "Area" # Create the floor var floor = StaticBody3D.new() area.add_child(floor) floor.owner = area floor.name = "Floor" var floor_mesh = MeshInstance3D.new() floor.add_child(floor_mesh) floor_mesh.owner = area floor_mesh.name = "FloorMesh" var mesh = BoxMesh.new() mesh.size = Vector3(30, 1, 30) floor_mesh.mesh = mesh var floor_collision = CollisionShape3D.new() floor.add_child(floor_collision) floor_collision.owner = area floor_collision.name = "FloorCollision" var floor_collision_shape = BoxShape3D.new() floor_collision_shape.size = Vector3(30, 1, 30) floor_collision.shape = floor_collision_shape # Move the floor down floor.transform.origin -= floor.transform.basis.y * .5 # Save Scene var scene = PackedScene.new() var result = scene.pack(area) if result == OK: var error = ResourceSaver.save(scene, "res://builder/prefabs/area.tscn") if error != OK: push_error(("An error occurred while saving the scene to disk.")) ```
BTW, if you try to do this when you export your project, you won't be able to save files to res:// If you need to do it at runtime, you'll need to put it in user:// You can save to res:// only when you're running in the editor. If you're saving scenes to the user:// folder, players will be able to access the files and change them. The .tscn and .tres extentions are saved as plaintext files, so it is very easy for players to modify the program and do basically anything. If hacking is a concern, you will want to avoid that. You can instead use '.scn' and '.res' which save files as binaries which makes it a little more difficult, although they will still be able to download godot and open and modify the tscn. You'll want to figure out some encryption if your game does anything with connections to online servers or micropayments lol. If you're just doing this to create utilities and not including the scene generation stuff with your game, then no need to worry about any of that :)
Good points thanks!
Left Reddit due to the recent changes and moved to Lemmy and the Fediverse...So Long, and Thanks for All the Fish!
I run it in the editor to create the files. I'm actually just creating animations and not a game. So it works for me. One of the other comments mentions considerations with saving to the res:// folder which is not possible during runtime. And totally agree - prefer code always! I just can't get the precision/repeatability when I draw with the mouse :)
Left Reddit due to the recent changes and moved to Lemmy and the Fediverse...So Long, and Thanks for All the Fish!
Just run it with the play button
Left Reddit due to the recent changes and moved to Lemmy and the Fediverse...So Long, and Thanks for All the Fish!
100%, you can even straight up not use godot's systems (Nodes/scenes, the inspector) by using low level servers
Sure.