Godot Version
4.3 or 4.2
Question
If i spawn 3 the 3rd one detects enemies and chases them
if i spawn 2 the 2nd one detects enemies and chases them
if i spawn 1 at a time and wait a second in between each spawn it works perfectly.
no idea what is making this happen.
even having the
func _ready():
%EnemyDetection.monitoring = true
still it doesnt fix the issue.
i can put this code in the process function but i think this is a bit of a waste.
also feel like im avoiding whats actually causing the problem.
tried putting this in the function that runs when the CharacterBody enters the state it spawns in
func on_enter():
if just_spawned:
just_spawned = false
%EnemyDetection.monitoring = true
this just makes the character switch states to the attack state and it attacks quickly twice, but after those double attacks everythings fine.
i feel like im appplying minor fixes but there is something im missing.
monitoring is turned on in the inspector 100% aswell
i think its soemthing to do with the code thats spawning the characters,
if Input.is_action_just_pressed("shoot") :
shoot(BasicCloneScene)
func shoot(projectile : PackedScene) -> void:
var projectile_instance = projectile.instantiate()
projectile_instance.position = $".".position
var aim_direction = global_position.direction_to(get_global_mouse_position()) #can delete if clone shooting doesnt work
add_child(projectile_instance)
projectile_instance.throw(aim_direction)
or maybe a problem with my state machine code
extends Node
class_name CharacterStateMachine
@export var character : CharacterBody2D
@export var animation_tree : AnimationTree
@export var current_state : State
var states : Array[State]
func _ready() -> void:
#creating array of states
for child : State in get_children():
if (child is State):
states.append(child)
#telling the states what they need to function
child.character = character
child.playback = animation_tree["parameters/playback"]
#connect to interrupt signal
child.connect("interrupt_state", on_state_interrupt_state)
else: #warning if a child is not a state
push_warning("Child " + child.name + " is not a State for CharacterStateMachine")
func _physics_process(delta : float) -> void:
if (current_state.next_state != null): #if current state holds something switch
switch_states(current_state.next_state)
current_state.state_process(delta)
func check_if_can_move() -> bool: #checking if the state allows the character to move
return current_state.can_move
func check_gravity() -> bool:
return current_state.apply_gravity
func switch_states(new_state : State) -> void: #creating new_state argument to pass into function
if (current_state != null):
current_state.on_exit()#if i need to do anything else when state exits
current_state.next_state = null #resets state to null for next change
current_state = new_state #change of state
current_state.on_enter() #any code i want to execute upon entering a new state
func _input(event :InputEvent) -> void:
current_state.state_input(event)
func on_state_interrupt_state(new_state : State):
switch_states(new_state)
or maybe this part of my idle state
func on_exit():
%EnemyDetection.monitoring = false