Files
XCEngine/docs/api/core/event/Subscribe.md
ssdfasd de4086dbfe docs: Fix core module documentation discrepancies
Fixed the following issues in XCEngine Core module documentation:
- Added 'using namespace XCEngine::Core;' to all code examples that use
  Core types (Event, FileWriter, etc.) without full namespace qualification
- Added missing '#include <XCEngine/Containers/String.h>' to FileWriter
  examples that use Containers::String
- Added '#include <string>' to Flush.md example using std::to_string

Affected files:
- core/core.md: Added using directive and Containers include
- event/*.md: Added using namespace to all 8 event doc files
- filewriter/*.md: Added using namespace and proper includes to all 6 files
2026-03-19 01:04:30 +08:00

1.1 KiB
Raw Blame History

Event::Subscribe

uint64_t Subscribe(Callback callback);

订阅事件回调。

描述

将回调函数添加到事件订阅列表中,并返回一个唯一的订阅 ID。该 ID 可用于后续退订。线程安全,可在任意线程调用。

参数:

  • callback - 要订阅的回调函数,类型为 std::function<void(Args...)>

返回: uint64_t - 订阅 ID用于退订

复杂度: O(1) amortized

示例:

#include <XCEngine/Core/Event.h>

using namespace XCEngine::Core;

// 定义事件
Event<int, float> damageEvent;

// 订阅多个回调
uint64_t id1 = damageEvent.Subscribe([](int damage, float time) {
    printf("Damage taken: %d at time %f\n", damage, time);
});

uint64_t id2 = damageEvent.Subscribe([](int damage, float time) {
    // 记录伤害日志
});

// 使用 lambda 表达式
auto callback = [](int damage, float time) {
    // 处理伤害
};
uint64_t id3 = damageEvent.Subscribe(callback);

相关文档