View Single Post
Author Message
^SmileY
Veteran Member
Join Date: Jan 2010
Location: Brazil [<o>]
Old 11-12-2019 , 17:04   Make tasks inside ServerFrame loop
Reply With Quote #1

Hi folks, it is sane to make a class to create 'Tasks' in a metamod plugin using this:

.cpp
Code:
#include "Task.h"

cTask gTask;

cTask::cTask()
{
	this->m_Data.clear();
}

void cTask::SetTask(int iIndex,float fTime,bool bLoop)
{
	if(!this->TaskExists(iIndex))
	{
		pTaskInfo pInfo = 
		{
			iIndex,
			fTime,
			gpGlobals->time + fTime,
			bLoop
		};

		this->m_Data.insert(std::pair<int, pTaskInfo>(iIndex, pInfo));
	}
}

bool cTask::TaskExists(int iIndex)
{
	return (this->m_Data.find(iIndex) != this->m_Data.end());
}

void cTask::Run() // StartFrame_Post
{
	for(auto it = this->m_Data.begin();it != this->m_Data.end();it++)
	{
		if(gpGlobals->time >= it->second.fEndTime)
		{
			////////////////////////////
			// Run Function an function
			////////////////////////////

			if(it->second.bLoop)
			{
				it->second.fEndTime += it->second.fTime;
			}
			else
			{
				it = this->m_Data.erase(it);
			}
		}
		else
		{
			it++;
		}
	}
}
.h
Code:
#pragma once

typedef struct
{
	int		iIndex;
	float	fTime;
	float	fEndTime;
	bool	bLoop;
} pTaskInfo, *lpTaskInfo;

class cTask
{
public:
	cTask();

	void SetTask(int iIndex,float fTime,bool bLoop);
	bool TaskExists(int iIndex);

	void Run();

private:
	std::map<int, pTaskInfo> m_Data;
};

extern cTask gTask;
Ps.
I'm using StartFrame_Post to run the ::Run of tasks.
If is a sane thing, how i pass a function as parameter to SetTask or something like else?
__________________
Projects:

- See my Git Hub: https://github.com/SmileYzn
PHP Code:
set_pcvar_num(pCvar, !get_pcvar_num(pCvar)); 

Last edited by ^SmileY; 11-12-2019 at 17:05.
^SmileY is offline
Send a message via MSN to ^SmileY Send a message via Skype™ to ^SmileY