Can i somehow only create one "trap" in my game?

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

Hello,

so i recently started to create my first game with Godot. Today i stumbled across an issue that i would like to move out of my way. So here comes the explanation:

I have 2 functions that kill the player on contact. Those 2 functions go on 2 circular saws each.

func _on_Movable_Saw_1_body_entered(body):
if body.has_method("death"):
	var player = get_parent().get_node("Player")
	$Light2D.energy = 0
	player.position = Vector2(0, 0)
	player.death()

func _on_Movable_Saw_2_body_entered(body):
if body.has_method("death"):
	var player = get_parent().get_node("Player")
	$Light2D.energy = 0
	player.position = Vector2(0, 0)
	player.death()

I need to seperate them because when i duplicate the first circular saw the collision on the duplicated one wont work. Is there a way to just create one saw object that i can duplicate over and over again?

:bust_in_silhouette: Reply From: TchnlgPsnt

If you right click on the saw’s root node in the scene panel there should be an option like save branch as scene. This will save your saw as a scene file which can be created from that file in any other scene. Kind of like a prefab if you’re coming from unity. If you make a change to the saw’s scene file, that change will be applied to any instance that is being created from that file. But if you make a change to one of the instances, it will only affect that one saw.

You can edit this scene file just like any scene file, so open it up. Connect the on_body_entered signal to the function on itself. Now any instance of this saw will call that function on itself.

Click on the chain link icon on top of the scene panel to display all your scenes. Click the saw scene file to create an instance of it in your current scene.

Thanks for your answer it is working ^^

illnesslilin | 2021-01-24 11:42