Page 1 of 1

(Script Problem) Working with multiple players at once

Posted: Mon Mar 15, 2021 6:57 am
by Diamond TH
So, I was making a script that allows any player with a katana kill a player instantly, in a cool way, like in a movie. The problem that I have is that my script doesn't want to work with multiple kills in a short time, because if I hit another player while other haven't died yet, the script switches immediately to another player and the first player I hit doesn't die. Is there a way it could be fixed? I want it to be able to play my death animation for multiple players at the same time.

Code: Select all

        public void OnStartup()
        {
            Events.PlayerMeleeActionCallback.Start(OnPlayerMeleeAction);
        }

        public IObjectTimerTrigger CreateTimer(int interval, int count, string method, string id)
        {
            IObjectTimerTrigger trig = (IObjectTimerTrigger)Game.CreateObject("TimerTrigger");
            trig.SetIntervalTime(interval);
            trig.SetRepeatCount(count);
            trig.SetScriptMethod(method);
            trig.CustomId = id;
            trig.Trigger();
            return trig;
        }

        IPlayer hitPlayer;

        public void OnPlayerMeleeAction(IPlayer killerPlayer, PlayerMeleeHitArg[] args)
        {
                foreach (PlayerMeleeHitArg arg in args)
                {
                    IObject hitObject = arg.HitObject;
                    hitPlayer = hitObject as IPlayer;
                }

                if (hitPlayer != null)
                {
                    if (killerPlayer.IsMeleeAttacking || killerPlayer.IsJumpAttacking &&
                        killerPlayer.CurrentWeaponDrawn == WeaponItemType.Melee &&
                        killerPlayer.CurrentMeleeWeapon.WeaponItem == WeaponItem.KATANA)
                    {
                        for (int i = 0; i <= 6; i++)
                            Game.PlayEffect("BLD", new Vector2(hitPlayer.GetWorldPosition().X - 10,
                                                               hitPlayer.GetWorldPosition().Y + (10 - i)));

                        killerPlayer.SetWorldPosition(new Vector2(hitPlayer.GetWorldPosition().X +
                                                                 (hitPlayer.FacingDirection == -1 ? 17 : -17),
                                                                  killerPlayer.GetWorldPosition().Y));

                        for (int i = 0; i <= 6; i++)
                            Game.PlayEffect("BLD", new Vector2(hitPlayer.GetWorldPosition().X + 10,
                                                               hitPlayer.GetWorldPosition().Y + 10 - i));

                        hitPlayer.SetHealth(hitPlayer.GetMaxHealth());
                        CreateTimer(1500, 1, "PlayAnimation", hitPlayer.Name);
                    }
            }
        }

        public void PlayAnimation(TriggerArgs args)
        {
            hitPlayer.SetHealth(1);
            for (int i = 0; i <= 2; i++)
                Game.PlayEffect("BLD", new Vector2(hitPlayer.GetWorldPosition().X,
                                                   hitPlayer.GetWorldPosition().Y + 8));
            CreateTimer(2000, 1, "KillPlayer", hitPlayer.Name);
        }

        public void KillPlayer(TriggerArgs args)
        {
            hitPlayer.Gib();
            hitPlayer = null;
        }

Re: (Script Problem) Working with multiple players at once

Posted: Mon Mar 15, 2021 9:22 am
by Odex64
The script doesn't work because you have a global variable for the killed players. You can fix it by passing the player's uniqueId in the timer's customId, and get the current player in PlayAnimation() and KillPlayer() with Game.GetPlayer(int.Parse((IObject)args.Caller).CustomId);

If you still encounter difficulties I'll post the fixed code later.

Re: (Script Problem) Working with multiple players at once

Posted: Mon Mar 15, 2021 4:57 pm
by Diamond TH
Odex64 wrote:
Mon Mar 15, 2021 9:22 am
The script doesn't work because you have a global variable for the killed players. You can fix it by passing the player's uniqueId in the timer's customId, and get the current player in PlayAnimation() and KillPlayer() with Game.GetPlayer(int.Parse((IObject)args.Caller).CustomId);

If you still encounter difficulties I'll post the fixed code later.
I'm passing the player's uniqueId in the timer's customId like this (hitPlayer isn't a global variable anymore):

Code: Select all

CreateTimer(1500, 1, "PlayAnimation", hitPlayer.UniqueID.ToString());
But when I'm trying to get the current player in the next method:

Code: Select all

IPlayer hitPlayer = Game.GetPlayer(int.Parse((IObject)args.Caller).CustomId);
I get "Argument 1: cannot convert from 'SFDGameScriptInterface.IObject' to 'string'" error. Am I doing something wrong :? ?

Re: (Script Problem) Working with multiple players at once

Posted: Mon Mar 15, 2021 5:05 pm
by Odex64
Ah, I forgot to add another parhentesis, it should be

Code: Select all

 
IPlayer hitPlayer = Game.GetPlayer(int.Parse(((IObject)args.Caller).CustomId));

Re: (Script Problem) Working with multiple players at once

Posted: Mon Mar 15, 2021 5:12 pm
by Diamond TH
Odex64 wrote:
Mon Mar 15, 2021 5:05 pm
Ah, I forgot to add another parhentesis, it should be

Code: Select all

 
IPlayer hitPlayer = Game.GetPlayer(int.Parse(((IObject)args.Caller).CustomId));
It's working perfectly now! Thank you very much!