I need your advice about function!

Hi…
I’m very sorry about asking question again and again…
But I can’t solve it.
So I’m asking you a question.

I want my enemy to move every 10 seconds.
So I made a timer and operate it
But the enemy moves only on the first turn and no longer moves
The timer is working normally

Maybe thhe ‘enemy_move_possible’ make problem…
Could you help me…?

I’m not sure how you want the movement to work.

my first guess was that aftter moving in _process() you might want to set enemy_move_possible to false. But this probably result in a short stutter instead of moving correctly.

Should it move every few seconds for a fixed distance?
Then you could consider getting rid of the enemy_move_possible variable completely and just handle the position change in _on_enemy_movement_cooldown_timer_timeout() along with starting the timer.

If you want it to move every x seconds for y seconds (e.g. move for 3 seconds, then wait for 6 seconds, then move again,…) then you have two things that you might want a timer for. You could e.g. use the same timer and toggle between a moving and a waiting state or use two timers, one for each.
For the first variant, you only need to change

enemy_move_possible = true

in line 20 to

enemy_move_possible = !enemy_move_possible

which negates the boolean (i.e. true becomes false and false becomes true)

1 Like

Thank you from the bottom of my heart!!
I want to express my gratitude, but I don’t have a good vocabulary…
I will try to make two timer!!

1 Like

Based on your advice, I made a bit of code and it doesn’t seem to work the way I want it to.
I’m really sorry, but could you tell me the problem…?


can you describe the new problem?

1 Like

Ah! Oh! I’m so sorry!
The problem is that the enemy moves only on the first turn!
And maybe the on_enemy_movement_end_timer() funtion isn’t working properly…

that sounds like the timeout functions are maybe not called? do you see the “start” and “end” messages in the console?

If not, check if you have the timeout signals of the timers connected to the respective functions. Also make sure they are set to One Shot in the inspector (otherwise they might introduce some unexpected behavior in your case)

1 Like

he issue seems to be related to how enemy_move_possible is being reset in your code. Once enemy_move_possible is set to true, it allows movement, but you don’t reset it to false after the movement, causing the condition to always be true.

Reset enemy_move_possible after the enemy moves.
Consider adding more logic to control when enemy_move_possible should be set to true again.

Here’s how you can modify the _process function:
func _process(delta: float) → void:
if enemy_move_possible:
enemy_object.global_position = position.move_toward(Vector2(enemy_random_value, 98), delta * enemy_speed)
enemy_move_possible = false # Reset after moving to avoid multiple moves
return

This way, after the enemy moves once, enemy_move_possible is set back to false, preventing further movement until the timer allows it again.

1 Like

Thank you so much…!
I’m sorry for the late response!
I’ll try as your advice!!!

1 Like

Thank you very much for your help!!!
I tried putting enemy_movement_possible in the process function and it stopped moving!
Maybe I need a more complicated way

Thank you again for your advice!!!

1 Like

It sounds like resetting “enemy_move_possible” inside the _process function caused the movement to stop altogether, likely because it was reset too quickly, preventing any visible movement…well…try this

extends Area2D

@onready var enemy_object = $“.” # Mabye this refers to the correct node
@onready var enemy_movement_cooldown_timer = $enemy_movement_cooldown_timer
var enemy_speed = 100
var enemy_random_value = 0
var enemy_move_possible = false

func _ready() → void:
enemy_random_value = pick_random_position()
enemy_movement_cooldown_timer.start()

func _on_enemy_movement_cooldown_timer_timeout() → void:
enemy_random_value = pick_random_position()
enemy_move_possible = true
print(“time out”)

func _process(delta: float) → void:
if enemy_move_possible:
enemy_object.global_position = enemy_object.global_position.move_toward(Vector2(enemy_random_value, 98), delta * enemy_speed)
if enemy_object.global_position.distance_to(Vector2(enemy_random_value, 98)) < 1:
enemy_move_possible = false
enemy_movement_cooldown_timer.start()

func pick_random_position() → int:
var enemy_coord = [-127, -100, -90, -78, -62, -48, -28, -10, 0, 9, 12, 26, 40, 63, 72, 99, 102, 130]
return enemy_coord.pick_random()

Movement Control:

“enemy_move_possible” is only set to “true” when the timer times out.
The enemy moves toward a random position, and once it gets close enough (within a distance of 1 unit), movement stops (“enemy_move_possible” = “false”), and the timer restarts.

Timer and Movement:
Each time the timer times out, a new random position is selected.
The movement happens over multiple frames, based on the delta time, so it’s smooth and visible.

Movement Completion:
The enemy stops moving when it reaches (or is very close to) the target position, and the process repeats with the timer.

This approach should allow your enemy to move every 10 seconds as desired, with smooth and controlled movement.

Let me know if this helped … :smiley:

oh wait its already solved… my bad

1 Like

I can’t check it right away because I’m outside the house, but I’m sure it’ll help!
I heartily thank you!!!
Thank you so much for taking the time to help me out!
I’ll try it and tell you about the result!!!

1 Like

Sorry for replying so late… your code was perfect and after some tweaks it works perfectly now. Thank you so much

Happy to help :smiley:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.