#Troubles making a Camera3D a child of a Subviewport and connecting the output to a Sprite3D in code.

13 messages · Page 1 of 1 (latest)

gloomy sierra
#

I am trying to connect the feed of a subviewport to a sprite3d after procedural generation is done. I can't have the camera be a child of the subviewport before launching the game. If I do, it causes the positioning to be lost. So, after the game is run and the map is generated, then I have to connect the camera as a child of the subviewport and connect the subviewport to the sprite3d texture. I can get it so that the subviewports are created but I can't reparent the Node3d (that holds the camera model and the camera3d) to the subviewport. I've got it to a point where the errors have stopped but now it is not creating the child of the subviewport like intended and the Sprite3D is not getting the subviewport path.

Notes regarding the code below:
The GlobalConnector.generation_complete is a Global that starts the code so that it doesn't try to find the cameras before they exist in the world.
The Security Cameras already exist as children of rooms, keeping their global positioning and such.

#

Code Part 1:
`extends Node3D

@onready var screen_1 : Sprite3D = $"Screen 1/Screen"
@onready var screen_2 : Sprite3D = $"Screen 2/Screen"
@onready var screen_3 : Sprite3D = $"Screen 3/Screen"
@onready var screen_4 : Sprite3D = $"Screen 4/Screen"
@onready var screen_5 : Sprite3D = $"Screen 5/Screen"
@onready var screen_6 : Sprite3D = $"Screen 6/Screen"
@onready var screen_7 : Sprite3D = $"Screen 7/Screen"
@onready var screen_8 : Sprite3D = $"Screen 8/Screen"

var camera_1 : Node3D
var camera_2 : Node3D
var camera_3 : Node3D
var camera_4 : Node3D
var camera_5 : Node3D
var camera_6 : Node3D
var camera_7 : Node3D
var camera_8 : Node3D

var security_room : Node3D
var connected : bool = false

Called when the node enters the scene tree for the first time.

func _ready():
security_room = get_parent().get_parent()
print("Security Room: " + security_room.name)# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):

if GlobalConnector.generation_complete and !connected:
    find_cameras()
    connected = true

func create_camera_feed(camera : Node3D, screen : Sprite3D):
#creates viewports of the cameras so the cameras have feed
var viewport = SubViewport.new()
var cctv = camera.find_child("Camera", true, true)
viewport.add_child(cctv)
camera.add_child(viewport)

screen.texture = viewport.get_texture()`
#

Code Part 2:
`func find_cameras():
#finds where the cameras are in the dungeon
camera_1 = security_room.get_parent().find_child("SecurityCameraOne", true, false)
camera_2 = security_room.get_parent().find_child("SecurityCameraTwo", true, false)
camera_3 = security_room.get_parent().find_child("SecurityCameraThree", true, false)
camera_4 = security_room.get_parent().find_child("SecurityCameraFour", true, false)
camera_5 = security_room.get_parent().find_child("SecurityCameraFive", true, false)
camera_6 = security_room.get_parent().find_child("SecurityCameraSix", true, false)
camera_7 = security_room.get_parent().find_child("SecurityCameraSeven", true, false)
camera_8 = security_room.get_parent().find_child("SecurityCameraEight", true, false)
call_deferred("connect_cameras_to_feed")

func connect_cameras_to_feed():
#Connects the cameras to feed as it is created
create_camera_feed(camera_1, screen_1)
create_camera_feed(camera_2, screen_2)
create_camera_feed(camera_3, screen_3)
create_camera_feed(camera_4, screen_4)
create_camera_feed(camera_5, screen_5)
create_camera_feed(camera_6, screen_6)
create_camera_feed(camera_7, screen_7)
create_camera_feed(camera_8, screen_8)`

#

What it is supposed to look like after generation:

#

What it actually looks like after generation:

#

The short of the situation is that I can't have settings preset connecting the Cameras, SubViewports, or the Sprite3Ds because when the map generates, it creates a copy of the room which seems to break all of the previous settings with the Inspector.

#

Troubles making a Camera3D a child of a Subviewport and connecting the output to a Sprite3D in code.

random finch
#

it will push an error into your debugger console telling you it can't do it

#

and the rest of the function won't run

#

what you need to do is either remove the child from its parent first

#

or call child.reparent(new_parent) instead, to indicate it's deliberately a change of parent

#

this is specifically about viewport.add_child(cctv) in create_camera_feed