Including the same code snippet in every entity

Godot Version

4.2.1

Question

Hi, I’m new to programming so forgive the simple question.

I’m writing code for a pause menu to pop up on screen whenever the “Events” singleton script sends the game_paused signal.

In the Events script i have this code:

signal game_paused(bool)

func _unhandled_input(event: InputEvent) -> void:
	if Input.is_action_just_pressed("pause"):
		game_paused.emit(true)

And I wrote this code for entities, so that they are paused when the pause menu shows up:

func _ready() -> void:
	Events.game_paused.connect(_on_events_game_paused)

func _on_events_game_paused(pause: bool) -> void:
	if pause == true:
		set_process(false)
	elif pause == false:
		set_process(true)

The problem is: I’m having to paste this second code snippet in every entity (player, enemies, enemy spawner, etc)

Is there a way to turn that code in a “standalone script” and “include” the script at the start of another script?

Like you would #include <stdio.h> in C for example?

Thanks!

btw.
a better way is to use pause_mode which is already in godot
and then you just set what you want and what you don’t want to have in the pause process

I see, so I’m reinventing the wheel here, lol

Thanks, i’ll check it out

Find out how to correctly pause the game along with the shader animations.

Thank you, I watched it