ProjectileCaller2D

Inherits: Node2D < CanvasItem < Node < Object

An intermediary between your code and the AP engine.


Description

Provides indirect access to the AP engine. Holds and delegates the creation of projectiles.

This node also serves as a container for your ProjectileBlueprints.


Properties



Methods



Property Descriptions

ProjectileProcessor2D projectile_processor

Direct access to the AP engine itself.

You should not use it for direct projectile creation. Use the request_projectile method instead.


Array[ProjectileBlueprint2D] projectile_resources

The list of ProjectileBlueprint2D resources that can be directly created from the ProjectileCaller2D.

Each ProjectileCaller2D instance can only request the creation of its own stored projectiles.



Method Descriptions

bool request_projectile (index: int, _position: Vector2, destination: Vector2, target: Node2D = null, move_method: Callable = Callable(), start_method: Callable = Callable(), collision_method: Callable = Callable(), expired_method: Callable = Callable())

Returns true if the creation request has been successful.


Takes the following arguments:

index: Element of the projectile_resources array that will be created by the AP engine.

position: Start position of the projectile (In global coordinates).

destination: Position the projectile will move towards (In global coordinates).

target: Node2D the projectile will actively seek to (If selected in ProjectileBlueprint2D).

move_method: Callable that will be called every physics tick and will actively change the direction the projectile is heading to.

start_method: Callable that will be called at projectile creation.

collision_method: Callable that will be called when the projectile collides with available bodies or areas.

expired_method: Callable that will be called once the projectile runs out of pierce or lifetime.


Each Callable argument follows a define pattern and must take a set number of parameters and return a specific value.

  • The move_method callable must return a Vector2 and take the following two parameters:

    proj: A representation of the projectile object to be changed.

    delta: The time elapsed in seconds since the previous call to move_method.

    extends Node2D
    
    @onready var projectile_caller = $ProjectileCaller2D
    						
    
    # In this example the "custom_movement" function is passed as a Callable in the optional "move_method" parameter.
    func _process(_delta):
    	if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
    		var destination = get_global_mouse_position()
    		projectile_caller.request_projectile(0, position, destination, null, custom_movement)
    
    
    # This function will cause the projectile to always move in the UP direction.
    func custom_movement(proj: Projectile2D, _delta: float) -> Vector2:
    	return Vector2.UP
    

  • The start_method callable returns void and takes the following parameter:

    proj: A representation of the projectile object to be changed.

    extends Node2D
    
    @onready var projectile_caller = $ProjectileCaller2D
    						
    
    # In this example the "custom_start" function is passed as a Callable in the optional "start_method" parameter.
    func _process(_delta):
    	if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
    		var destination = get_global_mouse_position()
    		projectile_caller.request_projectile(0, position, destination, null, Callable(), custom_start)
    
    
    # This function will change the projectile "speed" property at projectile creation.
    func custom_start(proj: Projectile2D) -> void:
    	proj.speed = 50
    

  • The collision_method callable returns void and takes the following parameters:

    proj: A representation of the projectile object to be changed.

    area_rid: The RID of the area/body that entered or exited the projectile.

    area: The Node of the area/body that entered or exited the projectile.

    area_shape_index: Index of the interacting shape from the collidiong area/body.

    local_shape_index: Index of the interacting shape from the projectile area.

    extends Node2D
    
    # The projectiles will immediately expire upon collision with objects in this layer.
    @export_flags_2d_physics var wall_layer: int
    @onready var projectile_caller = $ProjectileCaller2D
    						
    
    # In this example the "custom_collision" function is passed as a Callable in the optional "collision_method" parameter.
    func _process(_delta):
    	if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
    		var destination = get_global_mouse_position()
    		projectile_caller.request_projectile(0, position, destination, null, Callable(), Callable(), custom_collision)
    
    
    # This function will change the way projectiles collide.
    func custom_collision(proj: Projectile2D, area_rid: RID, area: Node2D, area_shape_index: int, local_shape_index: int) -> void:
    	# The "validate_collision" method makes sure that:
    	# -> The projectile is not expired.
    	# -> A lock projectile will only collide with its assigned target.
    	# -> The projectile is not colliding with areas/bodies it already collided.
    	if not proj.validate_collision(area_rid, area):
    		return
    	
    	# Custom code snippet for immediate expiration on collision with "wall_layer"
    	if wall_layer & area.collision_layer != 0:
    		proj.is_expired = true
    		return
    	
    	# The "on_pierced" method makes sure that:
    	# -> The projectile refreshes its rehit cooldown and angular speed.
    	# -> The projectile appends colliding areas/bodies to its excluded targets.
    	# -> The projectile instance reduces its remaining pierce.
    	proj.on_pierced(area_rid)
    

  • The expired_method callable returns void and takes the following parameter:

    proj: A representation of the projectile object to be changed.

    extends Node2D
    
    @onready var projectile_caller = $ProjectileCaller2D
    						
    
    # In this example the "custom_expired" function is passed as a Callable in the optional "expired_method" parameter.
    func _process(_delta):
    	if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
    		var destination = get_global_mouse_position()
    		projectile_caller.request_projectile(0, position, destination, null, Callable(), Callable(), Callable(), custom_expired)
    
    
    # This function will print "expired" on the console once the projectile runs out of pierce or lifetime.
    func custom_expired(proj: Projectile2D) -> void:
    	print("expired")