array (when player leave game)

You can talk about ManiaScript for ManiaPlanet here

Moderator: English Moderator

User avatar
steeffeen
Translator
Translator
Posts: 2463
Joined: 14 Oct 2012, 16:22
Location: Germany

Re: array (when player leave game)

Post by steeffeen »

just remove the id when the player doesn't exist anymore

(you shouldn't alter the array while looping through it, save the ids of players who left and remove them later)

Code: Select all

declare LeftPlayerIds = Ident[];
foreach (PlayerId in MyPlayerIds) {
	if (!Players.existskey(PlayerId)) {
		// Player left
		LeftPlayerIds.add(PlayerId);
	} else {
		// Player still exist
		declare Player <=> Players[PlayerId];
		// Do stuff
	}
}
// Remove players who left
foreach (PlayerId in LeftPlayerIds) {
	declare Removed = MyPlayerIds.remove(PlayerId);
}
    Game Mode and Title Pack Creator, Developer, ShootMania-Player & more

    ManiaControl, FancyManiaLinks
    User avatar
    alividerci
    Posts: 363
    Joined: 11 Feb 2012, 07:03

    Re: array (when player leave game)

    Post by alividerci »

    steeffeen wrote:just remove the id when the player doesn't exist anymore

    (you shouldn't alter the array while looping through it, save the ids of players who left and remove them later)

    Code: Select all

    declare LeftPlayerIds = Ident[];
    foreach (PlayerId in MyPlayerIds) {
    	if (!Players.existskey(PlayerId)) {
    		// Player left
    		LeftPlayerIds.add(PlayerId);
    	} else {
    		// Player still exist
    		declare Player <=> Players[PlayerId];
    		// Do stuff
    	}
    }
    // Remove players who left
    foreach (PlayerId in LeftPlayerIds) {
    	declare Removed = MyPlayerIds.remove(PlayerId);
    }
    and what me do with

    Code: Select all

    declare CSmPlayer[] WhoPickupedArtefact;
    declare CSmPlayer ArtefactPlayerAllias;	
    User avatar
    steeffeen
    Translator
    Translator
    Posts: 2463
    Joined: 14 Oct 2012, 16:22
    Location: Germany

    Re: array (when player leave game)

    Post by steeffeen »

    steeffeen wrote: save the player ids in

    Code: Select all

    declare Ident[] MyPlayerIds;
      Game Mode and Title Pack Creator, Developer, ShootMania-Player & more

      ManiaControl, FancyManiaLinks
      Usul
      Posts: 17
      Joined: 23 Jul 2013, 15:22
      Location: Belgique
      Contact:

      Re: array (when player leave game)

      Post by Usul »

      alividerci wrote: I want the player who picked up the artifact was removed it from an array when disconnecting...
      i can do it without id?
      No you can't do it, without id. (perhaps with magic but not with code :D )

      I had the same problem in the past and I resolved it with some functions

      Code: Select all

      CSmPlayer GetPlayerByID(Ident _PlayerId)
      {
      	if (_PlayerId==NullId) return Null;
      	if (Players.existskey(_PlayerId))
      		return Players[_PlayerId];	
      	return Null;
      }
      // ---------------------------------- //
      Ident GetIDByPlayer(CSmPlayer _Player)
      {
      	if (_Player==Null) return NullId;
      	if (Players.existskey(_Player.Id))
      		return _Player.Id;
      	return NullId;
      }
      // ---------------------------------- //
      Boolean IsValidPlayerId(Ident _PlayerId)
      {
      	declare CSmPlayer pl = GetPlayerByID(_PlayerId);
      	if (pl==Null) return False;
      	return (pl.SpawnStatus != CSmPlayer::ESpawnStatus::NotSpawned && !pl.RequestsSpectate && pl.Armor>0); 
      }
      // ---------------------------------- //
      Boolean IsValidCSmPlayer(CSmPlayer _Player)
      {
      	if (_Player==Null) return False;
      	return IsValidPlayerId(_Player.Id);	
      }
      
      User avatar
      alividerci
      Posts: 363
      Joined: 11 Feb 2012, 07:03

      Re: array (when player leave game)

      Post by alividerci »

      ok, what about if i want to made something
      example

      Code: Select all

      ArtefactPlayerAllias.AmmoPower = 4.;
      how me do it with id...i think it is not possible or possible :?
      Usul
      Posts: 17
      Joined: 23 Jul 2013, 15:22
      Location: Belgique
      Contact:

      Re: array (when player leave game)

      Post by Usul »

      With my functions like this

      Code: Select all

      foreach( Player in YourArtefactPlayerList){
                  If (IsValidCSmPlayer(Player)){
                        Player.AmmoPower = 4.;
                  }else{
                         //Player is disconnected or dead or spectator
                         Ident pID = GetIDByPlayer(Player);
                         if (pID == NullId) 
                         //Player is disconnected
                         else
                         //Player is dead (or spectator)       
      	         }
      }
      
      User avatar
      alividerci
      Posts: 363
      Joined: 11 Feb 2012, 07:03

      Re: array (when player leave game)

      Post by alividerci »

      Usul wrote:With my functions like this

      Code: Select all

      foreach( Player in YourArtefactPlayerList){
                  If (IsValidCSmPlayer(Player)){
                        Player.AmmoPower = 4.;
                  }else{
                         //Player is disconnected or dead or spectator
                         Ident pID = GetIDByPlayer(Player);
                         if (pID == NullId) 
                         //Player is disconnected
                         else
                         //Player is dead (or spectator)       
      	         }
      }
      
      IsValidCSmPlayer - fake :evil:
      User avatar
      steeffeen
      Translator
      Translator
      Posts: 2463
      Joined: 14 Oct 2012, 16:22
      Location: Germany

      Re: array (when player leave game)

      Post by steeffeen »

      use ids!...
      alividerci wrote:i can do it without id?
      nope
        Game Mode and Title Pack Creator, Developer, ShootMania-Player & more

        ManiaControl, FancyManiaLinks
        User avatar
        alividerci
        Posts: 363
        Joined: 11 Feb 2012, 07:03

        Re: array (when player leave game)

        Post by alividerci »

        steeffeen wrote:use ids!...
        alividerci wrote:i can do it without id?
        nope
        I try to do with ID
        In any case, I can ask you, where my mistake :D
        Usul
        Posts: 17
        Joined: 23 Jul 2013, 15:22
        Location: Belgique
        Contact:

        Re: array (when player leave game)

        Post by Usul »

        alividerci wrote:IsValidCSmPlayer - fake :evil:
        look my previous post I give you the function IsValidCSmPlayer ...
        if you don't read the answers to your question, I can not help you.
        Post Reply

        Return to “ManiaScript”

        Who is online

        Users browsing this forum: No registered users and 1 guest