Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

Create a Manager class with its own thread and loop

Dbo Dev
Joined
Sep 19, 2009
Messages
921
Reaction score
191
In this tutorial I will show you how you create a Manager class for example players,monsters, npcs, objects etc etc.

As example we will create a player manager function.


Ok first we create 2 files called PlayerManager.h and PlayerManager.cpp
Daneos - Create a Manager class with its own thread and loop - RaGEZONE Forums


Now open the PlayerManager.h and paste this into it:
Code:
#pragma once

#include "PlayerInfos.h"
#include "NtlThread.h"

class CPlayerManager : /*public PlayerInfos,*/ public CNtlRunObject
{

public:
	CPlayerManager();
	~CPlayerManager();

public:
	void			Init();
	void			Release();


	void			CreatePlayerThread();
	void			Run();
	


private:
	bool m_bRun;
	CNtlThread * pThread;
};
extern CPlayerManager * g_pPlayerManager;

Now safe and open GameServer.h and go to the top of the file where you can see
Code:
#pragma once
bellow it you write
Code:
#include "PlayerManager.h"

So it will look like this:
Code:
#pragma once
#include "PlayerManager.h"


Now you go to the function:
Code:
int					OnAppStart()

In Akcore files its looking like this:
Code:
int					OnAppStart()
	{
		dwThreadId = 0;
		if(CreateTableContainer(1))
		{
			return NTL_SUCCESS;
		}else{
		printf("FAILED LOADING TABLES !!! \n");
		return NTL_SUCCESS;
		}
	}

change to:
Code:
int					OnAppStart()
	{
		dwThreadId = 0;
		if(CreateTableContainer(1))
		{
			NTL_PRINT(PRINT_APP, "Create Player Manager");
			g_pPlayerManager = new CPlayerManager;
			g_pPlayerManager->Init();
			return NTL_SUCCESS;
		}else{
		printf("FAILED LOADING TABLES !!! \n");
		return NTL_SUCCESS;
		}
	}

Now we work on PlayerManager.cpp file
Code:
// PlayerManager.cpp: implementation of the CPlayerManager class.
// By Daneos @ RageZone
//////////////////////////////////////////////////////////////////////

#include "Stdafx.h"
#include "GameServer.h"


CPlayerManager* g_pPlayerManager = NULL;

CPlayerManager::CPlayerManager()
{
	m_bRun = true;
}

CPlayerManager::~CPlayerManager()
{
	Release();
}

void CPlayerManager::Init()
{	
	CreatePlayerThread();
}

void CPlayerManager::Release()
{	

}


void CPlayerManager::Run()
{
	CGameServer * app = (CGameServer*) NtlSfxGetApp();
	while( m_bRun )
	{


	Sleep(100);
	}
}

void CPlayerManager::CreatePlayerThread()
{

	pThread = CNtlThreadFactory::CreateThread( this , "CPMThread" ); 
	pThread->Start();

}


Now your player manager should be done!

You have now your own thread and a playermanager.

What is this good for?
Example:

There is a loop running, you can add your player list into there and do checks for every player without issues!

Example:

Code:
void CPlayerManager::Run()
{
	CGameServer * app = (CGameServer*) NtlSfxGetApp();
	while( m_bRun )
	{
	for( itertype(g_pUserTable->m_map_Player) it = g_pUserTable->m_map_Player.begin(); it != g_pUserTable->m_map_Player.end(); it++ )
		{
			PlayerInfos* pPlayer = it->second;
			if (pPlayer)
			{
				// pPlayer->CheckIfPcHealth();
			}
		}

	Sleep(100);
	}
}

pPlayer->CheckIfPcHealth()
^
this could be a function to check players health and do whatever you want.

You also can add timers (which is a must)
Code:
if( (timeGetTime() - pPlayer->dwRecoverLpEpCheckTime) >= 2000)
	{
	}

Code:
void CPlayerManager::Run()
{
	CGameServer * app = (CGameServer*) NtlSfxGetApp();
	while( m_bRun )
	{
	for( itertype(g_pUserTable->m_map_Player) it = g_pUserTable->m_map_Player.begin(); it != g_pUserTable->m_map_Player.end(); it++ )
		{
			PlayerInfos* pPlayer = it->second;
			if (pPlayer)
			{
				if( (timeGetTime() - pPlayer->dwRecoverLpEpCheckTime) >= 2000)
				{
					// pPlayer->CheckIfPcHealth();
				}
			}
		}

	Sleep(100);
	}
}
 
Elite Diviner
Joined
May 26, 2014
Messages
482
Reaction score
32
If don't work the part of pPlayer do this...

Code:
		for( CGameServer::USERIT it = app->m_userList.begin(); it != app->m_userList.end(); it++ )
		{
			PlayerInfos* plr = it->second->plr;
			if (plr)
		}
 
Dbo Dev
Joined
Sep 19, 2009
Messages
921
Reaction score
191
The player list(map) is just an example and ofc will not work XD
 
Back
Top