Rather than have the projectile be a child of the player and then duplicate that, why not instead have a ProjectileManager
node that is a child of the player.
The projectile manager node would be responsible for creating new projectile instances. You mentioned that you already have your projectile saved as a separate scene so to me this would potentially be a better solution maybe?
So with the projectile manager you can either read input from the player (a button press for example) to trigger the projectile and then create an instance or you could handle input somewhere else but make it call a function on your input manager, something like fire_projectile
maybe.
So to do all of this you would load your Projectile
scene in your ProjectileManager
and then create an instance of it when the fire_projectile()
function is called.
# This assumes that you have a saved scene called 'Projectile.tscn' in a folder called 'scenes'
onready projectile_scene = load("res://scenes/Projectile.tscn")
func _physics_process(delta):
# Assume input logic here to trigger the fire_projectile function?
# Alternatively you can handle you trigger input elsewhere and simply call fire_projectile from outside this code.
func fire_projectile():
# create an instance of our Projectile scene
var current_projectile = projectile_scene.instance()
# Add the new instance as a child of ProjectileManager
add_child(current_projectile)
Now the actual projectile itself would handle the logic for removing itself from the game world/memory by calling queue_free()
on itself on whatever condition/logic is met that you use to remove it.
# This should be in the Projectile.tscn code
# I assume your code for moving the projectile each physics step is in _physics_process()
func _physics_process(delta):
position += projectile_velocity # psuedocode for moving the projectile each frame
# Set up a signal in your projectile scene that links to itself and this function when it hits something
func _hit_something():
# whatever logic you want to happen when you hit something goes here
queue_free() # Delete the projectile instance and remove it from memory
Granted I have not tested the above code and you may need to adapt it for your use case but I think this should allow all of your projectiles to be created as separate instances without interfering with each other. The basic idea should apply.
Something to think about is also the different conditions/scenarios for when you want your projectile to delete itself.
- Should it delete itself upon impact with an enemy, wall or other object?
- Is it a special projectile that you want to move through multiple enemies?
- Do you want to delete it once it reaches a certain distance away from the player?
- Do you want to delete it once it goes beyond the current viewport or do you want it to continue traveling outside the viewable area?
You may have already thought about all of this so apologies if I'm going over problems/scenarios that you have already solved but if not then the above conditions are worth thinking about.