Jump to content

Recommended Posts

M2 Download Center

This is the hidden content, please
( Internal )

Hello,

 

Working on some new stuff I found out that current implementation of event looks a bit tricky.

Due to this fact I basically deciced to re-implement it in C++11 providing up to date tech.

Don`t forget to take a look at this topic before you start:

https://metin2dev.org/board/index.php?/topic/305-src-compile-with-gcc48-c11-and-optimized-flags/

So lets begin.

Add include into the main.cpp:

#ifdef __NEW_EVENT_HANDLER__
	#include "EventFunctionHandler.h"
#endif

And add this into main function before:

while (idle());
	#ifdef __NEW_EVENT_HANDLER__
		CEventFunctionHandler EventFunctionHandler;
	#endif

Now add this at the end of idle:

	#ifdef __NEW_EVENT_HANDLER__
		CEventFunctionHandler::instance().Process();
	#endif

Now search for:

sys_log(0, "<shutdown> Destroying CArenaManager...");

And add before:

	#ifdef __NEW_EVENT_HANDLER__
		sys_log(0, "<shutdown> Destroying CEventFunctionHandler...");
		CEventFunctionHandler::instance().Destroy();
	#endif

Now open service.h and add this define:

#define __NEW_EVENT_HANDLER__

That`s all.

Now just download attachment and add included files to your source.

  • Metin2 Dev 72
  • Sad 1
  • Confused 2
  • Good 15
  • muscle 1
  • Love 3
  • Love 79
Link to comment
https://metin2.dev/topic/17432-reimplementation-of-events/
Share on other sites

	#ifdef __NEW_EVENT_HANDLER__
		CEventFunctionHandler EventFunctionHandler;
	#endif

That doesn't need to exist, but if someone really want to use:

Search for:

  • CEventFunctionHandler::instance().Process();

Replace with:

  • EventFunctionHandler.Process();

Thanks for release, i like that structure, keep up the good work.

Link to comment
https://metin2.dev/topic/17432-reimplementation-of-events/#findComment-95546
Share on other sites

2 godziny temu, Tasho napisał:

	#ifdef __NEW_EVENT_HANDLER__
		CEventFunctionHandler EventFunctionHandler;
	#endif

That doesn't need to exist, but if someone really want to use:

Search for:

  • CEventFunctionHandler::instance().Process();

Replace with:

  • EventFunctionHandler.Process();

Thanks for release, i like that structure, keep up the good work.

Indeed. It is just singleton initial call.

Link to comment
https://metin2.dev/topic/17432-reimplementation-of-events/#findComment-95554
Share on other sites

  • 1 year later...
  • 6 months later...
  • 10 months later...
  • Honorable Member
40 minutes ago, Mafuyu said:

REUPLOAD AMK

Why are you cursing for no reason?

Link is at the top of the topic. You might want to go for an eye checkup.

Edited by Mali61
  • Lmao 2

 

Link to comment
https://metin2.dev/topic/17432-reimplementation-of-events/#findComment-138395
Share on other sites

2 minutes ago, Mali61 said:

Why are you cursing for no reason?

Link is at the top of the topic. You might want to go for an eye checkup.

160b238cf4.gif

 

big brain i wouldnt cry if it would be that simple my guuuuy

Edited by Metin2 Dev
Core X - External 2 Internal
Link to comment
https://metin2.dev/topic/17432-reimplementation-of-events/#findComment-138396
Share on other sites

  • Honorable Member
2 minutes ago, Mafuyu said:

160b238cf4.gif

 

big brain i wouldnt cry if it would be that simple my guuuuy

The link is broken = I have to swear

?

iTtUw4Y.png

Edited by Metin2 Dev
Core X - External 2 Internal
  • Good 1

 

Link to comment
https://metin2.dev/topic/17432-reimplementation-of-events/#findComment-138397
Share on other sites

  • Former Staff
4 minutes ago, Mafuyu said:

160b238cf4.gif

 

big brain i wouldnt cry if it would be that simple my guuuuy

What browser are you using?

Edited by Metin2 Dev
Core X - External 2 Internal
  • Good 1
Link to comment
https://metin2.dev/topic/17432-reimplementation-of-events/#findComment-138399
Share on other sites

  • 3 years later...
  • Honorable Member

Some people requested me to refactor this library for c++20 without invalidating the old syntax.

This is the hidden content, please

This is the hidden content, please

Here are the changes:

  • changed std::string_view instead of std::string handling
  • changed std::unordered_map instead of std::map
  • supported new foreach pair syntax
  • added missing const-ness
  • rewritten CEventFunctionHandler::Process with std::erase_if
  • replaced copy with impactless move for std::function
  • removed Destroy() from destructor because unneeded for std containers
  • removed SupportArg
  • added looped events
  • removed ProcessStatus
  • disabled DelayEvent for looped events

 

The usage is remained unchanged, except the support for looped timers.

	// Create the event and call it after 5 seconds. Using "this" as argument is safe only if it's a singleton, for CHARACTER or CItem, use their vid and find them inside the lambda.
	CEventFunctionHandler::instance().AddEvent([this](SArgumentSupportImpl*) {
			this->SendNotificationToAll();
		}, "MY_BEAUTIFUL_EVENT", std::chrono::seconds(5).count()
	);

	// Create the event and loop it every 5 minutes. Don't forget, keys already existing will be skipped, such as this one.
	CEventFunctionHandler::instance().AddEvent([this](SArgumentSupportImpl*) {
			this->SendNotificationToAll();
		}, "MY_BEAUTIFUL_EVENT", std::chrono::minutes(5).count(), true
	);

	// Check if it exists, then delay it again by 5s. Don't forget, DelayEvent has no effect to looped events.
	if (CEventFunctionHandler::instance().FindEvent("MY_BEAUTIFUL_EVENT"))
		CEventFunctionHandler::Instance().DelayEvent("MY_BEAUTIFUL_EVENT", std::chrono::seconds(5).count());

	// Cancel the event. Safe even if the key doesn't exist.
	CEventFunctionHandler::Instance().RemoveEvent("MY_BEAUTIFUL_EVENT");

 

  • Metin2 Dev 41
  • Flame 1
  • Good 2
  • muscle 1
  • Love 15
Link to comment
https://metin2.dev/topic/17432-reimplementation-of-events/#findComment-167484
Share on other sites

  • 4 months later...
On 10/14/2024 at 2:03 AM, martysama0134 said:

Some people requested me to refactor this library for c++20 without invalidating the old syntax.

This is the hidden content, please

This is the hidden content, please

Here are the changes:

  • changed std::string_view instead of std::string handling
  • changed std::unordered_map instead of std::map
  • supported new foreach pair syntax
  • added missing const-ness
  • rewritten CEventFunctionHandler::Process with std::erase_if
  • replaced copy with impactless move for std::function
  • removed Destroy() from destructor because unneeded for std containers
  • removed SupportArg
  • added looped events
  • removed ProcessStatus
  • disabled DelayEvent for looped events

 

The usage is remained unchanged, except the support for looped timers.

	// Create the event and call it after 5 seconds. Using "this" as argument is safe only if it's a singleton, for CHARACTER or CItem, use their vid and find them inside the lambda.
	CEventFunctionHandler::instance().AddEvent([this](SArgumentSupportImpl*) {
			this->SendNotificationToAll();
		}, "MY_BEAUTIFUL_EVENT", std::chrono::seconds(5).count()
	);

	// Create the event and loop it every 5 minutes. Don't forget, keys already existing will be skipped, such as this one.
	CEventFunctionHandler::instance().AddEvent([this](SArgumentSupportImpl*) {
			this->SendNotificationToAll();
		}, "MY_BEAUTIFUL_EVENT", std::chrono::minutes(5).count(), true
	);

	// Check if it exists, then delay it again by 5s. Don't forget, DelayEvent has no effect to looped events.
	if (CEventFunctionHandler::instance().FindEvent("MY_BEAUTIFUL_EVENT"))
		CEventFunctionHandler::Instance().DelayEvent("MY_BEAUTIFUL_EVENT", std::chrono::seconds(5).count());

	// Cancel the event. Safe even if the key doesn't exist.
	CEventFunctionHandler::Instance().RemoveEvent("MY_BEAUTIFUL_EVENT");

 

CEventFunctionHandler::Instance().AddEvent([pkKiller](SArgumentSupportImpl*) { pkKiller->TimeOver(2); }, "DEMON_TOWER_EVENT", std::chrono::seconds(10).count());

A function was developed that sends me a message with WHISPER after 10 seconds. But it doesn't work. Can the LPCHARACTER pointer be used?

Example:

char_battle.cpp ::Dead -> pkKiller

Edited by BadRomani
Link to comment
https://metin2.dev/topic/17432-reimplementation-of-events/#findComment-169760
Share on other sites

  • 1 month later...
  • Honorable Member
On 3/3/2025 at 6:47 PM, BadRomani said:

CEventFunctionHandler::Instance().AddEvent([pkKiller](SArgumentSupportImpl*) { pkKiller->TimeOver(2); }, "DEMON_TOWER_EVENT", std::chrono::seconds(10).count());

A function was developed that sends me a message with WHISPER after 10 seconds. But it doesn't work. Can the LPCHARACTER pointer be used?

Example:

char_battle.cpp ::Dead -> pkKiller

Never capture pointers in lambdas if it's not called somewhere inside the function where you created it (like an std::for_each or something), by this I also mean capturing "this" or "&". If the object gets destroyed, it becomes a dangling pointer causing a crash when you dereference it.

Capture the PID instead and try to find the corresponding LPCHARACTER from CHARACTER_MANAGER.

Edited by Distraught
  • Metin2 Dev 1

992404397646696589.png
Former C++ Developer at Gameloft on DML
Join my Discord: Distraught Labs

Link to comment
https://metin2.dev/topic/17432-reimplementation-of-events/#findComment-170158
Share on other sites

On 4/3/2025 at 10:18 PM, Distraught said:

Never capture pointers in lambdas if it's not called somewhere inside the function where you created it (like an std::for_each or something), by this I also mean capturing "this" or "&". If the object gets destroyed, it becomes a dangling pointer causing a crash when you dereference it.

Capture the PID instead and try to find the corresponding LPCHARACTER from CHARACTER_MANAGER.

Yes, I did that later. I tried to find the player's pid.

Link to comment
https://metin2.dev/topic/17432-reimplementation-of-events/#findComment-170247
Share on other sites

Don't use any images from : imgur, turkmmop, freakgamers, inforge, hizliresim... Or your content will be deleted without notice...
Use : https://metin2.download/media/add/

Please use https://metin2.download/ when uploading files smaller than 100MB, otherwise the approval will take longer due to manual upload.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.