Raised This Month: $7 Target: $400
 1% 

Make tasks inside ServerFrame loop


Post New Thread Reply   
 
Thread Tools Display Modes
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
TheDS1337
Veteran Member
Join Date: Jun 2012
Old 11-13-2019 , 14:58   Re: Make tasks inside ServerFrame loop
Reply With Quote #2

What is not sane about it? SM (and I think AMXx too) both use OnGameFrame to check for timers (tasks). and then execute them one by one depending on plugin order. this if more than one plugin are supposed to be executed at the same time.
TheDS1337 is offline
^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
TheDS1337
Veteran Member
Join Date: Jun 2012
Old 11-14-2019 , 01:47   Re: Make tasks inside ServerFrame loop
Reply With Quote #4

If the looping is what you are worried about, then it is totally safe, as both SM and AMXx does it that one.

Also,
Code:
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++;
		}
	}
}
I don't uderstand why are you incrementing it here if the task time is yet to come? in some cases it may even increment it twice because the loop does it already:

Code:
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);
			}
		}		
	}
}
This should suffice.
TheDS1337 is offline
^SmileY
Veteran Member
Join Date: Jan 2010
Location: Brazil [<o>]
Old 11-14-2019 , 07:14   Re: Make tasks inside ServerFrame loop
Reply With Quote #5

My bad, i forgot to remove it when paste here.

Other small doubt when coding for modules or metamod, is how to check if player is connected or entity is not null:

PHP Code:
void ClientPutInServer_Post(edict_t *pEdict
Thanks for help.
__________________
Projects:

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

Last edited by ^SmileY; 11-14-2019 at 07:24.
^SmileY is offline
Send a message via MSN to ^SmileY Send a message via Skype™ to ^SmileY
Reply


Thread Tools
Display Modes

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 23:01.


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