Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Query Players Within Script

  1. #1

    Default Query Players Within Script

    Hey all,

    Trying to get my old server back online and figured I'd try to brush up on my scripting....

    Is there a way, within a script, to get a list of all the current players that I can use to applied specific tasks based on who I find (I can't know the name ahead of time...)?

    I only know of 'netname local.player' for getting the (single) current player name.

    At this point I'm wondering if the only way would be to hack up the output of the status command somehow?

    Anyway thanks folks.

  2. #2
    Purple Developer Purple Elephant1au's Avatar
    Join Date
    Feb 2012
    Location
    Australia
    Posts
    1,269

    Default

    While in a script, all players are stored in the $player array of the game.

    If you open up and look at a view mods that are around, you see a similar piece of code that loops through all players

    for(local.i = 1; local.i <= $player.size; local.i++)
    {
    local.player=$player[local.i]
    if(local.player != NULL){
    // Do something with player
    local.name = netname local.player
    }
    }

    Purple's Playground
    OBJ :
    103.29.85.127:12203
    xfire: purpleelephant1au
    email: purpleelephant1au@gmail.com
    skydrive: PurpleElephantSkydrive




  3. #3

    Default

    Thank you o wise and purple elephant. You're right, I looked around and that expression is used just about everywhere. Evidently I have a lot to (re)learn.

    On a related note, do you know if it is possible to grab a incoming players info before they are added to the $player array? That way I can compare against the existing array and run commands based on what is found.

    Right now I'm looking into whatever triggers the "'player' is preparing for deployment" message, but I'm not sure if at that point their info has already been added to the array or not. I may just be wasting time.

    rambling edit: using 'connected local.player' seems to coincide with the player data already being added to the array. The "'player' is preparing for deployment" message appears to happen before this event registers.

    more rambling edit2: Maybe I'm going about this wrong, it looks like $player stores everything at any given time during the session. Is there a list somewhere of just what $player actually contains and what I can extract? I see 'local.connection_state = $player[1] getconnstate' will give a connection state so I'm going to start there.

    -BA
    Last edited by BuriedAlive; October 5th, 2019 at 01:10 PM.

  4. #4
    Purple Developer Purple Elephant1au's Avatar
    Join Date
    Feb 2012
    Location
    Australia
    Posts
    1,269

    Default

    Hi,

    $player would contain a list of Player Entities, ( http://homepage.tinet.ie/~abyrne/sdk...es.html#Player ) and commands added by the Reborn patch ( http://www.x-null.net/wiki/index.php...ommands#Player )

    The connection event would be your best bet to apply some 'commands' to a player as they enter your server.

    Taking a step back, what exactly are you trying to do?

    Purple's Playground
    OBJ :
    103.29.85.127:12203
    xfire: purpleelephant1au
    email: purpleelephant1au@gmail.com
    skydrive: PurpleElephantSkydrive




  5. #5

    Default

    " is preparing for deployment" is printed to console when a player begins to connect to a server (G_ClientConnect function in engine). The player can either be accepted or rejected by the server at this point. A new player entity is created in the game when a player has successfully connected (G_ClientBegin function in engine). The Reborn connected event will be during or after this, as it would be pretty useless checking for player connections without being able to reference the player that has connected. There's nothing you can use before this, as player entities are how we reference players in the game. When a new player entity is created, they are given the targetname of 'player', referenced in scripts via '$' + 'targetname' (e.g. $player). Any entity with a targetname (either provided by the engine or in a script) is referenced with '$', and it automatically becomes an array if more than one entity has the same targetname, starting at index 1.

    I'm not entirely sure what you want to do, but you can give them a property when they have connected that you can then check against.

    For example, whether you use Reborn events or tikis or loops to check for new players it doesn't really matter, when a player connects you can do:


    local.player.new = 1


    then somewhere down the line if you want to process new players you can do:


    for (local.i = 1; local.i <= $player.size; local.i++)
    {
    local.player = $player[local.i]
    if (local.player.new)
    {
    local.player.new = 0 // he's no longer new
    // do stuff
    }
    }
    Last edited by 1337Smithy; October 6th, 2019 at 10:54 AM.

  6. #6

    Default

    To do something to the player during the connection to the server you can use the command getconnstate.

    I put the example here from the documentation and a script you can use:

    Documentation:

    Code:
    getconnstate ( void )
    {
    
    
    Gets state of player's connection.
    Example:
    Code:
    
    local.connection_state = $player[1] getconnstate
    
    or
    
    local.connection_state = $player[1] getconnstate( )
    
    Result:
    Code:
    
    Returns integer value:
    
        0 = CS_FREE - given player slot is free
        1 = CS_ZOMBIE - given player slot is in zombie state (his data is still kept after he disconnected or lost connection)
        2 = CS_CONNECTED - player has connected to server, but he's not yet in the game
        3 = CS_PRIMED - player has passed through authorization checks and finished downloading any missing files
        4 = CS_ACTIVE - player is in game and can start playing 
    
    
    }
    Script


    while(1)
    {
    wait 0.02

    for(local.i = 1;local.i <= $player.size;local.i++)
    {

    local.player = $player[local.i]

    if(local.player == NULL)
    end

    local.connection_state = local.player getconnstate

    if(local.connection_state == 2)
    {
    //do something

    }

    }

    }

  7. #7

    Default

    Thanks, DK .

    But remember, BuriedAlive, the connection state information above is still after the "is preparing for deployment", and thus they are already part of the player array. If you want to just do checks against new vs already spawned players I wouldn't recommend looping over connection states (especially at a faster rate than per server frame).

  8. #8

    Default

    Hey thank you everybody.

    DoubleKill, - your code is almost exactly what I have I've come up with after reading and experimenting, however, and the reason I logged back on here, I can only get it to trigger at state level 4, anything below that and nothing happens.

    Actually, just tried your code sample and have the same problem.

    Maybe I'm a idiot and loading the script wrong? (late?) I'm calling it from my DMprecache.scr:

    Code:
    level waittill prespawn
    exec global/state_check.scr
    
    level waittill spawn
    I've tried both prespawn and spawn.


    Anyway, 1337Smithy, I suspect you're right on the caution against looping state checking. I've already noticed some 'performance' issues in the way it behaves.

    When you say "When a player connects.... local.player.new = 1" do you mean using something like:
    Code:
    connected local.player:
    local.player.new = 1
    end
    ...for setting the new variable or our we talking about a different connect event?

    I'm mostly just trying to get my head back around things since it's been a decade, but ideally my goal is to script more granular player control. I'm not sure how much abuse the engine can take however and if I'm better off just keeping control outside of the server (right now - monitoring via rcon to make decisions, trigger events, etc...). I like keeping things tight and centralized though. But yeah, at the moment just trying to get my bearings....

    -BA

  9. #9

    Default

    Yep, that's what I meant . If you're using Reborn events then use the connected event.

  10. #10
    Purple Developer Purple Elephant1au's Avatar
    Join Date
    Feb 2012
    Location
    Australia
    Posts
    1,269

    Default

    On that note..
    The connected event is triggered on map change aswell ( similar to how the "PlayerX has connected to server" message appears on map change ), so bare in mind that it will trigger for players who have already 'connected' to the game and have already been playing.
    A simple way to get around this is within the connected event, check if the player has joined a team, local.player.dmteam != "spectator" , that way if they do have a team, it means they were playing on the previous map and therefore not a 'new' player to the server. Downside is ofc that it will trigger for spectators on map change, but better then all players.

    Purple's Playground
    OBJ :
    103.29.85.127:12203
    xfire: purpleelephant1au
    email: purpleelephant1au@gmail.com
    skydrive: PurpleElephantSkydrive




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •