C++ wait before calling a function in a SAMPFUNCS plugin

Suicide Bomb

Active member
Joined
Jun 22, 2014
Messages
100
Reaction score
1
Let's say I've got an a() function that's bound to a command (like /something) and I have another function, let's call it function(), that I want to call inside a() function with a 3 second delay. How do I do that? Thank you for your answers!
 

Suicide Bomb

Active member
Joined
Jun 22, 2014
Messages
100
Reaction score
1
supahdupahnubah said:
Sleep(3000)?

Problem with that is that it freezes the main thread as well, meaning the game won't update anything (0 FPS) until 3000 milliseconds have passed.
 
Joined
Dec 31, 2015
Messages
712
Reaction score
27
Suicide Bomb said:
supahdupahnubah said:
Sleep(3000)?

Problem with that is that it freezes the main thread as well, meaning the game won't update anything (0 FPS) until 3000 milliseconds have passed.

Well then create a new thread for that
 
Joined
Feb 18, 2005
Messages
2,963
Reaction score
267
That means it waits for the command func. to return, so you can't really wait there.
Use a timer class, toggle a bool etc. and call the command from the main loop.
 

0x32789

Expert
Joined
May 26, 2014
Messages
849
Reaction score
51
Location
LongForgotten <-> 0x32789
I came across something like this when I was modifying m0d_sa..
Just use gettickcount, try this pseudo code:
1) at top of script, declare a DWORD variable which will store last tick, set it to -1 default value (DWORD last_tick = -1;)
2) in your command, set the variable we just created right now value to GetTickCount(); (last_tick = GetTickCount();)
3) in the main loop, check if last_tick isnt -1 (last_tick != -1) and if last tick is under the current tick count - 3000, Why 3000? because it should be in MS (milisecond), this is the time you want to be isn't it? 3 seconds = 3000 ms [if(last_tick < (GetTickCount() - 3000))] ( if(last_tick != -1 && last_tick < (GetTickCount() - 3000)) )
4) after the IF, inside it add last_tick = -1; (reset it) and then execute ur code for the command.

if your not having more timers in ur script which uses same method then, add a variable which will be a bool and tell which timer is running right now.
 

monday

Expert
Joined
Jun 23, 2014
Messages
1,125
Reaction score
149
you could make your own timer class in 2 separate files (cpp + h) and then include them in projects that require them. Here's how I did it:

Ctimer.cpp content:
Code:
#include "stdafx.h"
#include "Ctimer.h"

Timer timer;

bool Timer::set(int num, DWORD ms)
{
	if(startTime[num] == NULL)
	{
		startTime[num] = GetTickCount();
	}
	else
	{
		if(ms < ( (GetTickCount() ) - startTime[num]) )
		{
			startTime[num] = NULL;
			return true; //the given time has passed
		}
	}
	return false;
}

Ctimer.h content:
Code:
#ifndef CTIMER_H
#define CTIMER_H

#include <iostream>
#include <Windows.h>
using namespace std;

class Timer{
public:
	
	bool set(int, DWORD);

private:
	DWORD startTime[100];
};

extern Timer timer;

#endif


How to use it in your project:
Code:
#include "Ctimer.h" //at the top

While(true)
{ 
    if(timer.set(0, 300)) //timer(unique timer index, time delay in ms)
    {
        FunctionThatHasToBeCalledEvery300milliseconds();
    }
}
 

Suicide Bomb

Active member
Joined
Jun 22, 2014
Messages
100
Reaction score
1
Thank you, monday.
However, the case is not that I want to call a function every 3 seconds. I just want to delay calling a function by 3 seconds, without using the main loop if possible.
 
Top