I need help pausing Code

Godot Version

4.3

Question

I am working on making a boss and for it’s only attack I need it to pause a line of code for the attack and then unpause when it is complete here is the line of code here

extends Node2D

const SPEED = 60

var direction = 1

@onready var ray_cast_right: RayCast2D = $RayCastRight
@onready var ray_cast_left: RayCast2D = $RayCastLeft
@onready var ray_cast_down: RayCast2D = $RayCastDown
@onready var ray_cast_up: RayCast2D = $RayCastUp
@onready var timer: Timer = $Timer

#Movement
func _process(delta):
if ray_cast_right.is_colliding():
direction = -1
if ray_cast_left.is_colliding():
direction = 1

position.x += direction * SPEED * delta

#Attack
func _on_area_2d_body_entered(body):
print(“Seen”)

timer.start()

func _on_timer_timeout() → void:
direction = 1

position.y += direction
timer.stop()

func _on_area_2d_2_area_entered(area: Area2D) → void:
timer.start()

func _on_timer_2_timeout() → void:
direction = -1

position.y += direction 

if ray_cast_up.is_colliding():
	timer.stop()

Here on the forums, if you wrap your code like so:

```gdscript
code
```

It makes it far more readable:

extends Node2D

const SPEED = 60

var direction = 1

@onready var ray_cast_right: RayCast2D = $RayCastRight
@onready var ray_cast_left: RayCast2D = $RayCastLeft
@onready var ray_cast_down: RayCast2D = $RayCastDown
@onready var ray_cast_up: RayCast2D = $RayCastUp
@onready var timer: Timer = $Timer

#Movement
func _process(delta):
    if ray_cast_right.is_colliding():
        direction = -1
    if ray_cast_left.is_colliding():
        direction = 1

    position.x += direction * SPEED * delta

#Attack
func _on_area_2d_body_entered(body):
    print(“Seen”)
    timer.start()

func _on_timer_timeout() -> void:
    direction = 1
    position.y += direction
    timer.stop()

func _on_area_2d_2_area_entered(area: Area2D) -> void:
    timer.start()

func _on_timer_2_timeout() -> void:
    direction = -1
    position.y += direction 

    if ray_cast_up.is_colliding():
	timer.stop()

The indentation on that may have been messed up; I had to guess at some of it.

What is the line of code you want to pause? How do you want the boss to behave?

2 Likes

You got the code correct, I wanted the boss to be flying above the player until the player is directly below the boss, afterwards it slams itself on the floor and it should go back to its original position however when it detects the player it just keeps moving side to side like how it usauly moves
so I would like this line of code paused

#Movement
func _process(delta):
    if ray_cast_right.is_colliding():
        direction = -1
    if ray_cast_left.is_colliding():
        direction = 1

    position.x += direction * SPEED * delta

Fair enough. Typically the way you do something like that is with a “state machine”, which isn’t as complicated as it sounds:


enum BossState { search, fall, smash, retract }

var state = BossState.search

func _process(delta):
    match state:
        BossState.search:
            if   ray_cast_right.is_colliding(): direction = -1
            elif ray_cast_left.is_colliding():  direction =  1

            if player_is_underneath():
                state = BossState.fall
        BossState.fall:
            # Fall on the player!
        BossState.smash:
            # Sit on them for a bit!
        BossState.retract:
            # Go back up!
1 Like

did not work :frowning:

Can you post your current code?

    extends Node2D

const SPEED = 60

var direction = 1

@onready var ray_cast_right: RayCast2D = $RayCastRight
@onready var ray_cast_left: RayCast2D = $RayCastLeft
@onready var ray_cast_down: RayCast2D = $RayCastDown
@onready var ray_cast_up: RayCast2D = $RayCastUp



enum BossState { search, fall, smash, retract }

var state = BossState.search


func _process(delta):
	match state:
		BossState.search:
			if   ray_cast_right.is_colliding(): direction = -1
			elif ray_cast_left.is_colliding():  direction =  1

			if player_is_underneath():
				state = BossState.fall
		BossState.fall:
			position.y += 1
		BossState.smash:
			position.y += 0
		BossState.retract:
			position.y += -1

it says that the “player_is_underneath()” is not found in the base self

You could add a variable at the top var player_underneath: bool = false and then set it to true when the ray cast detects the player underneath. Just dont forget to return it to false when player no longer is underneath. Or you could write a function for it. The ““player_is_underneath()” is not found in the base self” means that the function does not exist.

Because you have to design the function that determines if the player is underneath the boss yourself. Something like…

func player_is_underneath() -> bool:
    return ray_cast_up.is_colliding():
1 Like