Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frame delays with optional delta return #15

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

daihaminkey
Copy link
Contributor

Changeling

Added

  • Methods GDTask<double> GDTask.NextFrameWithDelta(...)
  • Method GDTask<double> GDTask.DelayFrameWithDelta(...)

Changed

  • Amount of GDTask GDTask.NextFrame(...) overloads reduced with default values for CancellationToken support

Proposition

It's useful to know delta of frame-skipping functions, for example for animation progress in async methods.

Old solution

It was possible with GDTaskPlayerLoopAutoload.Global.DeltaTime and GDTaskPlayerLoopAutoload.Global.PhysicsDeltaTime

await GDTask.NextFrame();
var delta = GDTaskPlayerLoopAutoload.Global.DeltaTime;

// apply it to movement or animation

Issues with that:

  • works only for skip of one frame
  • two lines of code, one for await and one for reading value
  • timer loop needs to be set manually
  • no solution for new pause timer loops

New solution

var delta = await GDTask.NextFrameWithDelta();

// apply it to movement or animation

Why it's cool:

  • old methods work the same way, it's an additional module
  • working with deltas is the same, as in _Process methods, it would be easier for new users
  • two times less lines of code
  • it's calculating deltas manually, so works with pause loops

Default value of CancellationToken works since C# 7.1
Godot 4.0+ by default uses C# 10
@Atlinx
Copy link
Member

Atlinx commented May 19, 2024

Thanks for the PR, this seems like a neat idea! But I think there might be one drawback with measuring the delta manually. If the game is paused, any delays with PlayerLoopTiming.Process will be paused, and while paused the delta time would keep growing. This means if you unpaused the game, you'd get a large delta time value, which might mess up calculations that use it.

@daihaminkey
Copy link
Contributor Author

Thanks for the PR, this seems like a neat idea! But I think there might be one drawback with measuring the delta manually. If the game is paused, any delays with PlayerLoopTiming.Process will be paused, and while paused the delta time would keep growing. This means if you unpaused the game, you'd get a large delta time value, which might mess up calculations that use it.

Yes, that makes sense.

I can use GDTaskPlayerLoopAutoload.Global.DeltaTime under the hood, but then behavior will be undefined if delay intentionally uses pause time loop.

Do you have any tips how I can get delta values for pause time loops?

@Atlinx
Copy link
Member

Atlinx commented May 20, 2024

Maybe we could make a Global.DeltaTime and another Global.PauseDeltaTime. PauseDeltaTime would be the delta time of the game while the game is still paused. Then inside of ProcessListener, we could store the delta time of the last frame it received. The ProcessListener is used by the GDTask autoload, and runs even when the game is paused. Finally Global.PauseDeltaTime can be set to the delta time stored in the ProcessListener. Inside of the wait with delta, we'd also have to check what player loop timing is being used, and then return the appropriate delta time. Note that physics process should use a fixed delta time.

One downside to this approach is that we can't do a DelayFrameWithDeltaTime, since we can't measure the entire duration of the task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants