Jump to content

The importance of a game log and how to make it


Andy90

471 views

 Share

 

In this article, I would like to delve into the concept of "Game Logs" and explain why they play an important role. First, however, we should clarify what exactly a Game Log is.

The Game Log allows us to log and temporarily store various actions in the game. Furthermore, with the help of the Game Log, we can inform independent systems about various actions. A good example of this is a simple quest system, similar to the one in World of Warcraft. In such systems, we want to complete quests by, for example, looting a specific item, killing a certain enemy, or interacting with a specific NPC. But how are the individual quests supposed to know exactly when we loot an item? Sure, one could simply search through the inventory. However, that would only work for quest types where we are supposed to loot an item, and not for those where we are supposed to kill a specific enemy or talk to an NPC. This is where the Game Log proves to be extremely useful, as our quests can simply search through the entries to update themselves accordingly.

In the past few days, I have been working on implementing such a system in C++. It is important to note that this is not a component; instead, a "Global.h" file is required, where the log is defined as a global variable.

GameLog.h

#pragma once
#include "UltraEngine.h"

using namespace UltraEngine;

/// <summary>
/// Event types
/// </summary>
enum GameLogEvent {
	GAME_LOG_LOOT_ITEM,
	GAME_LOG_KILL_ENEMY
};

/// <summary>
/// Game Log entrys
/// </summary>
struct GameLogEntry {
	String uuid;
	uint64_t timestamp;
	GameLogEvent event;
	table data;
};

/// <summary>
/// GameLogListner forward declaration
/// </summary>
class GameLogListener;

/// <summary>
/// Handles the game log entrys
/// </summary>
class GameLog
{
private:
	vector<shared_ptr<GameLogListener>> m_listeners;
	vector<GameLogEntry> m_log;

public:
	void Log(GameLogEvent event, table eventData);
	void AddListener(shared_ptr<GameLogListener> listener);
	void LogLootItem(String itemName, int itemCount);
	void LogKilledEnemy(String enemyName);
	void Clean(uint64_t time);
	vector<GameLogEntry> FetchLog(uint64_t startTime);
};

GameLog.cpp

#include "UltraEngine.h"
#include "GameLog.h"
#include "GameLogListener.h"


void GameLog::Log(GameLogEvent event, table eventData)
{
	GameLogEntry entry;
	entry.uuid = Uuid();
	entry.timestamp = Millisecs();
	entry.event = event;
	entry.data = eventData;
	m_log.push_back(entry);

	for (auto listener : m_listeners) {
		if (listener->Event == event) {
			listener->Callback(eventData);
		}
	}

	Print("Added data to gamelog");
}

void GameLog::AddListener(shared_ptr<GameLogListener> listener)
{
	m_listeners.push_back(listener);
}

void GameLog::LogLootItem(String itemName, int itemCount)
{
	table data;
	data["itemName"] = itemName;
	data["itemCount"] = itemCount;
	
	Log(GAME_LOG_LOOT_ITEM, data);
}

void GameLog::LogKilledEnemy(String enemyName)
{
	table data;
	data["enemyName"] = enemyName;
	Log(GAME_LOG_KILL_ENEMY, data);
}

void GameLog::Clean(uint64_t time)
{
	vector<GameLogEntry> newEntrys;
	for (auto entry : m_log) {
		auto diff = Millisecs() - entry.timestamp;
		if (diff <= time) {
			newEntrys.push_back(entry);
		}
		else {
			Print("Entry " + entry.uuid + " out of time!");
		}
	}
	m_log = newEntrys;
}

vector<GameLogEntry> GameLog::FetchLog(uint64_t startTime)
{
	vector<GameLogEntry> result;
	for (auto entry : m_log) {
		if (entry.timestamp > startTime) {
			result.push_back(entry);
		}
	}
	return result;
}

 

In the file GameLog.h, we find an enum with various events, to which more can be added. Currently, there are entries for looting an item and killing an enemy.

Furthermore, the structure "GameLogEntry" defines various variables, including a string for a UUID, a long variable for the timestamp, the event, and the data of the entry.

Following that is the main class, which contains various functions as well as two lists. One list is for so-called hooks, and the other is for the log itself.

Here's an overview of the functions:

 

  • Log(): Adds entries to the log.
  • AddListener(): Adds a callback (no callback is needed in this article).
  • LogLootItem(): Simplifies adding a loot event to the game's log.
  • LogKilledEnemy(): Simplifies adding a kill event.
  • Clean(uint64_t time): Deletes all entries older than the specified parameter in milliseconds.
  • vector<GameLogEntry> FetchLog(uint64_t startTime): Returns a list of log entries whose timestamp is greater than the start time in the parameter.

 

If we want to determine whether we have looted an item or killed a specific enemy, we can simply retrieve the log. It is important to store the time of the last query so that we only receive the entries that are still unknown to us.

void VitalUI::UpdateNotifications()
{
	m_notifyer->ClearItems();
	auto fetchTime = Millisecs() - 5000;
	auto log = g_log.FetchLog(fetchTime);
	for (auto entry : log) {
		if (entry.event == GAME_LOG_LOOT_ITEM) {
			m_notifyer->AddItem("Looted " + String(entry.data["itemName"]) + " (X" + String(entry.data["itemCount"]) + ")");
		}
	}
	if (m_notifyer->items.size() > 0) {
		m_notifyer->Redraw();
	}
}

To ensure that the log does not grow infinitely, it is advisable to clear it regularly. In my implementation, I clear the log every 6 minutes in the main loop. That should be sufficient to process all events.

while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false)
{
    while (PeekEvent()) {
        const Event ev = WaitEvent();
        g_ui->GetInterface()->ProcessEvent(ev);
        switch (ev.id)
        {
        case EVENT_WIDGETACTION:
            break;
        case EVENT_WINDOWCLOSE:
            return 0;
            break;
        }
    }
    
    world->Update();
    world->Render(framebuffer, false, 0);
    g_log.Clean(60000);
}

As you can see, a Game Log has become extremely practical and indispensable in modern games. If you have any questions about this article, feel free to contact me. Additionally, it's worth mentioning that the log can be used for many other purposes and is not limited to just a quest system.

 

Gamelog.zip

  • Like 4
  • Upvote 1
 Share

0 Comments


Recommended Comments

There are no comments to display.

Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...