GetPlayerState Question
#1

Hey again guys, quick query for you,

Why use 'GetPlayerState(playerid)' over and over instead of just storing the players current state gathered from OnPlayerStateChange within a variable for that player?

Is there a reason for this?, am I missing something?
Reply
#2

It's the same thing.
Reply
#3

Quote:
Originally Posted by oMa37
Посмотреть сообщение
It's the same thing.
Alright I thought that too.. well thanks very much for clearing that up!
Reply
#4

It's not the same... Sure you'll end up with the same result, but the native being checked over and over doesn't need a global variable made, so therefore it's simpler.

OnPlayerStateChange makes it so it's an event. You can put code in there to be run upon the change. If you were to do the same via the function you'd need a timer to check over and over again on the players state.
Reply
#5

The player variable doesn't worry me as much.. I was merely trying to avoid the constant calls to gather state information from players, especially when I have so many cases where GetPlayerState is used within many timed functions, commands and many native call backs.. without even starting on filterscripts.. includes an so on.

Also, there isn't much point in getting it and getting and getting it.. when you could really already have it.
Reply
#6

I guess GetPlayerState is like GetPlayerPoolSize where it does calculation(s). What you can do is hook GetPlayerState and make it return the value of an array index, and create a function such as UpdatePlayerState then call it when the player's state changes.

You're not really going to notice any difference, but it will be easier to code as you won't require a local variable.
Reply
#7

Quote:
Originally Posted by Killa[DGZ]
Посмотреть сообщение
Why use 'GetPlayerState(playerid)' over and over instead of just storing the players current state gathered from OnPlayerStateChange within a variable for that player?
To answer this specifically; because it's redundant. If it's a local variable you will use multiple times in a single function call then sure. Otherwise you're wasting memory for no reason.

That's like asking why we would use GetPlayerHealth when we can store it in a variable. If you need the variable use it. Otherwise stick to the native calls.
Reply
#8

Quote:
Originally Posted by nG Inverse
Посмотреть сообщение
That's like asking why we would use GetPlayerHealth when we can store it in a variable. If you need the variable use it. Otherwise stick to the native calls.
It's nothing like that mate, you simply can't obtain a players current health without the use of GetPlayerHealth at some point in time.. unless you assume that it's 100 from the time of connect, which isn't very reliable.

This is more like saving the players name on connect so you don't have a bajillion useless calls of GetPlayerName throughout your script.



Here is a very primitive example of state collection incase you didn't quite get what I'm on about,

A variable to store the state within,
PHP код:
new gPlayerCurrentState[MAX_PLAYERS]; 
Gathering the current state,
PHP код:
/*
0     PLAYER_STATE_NONE     Empty (while initializing)
1     PLAYER_STATE_ONFOOT     Player is on foot
2     PLAYER_STATE_DRIVER     Player is the driver of a vehicle
3     PLAYER_STATE_PASSENGER     Player is passenger of a vehicle
4     PLAYER_STATE_EXIT_VEHICLE     (unused)used internally
5     PLAYER_STATE_ENTER_VEHICLE_DRIVER     (unused)used internally
6     PLAYER_STATE_ENTER_VEHICLE_PASSENGER     (unused)used internally
7     PLAYER_STATE_WASTED     Player is dead or on class selection
8     PLAYER_STATE_SPAWNED     Player is spawned
9     PLAYER_STATE_SPECTATING     Player is spectating*/
public OnPlayerStateChange(playeridnewstateoldstate)
{
    if(
newstate == PLAYER_STATE_NONE)
    {
        
gPlayerCurrentState[playerid] = 0;
    }
    if(
newstate == PLAYER_STATE_ONFOOT)
    {
        
gPlayerCurrentState[playerid] = 1;
    }
    if(
newstate == PLAYER_STATE_DRIVER)
    {
        
gPlayerCurrentState[playerid] = 2;
    }
    if(
newstate == PLAYER_STATE_PASSENGER)
    {
        
gPlayerCurrentState[playerid] = 3;
    }
    if(
newstate == PLAYER_STATE_WASTED)
    {
        
gPlayerCurrentState[playerid] = 7;
    }
    if(
newstate == PLAYER_STATE_SPAWNED)
    {
        
gPlayerCurrentState[playerid] = 8;
    }
    if(
newstate == PLAYER_STATE_SPECTATING)
    {
        
gPlayerCurrentState[playerid] = 9;
    }
    return 
1
Using the stored state we've already collected,
PHP код:
public OnPlayerKeyStateChange(playeridnewkeysoldkeys)
{
    if (
newkeys KEY_SPRINT)
    {
        if(
gPlayerCurrentState[playerid] == PLAYER_STATE_DRIVER)
        {
            
//do something
        
}
    }
    return 
1
Also with this I'm just assuming 'GetPlayerState' is gathered directly from the client?, so wouldn't avoiding this be more efficient for both client and server?, especially when GetPlayerState is being called for my players and sometimes all players depending on the situation at 200ms, 500ms, 1000ms, 3000ms, thats without command calls containing GetPlayerState, native callbacks,other filterscripts and includes..

It really just seems useless to keep get get getting it constantly, as for the memory usage.. I couldn't honestly care less about that, my server uses 60mb of ram at the best of times..
Reply
#9

Let's remove all the unnecessary calls under OnPlayerStateChange and the pointless variable memory usage:
pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(newkeys & KEY_SPRINT)
    {
        switch(GetPlayerState(playerid))
        {
            case PLAYER_STATE_DRIVER:
            {
                // do something
            }
        }
    }
    return 1;
}
Even to simplify your code if you chose to stick to your current method
pawn Код:
public OnPlayerStateChange(playerid, newsate, oldstate)
{
    g_CurrentPlayerState[playerid] = newstate;
    return 1;
}
Reply
#10

I did say a primitive example, that is not my actual method right there.. but you've posted exactly how I've gone about it anyway, I've also added the 3 internal states to the system, from exitvehicle & entervehicle(passenger/driver).. as they will also need to be used to make the system work correctly, say if someone bails out.. for a short time changing states between driver and onfoot they're still assumed as driver due to the current stored state.


Back on topic, so why use GetPlayerState over this method?, no reason?.. apart from a single player variable?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)