2
\$\begingroup\$

Many 2D games have an intentional screen freeze (say, 75 milliseconds) when the player takes or deals damage. During this freeze, all physics and animation processing stops.

How can one implement a screen freeze in Godot?

If you want to suggest how to do this in Unity or another engine, that's fine too. It might help me to figure out how to do it in Godot.


My only idea is to have a boolean called is_screen_freeze along with some variables freeze_start_time and freeze_duration. In all my nodes' _process() and _physics_process()methods, I can just pass if is_screen_freeze is true. This seems like a rather inelegant way of going about a screen freeze, and I'm not even sure it would work since animations would probably keep playing. Is there a better way?

\$\endgroup\$
1

2 Answers 2

2
\$\begingroup\$

As flesk already stated, Godot has a very good pause function. You can just call

get_tree().paused = true

However, this will basically stop all physics, process and input functions making your game completely non-responsible.

Since your game obviously should recover after a short time, I would just create a new node that doesn't inherit the pause property from its parent (this can be set in the editor) but is set to process mode. You could then create a signal that is emitted by this node after a given amount of time. For example with the timer node:

freeze_timer = Timer.new()
freeze_timer.connect("timeout", self, "on_freeze_timeout")
freeze_timer.set_wait_time(0.075) # time in seconds
add_child(freeze_timer)
timer.start()

Finally you create an on_freeze_timeout function to catch the signal.

func on_freeze_timeout():
    get_tree().paused = false

Alternatively, you could also create a dedicated Timer node and just call it or use the process() function to count up to the time you want. However, with the latter your timing might be a bit off, depending on the time that passed between two function calls.

\$\endgroup\$
1
  • \$\begingroup\$ You can just use yield(get_tree().create_timer(0.075), 'timeout') between get_tree().paused = true and get_tree().paused = false. This is way more readable and shorter. \$\endgroup\$ Commented Apr 25, 2018 at 17:59
2
\$\begingroup\$

I have no experience with Godot but I have done this in Unity.

Taking a quick look at the documentation, I'd say you can pause the game using OS.set_time_scale(0.0) and get the current ms ticks (milliseconds since start of the engine) with OS.get_ticks_msec()

This would freeze the simulation, as delta would be 0 each frame. You then can wait the amount of time you want by comparing your stored ms ticks with the current OS.get_ticks_msec(), and once the 75 milliseconds has passed, you can set OS.set_time_scale(1.0) to keep the execution.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .