![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | LeoJ |
I’ve been following a tutorial and i am not sure why my code doesn’t work and i don’t know why the “actor” is an integer value and how do I change this?
also I’m not sure how to insert images as I can’t copy and paste so here is the code:
yes it is a mess I’m planning to separate the different states into their own scripts with managers
also I have no idea why not all the code is going into the format its supposed to on this text box thing
signal state_changed(new_state)
enum State {
PATROL,
ENGAGE
}
onready var player_detection_zone = $Player_detection_zone
onready var patrol_timer = $PatrolTimer
var current_state: int = State.PATROL setget set_state
var actor: KinematicBody2D = null
var player: Player = null
var weapon: Weapon = null
var origin: Vector2 = Vector2.ZERO
var patrol_location: Vector2 = Vector2.ZERO
var patrol_location_reached := false
var actor_velocity: Vector2 = Vector2.ZERO
func _process(delta):
match current_state:
State.PATROL:
if not patrol_location_reached:
actor.move_and_slide(actor_velocity)
if actor.global_position.direction_to(patrol_location) < 5:
patrol_location = true
actor_velocity = Vector2.ZERO
patrol_timer.start()
State.ENGAGE:
if player != null and weapon != null:
var angle_to_player = actor.global_position.direction_to(player.global_position).angle()
actor.rotation = lerp(actor.rotation, angle_to_player, 0.05)
if abs(actor.rotation - angle_to_player) < 0.1:
weapon.shoot()
else:
print("in the engage state but no weapon or player")
_:
print("Error: found a state for our enemy that should not exist")
func initialize(actor, weapon: Weapon):
self.actor = actor
self.weapon = weapon
func set_state(new_state: int):
if new_state == current_state:
return
if new_state == State.PATROL:
origin = actor.global_position
patrol_timer.start()
patrol_location_reached = true
current_state = new_state
emit_signal("state_changed")
func _on_Player_detection_zone_body_shape_entered(body_rid, body, body_shape_index, local_shape_index):
if body.is_in_group(“player”):
set_state(State.ENGAGE)
player = body
func _on_Player_detection_zone_body_exited(body):
if player and body == player:
set_state(State.PATROL)
player = null
func _on_PatrolTimer_timeout():
var patrol_range = 50
var random_x = rand_range(-patrol_range, patrol_range)
var random_y = rand_range(-patrol_range, patrol_range)
patrol_location = Vector2(random_x, random_y) + origin
patrol_location_reached = false
actor_velocity = actor.global_position.direction_to(patrol_location) * 100