Raised This Month: $ Target: $400
 0% 

Make tasks inside ServerFrame loop


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
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
 



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 21:52.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode