0 votes

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.]

asked Nov 17, 2019 in Engine by System_Error (197 points)
edited Nov 19, 2019 by System_Error

1 Answer

0 votes

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

answered Nov 18, 2019 by thirite (154 points)
edited Nov 18, 2019 by thirite

So that'd be…

Emit a signal when the player enters the SearchRange.

And in process(), have playerpos equal the player position, until the player leaves the PursuitRange?

Emit a signal when the player leaves the PursuitRange.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.