I’m working on a game for school where you chop logs and sell them for fire wood.
I have it so that you’re able to spawn log instances when pressing “P”
var spawnlog = preload("res://Scenes/Logs/Log.tscn")
@onready var hand_right:= $Neck/Camera3D/HandRight
func _input(event: InputEvent):
if Input.is_action_just_pressed("Debug"):
var log = spawnlog.instantiate()
%Logs.add_child(log)
log.global_position = hand_right.global_position
and when you spawn a log you’re able to pick it up, place it on a stump and chop it, and when you chop it i want it to create an instance of the 2 separated half’s of the log
var loghalf1 = preload("res://Scenes/Logs/log_half1.tscn")
var loghalf2 = preload("res://Scenes/Logs/log_half2.tscn")
func _process(_delta) -> void:
if GlobalInfo.onstump == true and Input.is_action_just_pressed("Swing"):
if GlobalInfo.lookat.get_parent() == self:
GlobalInfo.lookat.get_parent().queue_free()
print("chop")
GlobalInfo.onstump = false
spawnhalf()
func spawnhalf():
var half1 = loghalf1.instantiate()
%Logs.add_child(half1)
half1.global_position = stump.get_child(1).global_position
var half2 = loghalf2.instantiate()
%Logs.add_child(half2)
half2.global_position = stump.get_child(1).global_position
whenever it try’s to spawn the log half instances it crashes my game and says Cannot call method 'add_child' on a null value
I only encountered this after removing all of the logs from my main world scene and having it so you can only spawn instances of logs, before that everything worked fine and i could chop the logs no problem.
You are trying to add a child to a scene that doesn’t exists.
Since you removed all your %Logs scenes from the main world, it doesn’t exists so you can’t call the add_child method of it.
Try adding it back or adding a placeholder node just to receive the logs half logs.
%Log is a node3d in my main scene that i use to store all the log instances, sorry if everythings so confusing, whenever i spawn a log it gets added as a child of the %Log node and the same thing goes with the log halfs. Basicly the %Log node is in my main scene always and it still gives me the error
First, try to check if your 3D Node still have the unique name %Logs. I mean, even if the unique name doesn’t exists, Godot gives you the auto complete when typing the add_child method, which can lead to a confusion.
Also you can try to add a breakpoint on it and see in the debug dock if %Logs exists in the context and also what is it’s value. You can also try to check it the “Remote” tab inside the Scene dock if it is really there and if it has the unique name.
It’s a way to start to debug it and follow the error until you find it, since as I said, the error is telling you that you are trying to call the add_child method in a object that doesn’t exists.
i checked and %Logs still has the unique name. I also checked the remote tab when running it and its still there.
Is there anything about godot that says that you can instantiate something from an instantiated scene? Because thats the only thing i can really think of.
I even tried making %Logs a global scene to see if it just couldnt find it and it still didnt work.
and its weird because it works when i want to spawn the logs with the “Debug” key but it just wont work when its trying to spawn the log halfs
Hmmm related to instantiate from an instantiated scene I don’t think there is any problem.
One more question just to make sure. Is your %Logs node inside the parent you are queueing free here?
No it is not. GlobalInfo.lookat.get_parent().queue_free() is used to find the rigid body of the indevidual log, get its parent and then delete it.
The hierarchy is basically %Logs (which is just a node3d that is used to store all the logs in the main scene), another node3d named log itself, and then the rigid body, which is being detected using a raycast. If that makes things make more sense.