Execute code when a player joins - via ServerActor only?

Discussions about Coding and Scripting
Post Reply
User avatar
Dizzy
Experienced
Posts: 109
Joined: Tue May 21, 2013 3:57 pm
Personal rank: Oaf
Contact:

Execute code when a player joins - via ServerActor only?

Post 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
        }
    }
}
Join the BunnyTrack.net Discord chat server: https://www.bunnytrack.net/discord
JackGriffin
Godlike
Posts: 3774
Joined: Fri Jan 14, 2011 1:53 pm
Personal rank: -Retired-

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

Post 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.
So long, and thanks for all the fish
User avatar
Barbie
Godlike
Posts: 2792
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

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

Post 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.
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett
User avatar
Dizzy
Experienced
Posts: 109
Joined: Tue May 21, 2013 3:57 pm
Personal rank: Oaf
Contact:

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

Post 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.
Join the BunnyTrack.net Discord chat server: https://www.bunnytrack.net/discord
User avatar
Barbie
Godlike
Posts: 2792
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

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

Post 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.
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett
JackGriffin
Godlike
Posts: 3774
Joined: Fri Jan 14, 2011 1:53 pm
Personal rank: -Retired-

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

Post by JackGriffin »

Server logo. By far the easiest way to do what you want.
So long, and thanks for all the fish
Higor
Godlike
Posts: 1866
Joined: Sun Mar 04, 2012 6:47 pm

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

Post 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;
    }
}
Post Reply