View Single Post
^SmileY
Veteran Member
Join Date: Jan 2010
Location: Brazil [<o>]
Old 11-13-2019 , 16:54   Re: Make tasks inside ServerFrame loop
Reply With Quote #3

My doubt is just that, if is safe to check time inside Serverframe to do tasks.

I have created a small class, now is:

Code:
#include "Task.h"
#include "amxxmodule.h"

cTask gTask;

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

void cTask::SetTask(int Index,float Time,bool Loop,void *FunctionCallback,int Param)
{
	if(!this->TaskExists(Index))
	{
		pTaskInfo Info = 
		{
			Index,
			Time,
			gpGlobals->time + Time,
			Loop,
			FunctionCallback,
			Param
		};

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

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

void cTask::RemoveTask(int Index)
{
	auto it = this->m_Data.find(Index);

	if(it != this->m_Data.end())
	{
		this->m_Data.erase(it);
	}
}

void cTask::Run() // StartFrame_Post
{
	for(auto it = this->m_Data.begin();it != this->m_Data.end();it++)
	{
		if(gpGlobals->time >= it->second.EndTime)
		{
			((void(*)(int))it->second.FunctionCallback)(it->second.Param);

			if(it->second.Loop)
			{
				it->second.EndTime += it->second.Time;
			}
			else
			{
				it = this->m_Data.erase(it);
			}
		}
		else
		{
			it++;
		}
	}
}
.h
Code:
#pragma once

#include <map>

typedef struct
{
	int		Index;
	float	Time;
	float	EndTime;
	bool	Loop;
	void	*FunctionCallback;
	int		Param;
} pTaskInfo, *lpTaskInfo;

class cTask
{
public:
	cTask();

	void SetTask(int Index,float Time,bool Loop,void *FunctionCallback,int Param);
	bool TaskExists(int Index);
	void RemoveTask(int Index);

	void Run();

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

extern cTask gTask;
I hope that is correct, since i never coded on amxx module before.

Thanks
__________________
Projects:

- See my Git Hub: https://github.com/SmileYzn
PHP Code:
set_pcvar_num(pCvar, !get_pcvar_num(pCvar)); 
^SmileY is offline
Send a message via MSN to ^SmileY Send a message via Skype™ to ^SmileY