Page 1 of 1

Execute code when a player joins - via ServerActor only?

Posted: Sun Jan 06, 2019 5:11 am
by Dizzy
What's the most elegant way to execute some code when a new player joins the server, using only a ServerActor and no client-side stuff such as a custom GameInfo class?

The only way I'm aware of was provided to me by OwYeaW (below) but I guess there are other ways, which might not rely on the Tick function?

Code: Select all

var int CurrentID;

function tick(float DeltaTime)
{
    CheckForNewPlayer();
    Super.tick(DeltaTime);
}

function CheckForNewPlayer()
{
    local Pawn P;

    if(Level.Game.CurrentID > CurrentID)
    {
        for(P = Level.PawnList; P != None; P = P.NextPawn)
            if(P.PlayerReplicationInfo.PlayerID == CurrentID)
                break;
        CurrentID++;

        if(PlayerPawn(P) != None && P.bIsPlayer)
        {
            //P.EXECUTE STUFF HERE
        }
    }
}

Re: Execute code when a player joins - via ServerActor only?

Posted: Sun Jan 06, 2019 8:06 am
by JackGriffin
It depends on what you want to do. As long as it all remains server side I'd just use a mutator that monitors the NumPlayers value for changes. If you really wanted to get elegant you could hook ModifyPlayer to let the server know someone just joined/respawned and to check the NumPlayers again for a change. There's a lot of ways to avoid having another timer always running.

Re: Execute code when a player joins - via ServerActor only?

Posted: Sun Jan 06, 2019 10:36 am
by Barbie
Polling as seen in your code snippet is in most cases the worst solution. I'd suggest using a Mutator's ModifyLogin() function. Depending on what the code does the client may not need the mutator.

Re: Execute code when a player joins - via ServerActor only?

Posted: Sun Jan 06, 2019 5:44 pm
by Dizzy
Barbie wrote:Polling as seen in your code snippet is in most cases the worst solution. I'd suggest using a Mutator's ModifyLogin() function. Depending on what the code does the client may not need the mutator.
I want the code to display a message to the client as soon as they connect/log in.

I suspect this could all be done (detecting logins AND displaying a message) via the functionality Nexgen exposes, but honestly I haven't got a clue how to start hooking in to other packages via my own code, especially trying to keep everything server-side.

Re: Execute code when a player joins - via ServerActor only?

Posted: Sun Jan 06, 2019 10:25 pm
by Barbie
Dizzy wrote:I suspect this could all be done (detecting logins AND displaying a message) via the functionality Nexgen exposes, but honestly I haven't got a clue how to start hooking in to other packages via my own code
I don't know neither, but what I have seen in the code you have to subclass Nexgen's class NexgenPlugin.

Concerning your message box you may inspect the code of NexgenWarn112 what can be found at http://ut-files.com/index.php?dir=Nexgen/ for example.

Re: Execute code when a player joins - via ServerActor only?

Posted: Sun Jan 06, 2019 10:58 pm
by JackGriffin
Server logo. By far the easiest way to do what you want.

Re: Execute code when a player joins - via ServerActor only?

Posted: Fri Nov 15, 2019 5:41 am
by Higor
Necroing this because code has bugs.
This assumes the rule that newer players ALWAYS have a higher PlayerID than previous one.

Code: Select all

var int LastPlayerID;

function DetectPlayers() //Done to detect any pawn holding a PlayerReplicationInfo
{
    local Pawn P;

    if ( Level.Game.CurrentID > LastPlayerID ) //Quick reject, no cost.
    {
        for ( P=Level.PawnList; P!=None; P=P.nextPawn )
            if ( P.PlayerReplicationInfo != None )
            {
                if ( P.PlayerReplicationInfo.PlayerID <= LastPlayerID )
                    break;
                else
                {
                    //INITIALIZE THIS PLAYER
                }
            }
        LastPlayerID = Level.Game.CurrentID;
    }
}