Skip to content

Error Log

Programming, much like navigating a vast sea, has its challenges. Among the waves and gusts, an error is a looming storm cloud, a call to action that something isn't right. The Error log is the beacon, the siren that not only alerts you to the issue but offers insights on how to navigate through it.

When invoked, if error logging is toggled on in your settings, an ErrorLog springs to life. Alongside the provided message, it captures the moment's essence, from the stack trace to any added context. Post-creation, if the throwException switch is on, the system halts, demanding attention, and if there's any relevant state data, it's serialized for deeper analysis.

Parameters

  • message: This is the alarm bell of the function. When an error occurs, this message provides clarity on the nature of the problem, be it "Failed to load level assets" or "Player HP is below 0."
  • throwException: Think of this as the next level alert. If this switch is on (and by default, it is), it's not just about logging the error; the system will actively throw an exception, pausing the code's execution to draw attention to the crisis.
  • state: Context is vital when deciphering errors. The state captures this context, like a snapshot in time. Whether it's a character's last action or the state of the game world when things went south, state keeps that memory. (Optional)

Effective Utilization

Imagine a scenario in a strategy game where a player is about to deploy a unit but lacks the necessary resources:

void DeployUnit(Unit unit)
{
    if (player.Resources < unit.Cost)
    {
        Log.Error("Insufficient resources to deploy the unit!", throwException: false);
        return;
    }
    // Deploy the unit
}

Here, if the resources fall short, the error logs it, but the game continues without crashing, thanks to the throwException set to false.

The Error log is more than just a reporter; it's a safeguard and a guide. It announces the presence of issues and provides a way to understand and address them. Being informed and agile in responding to such signals ensures that your game remains an enjoyable voyage for all players.