Can't spawn item by function

Godot Version

4.2.1 stable

Question

Hello :slight_smile:
I’m new to Godot and try to get some basics, but I can’t answer to my question.

I try to spawn object to scene by mouse clicking.
But unfortunately it doesn’t work for me.

I have main scene (World.tsc) and here I have a script

var road = preload("res://Main/Resources/Road.tscn")
func make():
	var instance = road.instantiate()
	instance.global_position = Vector3(0, 0, 1)
	add_child(instance)

I call it from camera script

extends Camera3D

var mouse = Vector3()
var createRoad = preload("res://Main/createRoad.gd").new()

func _input(event):
	if event is InputEventMouse:
		mouse = event.position
	if event is InputEventMouseButton and event.is_pressed():
		if event.button_index == MOUSE_BUTTON_LEFT:
			spawnRoad()
			
func spawnRoad():
	var position2D = get_viewport().get_mouse_position()
	print(mouse)
	createRoad.newPosition = mouse
	createRoad.make()

Unfortunately it don’t do anything. I can make breakpoint on it(I know I don’t use mouse position, but I remove that to test).
When I try for example using that i main script

func _physics_process(delta):
	if Input.is_action_just_pressed("left_click"):
		var instance = road.instantiate()
		instance.global_position = Vector3(0, 0, 1)
		add_child(instance)

It’s works okey
When I try call make() func in _ready() it’s works too.

How can I fix that and spawn object by clicking on screen from Camera script?

Thanks for answer in advance :slight_smile:

Does the problem lie in you being unable to call spawnRoad() or is the code executed but nothing is added to the sceneTree?

I don’t understand what var createRoad = preload("res://Main/createRoad.gd").new() is, I thought you want to call the main scene script?

  • add this to Camera script @export var root: Node
  • Go into the editor and assign your root node of World.tscn as root for camera
  • edit your make() method into make(position)
  • edit your spawnRoad() method to
func spawnRoad():
	var position2D = get_viewport().get_mouse_position()
	root.make(position2D)

if you know your camera is always the direct child node of your World.tscn main scene, then you can also go with get_parent().make(position2D)

1 Like

spawnRoad() is called and I can read variable value.
So it nothing added to sceneTree

createRoad.gd is connected to main World scene

If I understand this correctly, World.tscn is a root for camera

Zrzut ekranu 2024-09-02 214215

Or you mean something else?

When I add @export var root: Node and try call root.make I get Invalid call. Nonexistent function 'make' is base 'Nil'

When I add @export var root: Node and try call root.make I get Invalid call. Nonexistent function 'make' is base 'Nil'

Did you then click on Camera3D and assigned the root variable to be your World node3d?

grafik
it should look similar to this. Click on Assign and then choose the World

1 Like

It’s working now after I add Root to camera.gd
But I have one question.
You StageHandler is some your custom interface, or this is common in Godot?
Because I have it here
image

Thank you very much for you patience :slight_smile:

StageHandler is a node I created for my project. You can create your custom classes by adding
class_name YourClass to your script

1 Like