How do i instance a object from another scene in my current scene?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Skizi

So im trying to make the player grab a flashlight and then the flashlight to be on.

What i have now is a model for the flashlight which you can interact with:
class_name Flashlighti
extends StaticBody

var player = null

func interact(body):
player = body

And then the interact script in physics process:
if detected is Flashlighti:
prompt.text = “F”
if Input.is_action_just_pressed(“Interact”):

And then as a child of head/camera a spotlight,i want this spotlight to be instanced in my flashlight model but it doesnt work.These are the ways i tried it:
-instancing the player,this works but it doubles up the player and plays double sounds etc.
-onready var flash = $“res://Player/Player.tscn/Player/Head/Camera/Flashlight” doesn’t turn on the flash
-onready var flash = get_node(“res://Player/Player.tscn/Player/Head/Camera/Flashlight”) also doesn’t work1

:bust_in_silhouette: Reply From: Cam

Use the preload function to load the scene you want to instance:

onready var flash = preload("res://Player/Flash.tscn")

Then you have to instance it, and add it as a child:

var flash_instance = flash.instance()

You need to call add_child on the node you wish to add the flash to. In order to use that syntax for referencing the flashlight, right click Flashlight in the scene tree and select “Access as scene unique name”.

My only question is, why do you need to instance the flash for the flashlight in the player, does it not work to have the flash be always attached to the flashlight, but with the light intensity set to 0 unless the flashlight is turned on?

I tried that but the flash doesnt stay attached to the player,also error(4,85): Can’t preload resource at path: …

Skizi | 2023-02-05 07:28

That is my mistake, I did not read the path very carefully. To my knowledge that will not work. The scene you want to instance should be saved as a scene. I will edit the post to have a hypothetical path as an example. I am also now confused about the question you are asking. Do you want to add a .tscn file as a child of a node while the game is running? Or do you want to change the properties of a node? If you can clarify exactly what you want to do by editing your post, I can update the answer.

Cam | 2023-02-05 09:05

i want to control the flashlight under the player scene in the flashlight model scene.Basically i want to turn on the flashlight attached to the player to turn on after you picked up the flashlight model from the ground

Skizi | 2023-02-05 14:32