Time
Static class for time management. Access delta time, frame count, and FPS.
using Xengine.Core;
Properties
Time.Now
public static float Now { get; }
Time in seconds since the engine started.
// Move object based on total time
float y = MathF.Sin(Time.Now * 2f); // Oscillate up/down
Time.Delta
public static float Delta { get; }
Time in seconds since the last frame. Use this for frame-rate independent movement.
// Move 5 units per second regardless of frame rate
Transform.Position += direction * 5f * Time.Delta;
Time.UnscaledDelta
public static float UnscaledDelta { get; }
Delta time unaffected by Time.Scale. Use for UI animations during pause.
Time.Scale
public static float Scale { get; set; }
Time multiplier. Set to 0 to pause, 0.5 for slow-motion, 2 for fast-forward.
// Pause the game
Time.Scale = 0f;
// Slow motion
Time.Scale = 0.5f;
// Normal speed
Time.Scale = 1f;
Time.FixedDelta
public static float FixedDelta { get; set; }
Fixed timestep for physics updates. Default is 1/60 (60 updates per second).
Time.FrameCount
public static int FrameCount { get; }
Total number of frames since the engine started.
Time.FPS
public static float FPS { get; }
Current frames per second.
Time.SmoothedFPS
public static float SmoothedFPS { get; }
Smoothed FPS value that doesn't fluctuate as much.
Timer Helpers
TimeSince
Tracks time elapsed since creation. Perfect for cooldowns.
public class Weapon : Component
{
private TimeSince _lastShot;
protected override void OnUpdate()
{
// Check if 0.5 seconds have passed
if (_lastShot > 0.5f && RaylibInput.IsKeyPressed(KeyCode.Space))
{
Shoot();
_lastShot = 0; // Reset timer
}
}
}
TimeUntil
Tracks time remaining until a target. Great for delays and countdowns.
public class Bomb : Component
{
private TimeUntil _explodeTime;
protected override void OnStart()
{
_explodeTime = 3f; // Explode in 3 seconds
}
protected override void OnUpdate()
{
if (_explodeTime.HasElapsed)
{
Explode();
}
// Show remaining time
float remaining = _explodeTime.Remaining;
}
}
Common Patterns
Frame-Rate Independent Movement
protected override void OnUpdate()
{
float speed = 5f;
Vector3 velocity = new Vector3(1, 0, 0);
// Always multiply by Time.Delta!
Transform.Position += velocity * speed * Time.Delta;
}
Smooth Interpolation
protected override void OnUpdate()
{
// Smoothly move towards target
float smoothSpeed = 5f;
Transform.Position = Vector3.Lerp(
Transform.Position,
targetPosition,
smoothSpeed * Time.Delta
);
}
Periodic Actions
protected override void OnUpdate()
{
// Do something every 2 seconds based on Time.Now
if (Time.FrameCount % 120 == 0) // Roughly every 2 sec at 60fps
{
SpawnEnemy();
}
// Better approach using TimeSince
if (_spawnTimer > 2f)
{
SpawnEnemy();
_spawnTimer = 0;
}
}