Do something after player enter/leave the server

Discussions about Coding and Scripting
iloveut99
Skilled
Posts: 231
Joined: Mon Aug 16, 2010 10:25 pm

Do something after player enter/leave the server

Post by iloveut99 »

class code extends LastManStanding;

event PostLogin( playerpawn NewPlayer ){
super.PostLogin(NewPlayer);
log("Player Enterered");
}

function Logout( pawn Exiting ){
super.Logout(Exiting);
log("Player Left");
}
I'm using this code in a lms game but it doesn't logs what I write, seems the event/function don't get called. I'm spawning this class from a mutator.

Anyone got an idea why it isn't working?
User avatar
Feralidragon
Godlike
Posts: 5502
Joined: Wed Feb 27, 2008 6:24 pm
Personal rank: Work In Progress
Location: Liandri

Re: Do something after player enter/leave the server

Post by Feralidragon »

Afaik the LastManStanding is a gameype, only works if it's the current gametype running, and the same applies to its subclasses.
Therefore you can't just spawn it to use it's events.

In the context of a mutator you have this:

Code: Select all

function ModifyLogin(out class<playerpawn> SpawnClass, out string Portal, out string Options)
{
    if ( NextMutator != None )
        NextMutator.ModifyLogin(SpawnClass, Portal, Options);
}
This is called from every gametype's "Login(...)" function, so you can use that instead of PostLogin I think.

Regarding the logout, well, I am not sure what you can do about this one, there are some ideas I have in mind, but only someone who already had to use such events should enlighten you better and lead you the proper way.

But basically, on logout, the player ceases to exist, perhaps you should go from there, idk.
User avatar
>@tack!<
Adept
Posts: 338
Joined: Sat Apr 17, 2010 4:51 pm
Personal rank: lol?

Re: Do something after player enter/leave the server

Post by >@tack!< »

is there a way to access the playerpawn with the ModifyLogin function?
iloveut99
Skilled
Posts: 231
Joined: Mon Aug 16, 2010 10:25 pm

Re: Do something after player enter/leave the server

Post by iloveut99 »

Feralidragon wrote:Afaik the LastManStanding is a gameype, only works if it's the current gametype running, and the same applies to its subclasses.
Therefore you can't just spawn it to use it's events.
The mutator is just for use in LMS gametype. But even so it will not work if overwrite the gametype functions?
Feralidragon wrote: In the context of a mutator you have this:

Code: Select all

function ModifyLogin(out class<playerpawn> SpawnClass, out string Portal, out string Options)
{
    if ( NextMutator != None )
        NextMutator.ModifyLogin(SpawnClass, Portal, Options);
}
I read about this function but seems it also is called after a player respawn. Is this correct?

Thanks for all the help.
User avatar
Sp0ngeb0b
Adept
Posts: 376
Joined: Wed Feb 13, 2008 9:16 pm
Location: Cologne

Re: Do something after player enter/leave the server

Post by Sp0ngeb0b »

Yes, ModifyPlayer is called everytime on a mutator when a player respawn. If you only want to call it when the player enters the game, make sure that the players hasn't died yet.

Should look like this:

Code: Select all

function ModifyPlayer(Pawn Other) {
  if(Other.IsA('TournamentPlayer') && Other.PlayerReplicationInfo.Deaths < 1) {
    
     // Do something here
   }
   if (NextMutator != None) NextMutator.ModifyPlayer(Other);
}
Taking tracks of player leaving is a bit tricky though, I recommend creating a player-structure and then check each tick whether a player joined/left.

EDIT: Here's a good tutorial on how to maintain the player structure. Link
Website, Forum & UTStats

Image
******************************************************************************
Nexgen Server Controller || My plugins & mods on GitHub
******************************************************************************
iloveut99
Skilled
Posts: 231
Joined: Mon Aug 16, 2010 10:25 pm

Re: Do something after player enter/leave the server

Post by iloveut99 »

Sp0ngeb0b wrote:Yes, ModifyPlayer is called everytime on a mutator when a player respawn. If you only want to call it when the player enters the game, make sure that the players hasn't died yet.

Should look like this:

Code: Select all

function ModifyPlayer(Pawn Other) {
  if(Other.IsA('TournamentPlayer') && Other.PlayerReplicationInfo.Deaths < 1) {
    
     // Do something here
   }
   if (NextMutator != None) NextMutator.ModifyPlayer(Other);
}
Taking tracks of player leaving is a bit tricky though, I recommend creating a player-structure and then check each tick whether a player joined/left.

EDIT: Here's a good tutorial on how to maintain the player structure. Link
My idea was to optimizes the code at max. But if I need to use timers/ticks functions I will just forget it, and do a timer that do some checks every 5 seconds for example. It would be enough.

I though it could be simpler to check if a player left, with overriding functions. (probably I could do that if I create a new gametype extending to lms)

Thanks to all. I should release the mutator soon, and I can say now too that it will try optimize the bots in lms. (which now tend to block the server without letting players enter)
iloveut99
Skilled
Posts: 231
Joined: Mon Aug 16, 2010 10:25 pm

Re: Do something after player enter/leave the server

Post by iloveut99 »

Hi again,

What's it's the easier way to addbots to the game via uscript? Tried Engine.GameInfo.AddBot() but seems isn't recognized.
User avatar
Feralidragon
Godlike
Posts: 5502
Joined: Wed Feb 27, 2008 6:24 pm
Personal rank: Work In Progress
Location: Liandri

Re: Do something after player enter/leave the server

Post by Feralidragon »

Well, if you're basing yourself in the console commands, let me say that none of them run from GameInfo. They are all (or at least most of them) within the PlayerPawn class: all the functions started with the "exec" modfier.

If the case of PlayerPawn class, that function is this one:

Code: Select all

exec function AddBots(int N)
{
    ServerAddBots(N);
}

function ServerAddBots(int N)
{
    local int i;

    if ( !bAdmin && (Level.Netmode != NM_Standalone) )
        return;

    if ( !Level.Game.bDeathMatch )
        return;

    for ( i=0; i<N; i++ )
        Level.Game.ForceAddBot();
}
Basically, you can implement the code directly as a function in your own class like this:

Code: Select all

function AddBots(int N)
{
    local int i;

    if ( !Level.Game.bDeathMatch )
        return;

    for ( i=0; i<N; i++ )
        Level.Game.ForceAddBot();
}
It's basically the same as ServerAddBots, but without the admin checks.
iloveut99
Skilled
Posts: 231
Joined: Mon Aug 16, 2010 10:25 pm

Re: Do something after player enter/leave the server

Post by iloveut99 »

Ha awesome, thanks! :D

Abraço.