How can I update player_pos every tick?

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

Hi all.

Here’s the situation: I can get the player’s position as soon as they enter the Pursuit Area, but I want it to update every frame so the hostile ship can turn towards them. However, it only changes when I enter/leave the area.

EnemyTemplate.gd

extends RigidBody2D

signal searching
signal turning
signal moving

enum states {
	SEARCH,
	ROTATE,
	MOVE,
	DEAD
}

export var move_thrust = Vector2(175,0)
export var torque = 3000
var has_target = false
var target_list = ["none"]
var current_state = "SEARCH"
var player_pos = Vector2()
var in_pursuit_area = false
var thruster_active = false
var is_turning = false
var is_searching = true
var d
var player


func _ready():
	$MaxPursuitCaster.add_exception($SearchRange)
	$MaxPursuitCaster.add_exception($PursuitRange)

func _integrate_forces(state):
	var ship_pos = self.global_position
	var ship_rot = self.global_rotation
	var speed = self.get_linear_velocity()
	var spin = self.get_angular_velocity()

	# SEARCH
	if is_searching == true:
		if speed < Vector2(0,0) or speed > Vector2(0,0):
			self.set_linear_damp(1)
		else:
			self.set_linear_damp(-1)
		if spin < 0 or spin > 0:
			self.set_angular_damp(3)
		else:
			self.set_angular_damp(-1)

	# ROTATE
	if is_turning == true:
		var rot_dir = 0
		var angle_rad = self.get_angle_to(player_pos)
		var angle_deg = rad2deg(angle_rad)
		var w = (d * 1000) / angle_deg
		var new_degrees = lerp(0, angle_deg, w)
		var new_rad = deg2rad(new_degrees)
		if new_rad > 0:
			rot_dir = 1
		if new_rad < 0:
			rot_dir = -1
		set_applied_torque(torque * rot_dir)

	# MOVE
	if thruster_active == true:
		self.set_angular_damp(5)
		set_applied_force(move_thrust.rotated(rotation))
	if thruster_active == false:
		set_applied_force(Vector2())
		self.set_angular_damp(-1)
func _on_PursuitRange_body_entered(body):
	if body.is_in_group("Player"):
		in_pursuit_area = true
		player_pos = body.get_global_position()
		# /\ Needs an update every tick /\
func _on_SearchRange_body_entered(body):
	if body.is_in_group("Player"):
		has_target = true
		if target_list.size() == 1:
			target_list.append(body)


func _on_PursuitRange_body_exited(body):
	if body.is_in_group("Player"):
		if target_list.size() > 1:
			target_list.pop_back()
			has_target = false
			in_pursuit_area = false


func _process(delta):
	d = delta

	match(current_state):
	
		"SEARCH":
			emit_signal("searching")
			if target_list.size() > 1:
				current_state = "ROTATE"


		"ROTATE":
			emit_signal("turning")
			if target_list.size() == 1:
				current_state = "SEARCH"


		"MOVE":
			if $MaxPursuitCaster.is_colliding() == true and has_target == true:
				var collider = $MaxPursuitCaster.get_collider()
				if collider.is_in_group("Player") and in_pursuit_area == true:
					emit_signal("moving")
					is_turning = false
			else:
				$Thrusters/Engine.emitting = false
				thruster_active = false
				current_state = "ROTATE"


		"DEAD":
			self.queue_free()


func _on_EnemyTemplate_body_entered(body):
	if body.is_in_group("Singularity"):
		current_state = "DEAD"

func _on_EnemyTemplate_searching():
	is_searching = true

func _on_EnemyTemplate_turning():
	is_turning = true

func _on_EnemyTemplate_moving():
	thruster_active = true
	$Thrusters/Engine.emitting = true

[EDIT: Figured I should include the entire script.]

:bust_in_silhouette: Reply From: thirite

You can’t get an update each tick on a signal, that’s the opposite of what they’re for. :stuck_out_tongue: Set a variable pointing towards the player object in the signals (and clear it on exit) and then reference that on _process

So that’d be…

Emit a signal when the player enters the SearchRange.

And in _process(), have player_pos equal the player position, until the player leaves the PursuitRange?

Emit a signal when the player leaves the PursuitRange.

System_Error | 2019-11-18 21:31