Check every 5s help
#1

Hi... Idk how to check for something every 5s. I made a script and now i want to check every 5s for it. Thanks
Reply
#2

Use a timer for it:
pawn Код:
// global:
new Something_Timer;

// in OnGameModeInit or OnFilterScriptInit (depending on your mode):
Something_Timer = SetTimer("OnSomethingUpdate", 5000, true);

// in OnGameModeExit or OnFilterScriptExit (the same as above):
KillTimer(Something_Timer);

forward OnSomethingUpdate();
public OnSomethingUpdate()
{
    // code to check for something..
}
You can replace "Something" with its name of it, I used it as example only.
Reply
#3

The only thing I'd like to say is to make a timer with 5,000 interval, you also need a callback to make everything work, the code below is an example of what I am talking.

pawn Код:
SetTimerEx("funcname", 100 * 10 * 5, true, "i", playerid); //Use SetTimer if you're not using any arguements.

forward funcname(playerid);
public funcname(playerid)
{
    //code here
    return true;
}
EDIT: Konstantinos beat me
Reply
#4

Im totally new to scripting could this work?
Код:
// This is a comment
// uncomment the line below if you want to write a filterscript
//#define FILTERSCRIPT

#include <a_samp>
new Something_Timer;
new b=30;

public OnFilterScriptInit()
{
Something_Timer = SetTimer("OnSomethingUpdate", 5000, true);

	return 1;
}

public OnFilterScriptExit()
{
KillTimer(Something_Timer);
	return 1;
}
forward OnSomethingUpdate(playerid);
public OnSomethingUpdate(playerid)
{   new Float:x, Float:y, Float:z;
    SendClientMessage(playerid,-1,"Preostalo vam je jos kiseonika za 15s ronjenja");

    GetPlayerPos(playerid, x, y, z);
    if(z<=0){
    b=b-5;
    if (b=15){
	SendClientMessage(playerid,-1,"Preostalo vam je jos kiseonika za 15s ronjenja");

	}
	if (b=5){
    SendClientMessage(playerid,-1,"Preostalo vam je jos kiseonika za 5s ronjenja");
	}
	if (b=0){
	SetPlayerHealth(playerid, 0);
	b=30;
	}


	}
	if (z>0){
	b=30;
	}

}
Reply
#5

If you want to use parameters such as playerid, you'll need to use SetTimerEx. But I recommend you to use SetTimer and a loop through the players:
pawn Код:
#define FILTERSCRIPT

#include <a_samp>

new Something_Timer;
new b=30;

public OnFilterScriptInit()
{
    Something_Timer = SetTimer("OnSomethingUpdate", 5000, true);
    return 1;
}

public OnFilterScriptExit()
{
    KillTimer(Something_Timer);
    return 1;
}

forward OnSomethingUpdate();
public OnSomethingUpdate()
{
    new Float:x, Float:y, Float:z;
    for (new playerid; playerid != MAX_PLAYERS; ++playerid)
    {
        if (!IsPlayerConnected(playerid)) continue;
        SendClientMessage(playerid,-1,"Preostalo vam je jos kiseonika za 15s ronjenja");
        GetPlayerPos(playerid, x, y, z);
        if(z<=0)
        {
            b=b-5;
            if (b=15) SendClientMessage(playerid,-1,"Preostalo vam je jos kiseonika za 15s ronjenja");
            else if (b=5) SendClientMessage(playerid,-1,"Preostalo vam je jos kiseonika za 5s ronjenja");
            else if (b=0)
            {
                SetPlayerHealth(playerid, 0);
                b=30;
            }
        }
        if (z>0)
        {
            b=30;
        }
    }
}
Although I'm not so sure about the "b" symbol. It's the same for every person so it gets modified. If you want to be for each player, then use an array.
Reply
#6

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
If you want to use parameters such as playerid, you'll need to use SetTimerEx. But I recommend you to use SetTimer and a loop through the players:
pawn Код:
#define FILTERSCRIPT

#include <a_samp>

new Something_Timer;
new b=30;

public OnFilterScriptInit()
{
    Something_Timer = SetTimer("OnSomethingUpdate", 5000, true);
    return 1;
}

public OnFilterScriptExit()
{
    KillTimer(Something_Timer);
    return 1;
}

forward OnSomethingUpdate();
public OnSomethingUpdate()
{
    new Float:x, Float:y, Float:z;
    for (new playerid; playerid != MAX_PLAYERS; ++playerid)
    {
        if (!IsPlayerConnected(playerid)) continue;
        SendClientMessage(playerid,-1,"Preostalo vam je jos kiseonika za 15s ronjenja");
        GetPlayerPos(playerid, x, y, z);
        if(z<=0)
        {
            b=b-5;
            if (b=15) SendClientMessage(playerid,-1,"Preostalo vam je jos kiseonika za 15s ronjenja");
            else if (b=5) SendClientMessage(playerid,-1,"Preostalo vam je jos kiseonika za 5s ronjenja");
            else if (b=0)
            {
                SetPlayerHealth(playerid, 0);
                b=30;
            }
        }
        if (z>0)
        {
            b=30;
        }
    }
}
Although I'm not so sure about the "b" symbol. It's the same for every person so it gets modified. If you want to be for each player, then use an array.
Can u change it to array? Because idk how to store it for each player. I mean how do i know number of elements and how to check for each player? Thanks
Reply
#7

Let's say MAX_PLAYERS is 50. The array will have indexes from 0-49. Each index is unique for playerid.

pawn Код:
#define FILTERSCRIPT

#include <a_samp>
   
// If you don't want MAX_PLAYERS as 500:
//#undef MAX_PLAYERS
//#define MAX_PLAYERS (x)
// Change "x" to the number you want.

new Something_Timer;
new b[MAX_PLAYERS];

public OnFilterScriptInit()
{
    Something_Timer = SetTimer("OnSomethingUpdate", 5000, true);
    return 1;
}

public OnFilterScriptExit()
{
    KillTimer(Something_Timer);
    return 1;
}

public OnPlayerConnect(playerid)
{
    b[playerid] = 30;
    return 1;
}

forward OnSomethingUpdate();
public OnSomethingUpdate()
{
    new Float:x, Float:y, Float:z;
    for (new playerid; playerid != MAX_PLAYERS; ++playerid)
    {
        if (!IsPlayerConnected(playerid)) continue;
        SendClientMessage(playerid,-1,"Preostalo vam je jos kiseonika za 15s ronjenja");
        GetPlayerPos(playerid, x, y, z);
        if(z<=0)
        {
            b[playerid] -= 5;
            if (b[playerid]=15) SendClientMessage(playerid,-1,"Preostalo vam je jos kiseonika za 15s ronjenja");
            else if (b[playerid]=5) SendClientMessage(playerid,-1,"Preostalo vam je jos kiseonika za 5s ronjenja");
            else if (b[playerid]=0)
            {
                SetPlayerHealth(playerid, 0);
                b[playerid]=30;
            }
        }
        if (z>0)
        {
            b[playerid]=30;
        }
    }
}
About arrays: https://sampwiki.blast.hk/wiki/Scripting_Basics#Arrays
https://sampforum.blast.hk/showthread.php?tid=318212
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)