How do you disable a function?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Tigersteel

I am wondering if there is a way to disable the _physics_function(delta) function. I tried using set_process(false) and that didn’t work.

What’s the goal exactly? If you don’t want the function to do anything, don’t put any code in it. I assume that’s not what you’re after, but I’m not sure I understand the ask.

jgodfrey | 2022-12-19 15:07

I have some code in the physics process delta function but I want to disable that code because when the player dies and does its death sequence it keeps following the code under the physics process function and I don’t want it to do that.

Tigersteel | 2022-12-19 15:13

So, in that case, you probably want to just put some sort of boolean guard around the logic you want to “skip” when your player dies. Something like:

var alive = true

func _physics_process(delta):
    if alive:
        # do normal "alive" processing

Then, when your player “dies”, just set the alive variable to false to skip the _physics_process() logic.

jgodfrey | 2022-12-19 15:16

That worked thank you so much

Tigersteel | 2022-12-19 15:21

:bust_in_silhouette: Reply From: jgodfrey

So, based on the above discussion, the answer here is to put a boolean guard around the logic in question to only run the code when appropriate. Example transferred from above:

var alive = true

func _physics_process(delta):
    if alive:
        # do normal "alive" processing

Then, set alive to false when the player dies and the associated _physics_process() code will stop exeucting.

:bust_in_silhouette: Reply From: LeslieS

Alternatively you could use set_physics_process(false)