Dear forum users! In compliance with the new European GDPR regulations, we'd just like to inform you that if you have an account, your email address is stored in our database. We do not share your information with third parties, and your email address and password are encrypted for security reasons.

New to the forum? Say hello in this topic! Also make sure to read the rules.

(Script Problem) Working with multiple players at once

Share questions, scripts and tutorials related to the ScriptAPI in SFD.
Forum rules
By using the forum you agree to the following rules.
Post Reply
User avatar
Diamond TH
Fighter
Fighter
Posts: 21
Joined: Fri Sep 14, 2018 10:44 am
Title: Fairplayer
SFD Account: Diamond_TH
SFD Alias: Diamond_TH
Started SFD: May 2015
Location: Russia
Gender:
Age: 20
Contact:

(Script Problem) Working with multiple players at once

Post by Diamond TH » Mon Mar 15, 2021 6:57 am

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;
        }
0 x
Image - Noob SuperFighter saying

User avatar
Odex64
Superfighter
Superfighter
Posts: 172
Joined: Sat Jul 29, 2017 12:39 pm
Title: Content Creator
SFD Account: Odex64
Started SFD: PreAlpha
Location: Italy
Gender:
Age: 22

Post by Odex64 » 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.
0 x
Image

User avatar
Diamond TH
Fighter
Fighter
Posts: 21
Joined: Fri Sep 14, 2018 10:44 am
Title: Fairplayer
SFD Account: Diamond_TH
SFD Alias: Diamond_TH
Started SFD: May 2015
Location: Russia
Gender:
Age: 20
Contact:

Post by Diamond TH » Mon Mar 15, 2021 4:57 pm

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 :? ?
0 x
Image - Noob SuperFighter saying

User avatar
Odex64
Superfighter
Superfighter
Posts: 172
Joined: Sat Jul 29, 2017 12:39 pm
Title: Content Creator
SFD Account: Odex64
Started SFD: PreAlpha
Location: Italy
Gender:
Age: 22

Post by Odex64 » 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));
0 x
Image

User avatar
Diamond TH
Fighter
Fighter
Posts: 21
Joined: Fri Sep 14, 2018 10:44 am
Title: Fairplayer
SFD Account: Diamond_TH
SFD Alias: Diamond_TH
Started SFD: May 2015
Location: Russia
Gender:
Age: 20
Contact:

Post by Diamond TH » Mon Mar 15, 2021 5:12 pm

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!
0 x
Image - Noob SuperFighter saying

Post Reply