Is there a better way ?
#1

Im making a script that need an OnPlayerWorldChange callback. I know this callback doesnt exist, so i built my own using OnPlayerUpdate, which can be found below. It works, but im just worried its going to put a lot of strain on my server (The OnPlayerWolrdChange needs to have a lot of checks/loops/functions ).

Is there a more efficient way to do this?

Код:
public OnPlayerUpdate(playerid)
{
	pData[playerid][WorldCheck] = GetPlayerVirtualWorld(playerid);
	
	if(pData[playerid][WorldCheck] != pData[playerid][NewWorld])
	{
	  pData[playerid][OldWorld] = pData[playerid][NewWorld];
	  pData[playerid][NewWorld] = pData[playerid][WorldCheck];
	  
	  OnPlayerWorldChange(playerid, pData[playerid][NewWorld], pData[playerid][OldWorld]);
	}
	return 1;
}
Reply
#2

well yeah, since OnPlayerUpdate is called like 40-60 times a seconds from what I heard.

what causes a player to change virtual world? I would have the OnPlayerWorldChange there not OnPlayerUpdate
Reply
#3

Why would you need It?
If It's for user stats, just save it under OnPlayerDisconnect or use a timer.
https://sampwiki.blast.hk/wiki/SetTimer

pawn Код:
// TOP OF YOUR SCRIPT
forward CheckPlayerWorldChange(playerid);

// Under OnGameModeInit
SetTimer("CheckPlayerWorldChange",1000,true); // True = Repeat False = Once (One Second Timer)

// Change 1000 to 500 If you want It to check twice / second
// Change 1000 to 250 If you want it to check four times / second
// Change 1000 to 125 If you want it to check 8 times / second
// And so on....


// Anywhere (NOT under any callback)
public CheckPlayerWorldChange(playerid)
{
    pData[playerid][WorldCheck] = GetPlayerVirtualWorld(playerid);
   
    if(pData[playerid][WorldCheck] != pData[playerid][NewWorld])
    {
      pData[playerid][OldWorld] = pData[playerid][NewWorld];
      pData[playerid][NewWorld] = pData[playerid][WorldCheck];
     
      OnPlayerWorldChange(playerid, pData[playerid][NewWorld], pData[playerid][OldWorld]);
    }
    return 1;
}
This will (if your code works) check If the player changed world every second.

Reply
#4

Quote:
Originally Posted by cessil
well yeah, since OnPlayerUpdate is called like 40-60 times a seconds from what I heard.

what causes a player to change virtual world? I would have the OnPlayerWorldChange there not OnPlayerUpdate
Many events cause players to switch virtual worlds. Thats my problem lol. i guess i COULD go through all my filterscripts/my gm, and add a callremotefunction line before the SetVirtualWorld functions, but i would rather find a different way .

Is there any way i can redefine SetVirtualWorld to a custom SetVirtualWorldEx ? (i tried to hook them, but failed due to not knowing much about the preprocessor ).
Reply
#5

Quote:
Originally Posted by Kyosaur!!
Quote:
Originally Posted by cessil
well yeah, since OnPlayerUpdate is called like 40-60 times a seconds from what I heard.

what causes a player to change virtual world? I would have the OnPlayerWorldChange there not OnPlayerUpdate
Many events cause players to switch virtual worlds. Thats my problem lol. i guess i COULD go through all my filterscripts/my gm, and add a callremotefunction line before the SetVirtualWorld functions, but i would rather find a different way .

Is there any way i can redefine SetVirtualWorld to a custom SetVirtualWorldEx ? (i tried to hook them, but failed due to not knowing much about the preprocessor ).
Just use the timer I made. Should work without issues.
Reply
#6

Quote:
Originally Posted by mavtias
Why would you need It?
If It's for user stats, just save it under OnPlayerDisconnect or use a timer.
https://sampwiki.blast.hk/wiki/SetTimer

pawn Код:
// TOP OF YOUR SCRIPT
forward CheckPlayerWorldChange(playerid);

// Under OnGameModeInit
SetTimer("CheckPlayerWorldChange",1000,true); // True = Repeat False = Once (One Second Timer)

// Change 1000 to 500 If you want It to check twice / second
// Change 1000 to 250 If you want it to check four times / second
// Change 1000 to 125 If you want it to check 8 times / second
// And so on....


// Anywhere (NOT under any callback)
public CheckPlayerWorldChange(playerid)
{
    pData[playerid][WorldCheck] = GetPlayerVirtualWorld(playerid);
   
    if(pData[playerid][WorldCheck] != pData[playerid][NewWorld])
    {
      pData[playerid][OldWorld] = pData[playerid][NewWorld];
      pData[playerid][NewWorld] = pData[playerid][WorldCheck];
     
      OnPlayerWorldChange(playerid, pData[playerid][NewWorld], pData[playerid][OldWorld]);
    }
    return 1;
}
This will (if your code works) check If the player changed world every second.

Hmmm 3 things lol.

1. Not really looking to use timers (not a bad idea ... just dont want to use them/onplayerupdate if there's another way.)
2. Your using SetTimer on a public with parameters, that wont work.
3. If i wanted to use timers ... wouldnt be much easier to call OnPlayerWorldChange all on its own :P?

What i plan on doing with this OnPlayerWorldChange callback cant really be done with timers (even if i set the time to 500ms, still wouldnt work ). OnPlayerUpdate works fine (even thought i need to add it/all my variables to every script i use want to use my callback in), but im really worried about lag aha.

So im hoping there's a way to just hook SetPlayerVirtualWorld to a custom SetPlayerVirtualWorldEx... would be a lot easier on me.


Reply
#7

Well you could make it easy for yourself

forward OnPlayerWorldChange(playerid,worldid);

pawn Код:
stock SetPlayerVirtualWorldn(playerid,worldid)
{
SetPlayerVirtualWorld(playerid,worldid);
OnPlayerWorldChange(playerid,worldid);
return 1;
}
then you have the callback onplayerworldchange and a new modified SetPlayerVirtualWorld called SetPlayerVirtualWorldn just click CTRL + h which shows up a small box which can be used to quickly change 1 type of code to another with a few clicks.

Then just remove the additional "n" that will come.


Or even better put it in a include file
Reply
#8

It shouldn't be a problem, i saw someone putting a whole speedometer script under OnPlayerUpdate and it works fine without any lag, but the download/upload brandwith is high.
Reply
#9

Quote:
Originally Posted by Desert
...
pawn Код:
forward OnPlayerVirtualWorldChange(playerid, oldworld, newworld);
public OnPlayerVirtualWorldChange(playerid, oldworld, newworld)
{
  printf("player %d's virtual world changed from %d to %d", playerid, oldworld, newworld);
  return 1;
}

stock SetPlayerVirtualWorldEx(playerid, worldid)
{
  OnPlayerVirtualWorldChange(playerid, GetPlayerVirtualWorld(playerid), worldid);
  return SetPlayerVirtualWorld(playerid, worldid);
}

#define SetPlayerVirtualWorld SetPlayerVirtualWorldEx
With this you don't need to edit your code.
Reply
#10

Quote:
Originally Posted by Finn
Quote:
Originally Posted by Desert
...
pawn Код:
forward OnPlayerVirtualWorldChange(playerid, oldworld, newworld);
public OnPlayerVirtualWorldChange(playerid, oldworld, newworld)
{
  printf("player %d's virtual world changed from %d to %d", playerid, oldworld, newworld);
  return 1;
}

stock SetPlayerVirtualWorldEx(playerid, worldid)
{
  OnPlayerVirtualWorldChange(playerid, GetPlayerVirtualWorld(playerid), worldid);
  return SetPlayerVirtualWorld(playerid, worldid);
}

#define SetPlayerVirtualWorld SetPlayerVirtualWorldEx
With this you don't need to edit your code.
Thats what im looking for lol.

Thanks for the help everyone, especially you Mr.Finn.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)