Godot Version
4.3
Question
Good afternoon
Generating a scene via .gd script
Can I copy this scene directly after generation and make it into a scene with these objects ?
4.3
Good afternoon
Generating a scene via .gd script
Can I copy this scene directly after generation and make it into a scene with these objects ?
I tried this, it only saves a link to the script itself.
I haven’t figured out how to save the finished scene, its objects, camera, lights.
I’m not sure how you’re “generating” this scene. In this case it sounds like you’re trying to save the wrong resource, because ResourceSaver has a save
function that does exactly what you want it to do.
This is my code for scene generate
and i need get this scene as object and edit in 3D editor
extends Node3D
var arena_size = Vector2.ZERO # Размер арены (ширина, длина)
var tower_offset = 2 # Отступ башен от краёв
func _ready():
calculate_arena_size()
setup_camera()
setup_lights()
create_arena_floor()
place_towers()
func calculate_arena_size():
var screen_size = DisplayServer.window_get_size()
# Ширина арены = ширина экрана / 50 (подбор коэффициента)
arena_size.x = screen_size.x / 50
# Длина арены = высота экрана / 40 (чтобы не была слишком узкой)
arena_size.y = screen_size.y / 40
print("Размер экрана:", screen_size)
print("Размер арены:", arena_size)
func setup_camera():
var camera = Camera3D.new()
var half_length = arena_size.y / 2 # Половина длины арены (центр)
# Включаем ортографическую проекцию
camera.projection = Camera3D.PROJECTION_ORTHOGONAL
# Камера немного выше, чтобы вся арена помещалась в поле зрения
camera.position = Vector3(0, half_length + 15, 15) # Камера чуть выше
camera.rotation_degrees = Vector3(-60, 0, 0) # Камера смотрит вниз, но с меньшим углом наклона
# Устанавливаем размер ортографической проекции
camera.size = arena_size.y * 1.4 # Увеличиваем размер, чтобы захватить всю арену
add_child(camera)
func setup_lights():
var sun = DirectionalLight3D.new()
sun.light_energy = 1.5
sun.rotation_degrees = Vector3(-50, 120, 0) # Свет слева, падающая тень вправо
sun.light_color = Color(1.0, 1.0, 0.9)
sun.shadow_enabled = true
sun.shadow_normal_bias = 0.15
sun.shadow_bias = 0.1 # Уменьшение артефактов теней # Отключение сглаживания теней
add_child(sun)
func place_towers():
var half_width = arena_size.x / 2
var half_length = arena_size.y / 2
var tower_positions = [
# Верхняя сторона (сзади)
Vector3(0, 0, -half_length + tower_offset - 2), # Центральная верхняя (сзади)
# Верхняя сторона (впереди)
Vector3(-half_width + tower_offset, 0, -half_length + tower_offset), # Левая верхняя (впереди)
Vector3(half_width - tower_offset, 0, -half_length + tower_offset), # Правая верхняя (впереди)
# Нижняя сторона (сзади)
Vector3(0, 0, half_length - tower_offset + 2), # Центральная нижняя (сзади)
# Нижняя сторона (впереди)
Vector3(-half_width + tower_offset, 0, half_length - tower_offset), # Левая нижняя (впереди)
Vector3(half_width - tower_offset, 0, half_length - tower_offset) # Правая нижняя (впереди)
]
print("Координаты башен:", tower_positions)
for pos in tower_positions:
var tower = create_tower()
tower.position = pos
add_child(tower)
print("Добавлена башня в:", pos)
# Проверка Y-координаты башен
if tower.position.y > 0:
print("⚠ Внимание! Башня выше уровня пола: ", tower.position.y)
func create_tower():
var tower_scene = preload("res://scenes/Tower.tscn") # Загружаем сцену Tower
var tower = tower_scene.instantiate() # Инстанцируем сцену
return tower
func create_arena_floor():
# Создаем MeshInstance3D для плоскости
var floor = MeshInstance3D.new()
# Создаем плоскость
var plane = PlaneMesh.new()
floor.mesh = plane
# Масштабируем плоскость под размер арены
floor.scale = Vector3(arena_size.x, 1, arena_size.y) # Ширина, высота и длина
# Позиционируем плоскость немного ниже уровня башен
floor.position = Vector3(0, 0, 0)
# Добавляем материал с цветом (например, зеленый фон)
var material = StandardMaterial3D.new()
material.albedo_color = Color(0.2, 0.5, 0.2) # Зеленый цвет
floor.set_surface_override_material(0, material)
add_child(floor)
I’m sure I remember reading something about needing to set the owner of each created node to the scene root for them to be saved. Maybe give that a try?
Sish
I dont seen anywhere this information
But it helped
Big thanks!
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.