Page 1 of 1

How to make a bot target a player via script?

Posted: Wed Aug 07, 2019 10:21 pm
by NearHuscarl
EDIT: Here is a pretty good workaround for me

To make a bot target a different bot or any IObject

Code: Select all

IObject targetObj = ...
IPlayer player = ...

player.SetGuardTarget(targetObj);
var bs = Player.GetBotBehaviorSet();
bs.GuardRange = 1f;
bs.ChaseRange = 1f;
player.SetBotBehaviorSet(bs);
-------

I need to find a way to make my bot hunt down a specific target, in my case only melee is sufficient. This code is the closest I can get for now

Code: Select all

using SFDGameScriptInterface;

namespace SFDScript.Test
{
    public class TargetChasing : GameScriptInterface
    {
        /// <summary>
        /// Placeholder constructor that's not to be included in the ScriptWindow!
        /// </summary>
        public TargetChasing() : base(null) { }

        public void OnStartup()
        {
            var pos = Game.GetPlayers()[0].GetWorldPosition();
            pos.X -= 50;

            var bot = Game.CreatePlayer(pos);
            bot.SetBotBehaviorSet(BotBehaviorSet.GetBotBehaviorPredefinedSet(PredefinedAIType.BotA));
            bot.SetBotBehaviorActive(true);

            Events.UpdateCallback.Start(OnUpdate);
        }

        private bool m_attacking = false;
        private void OnUpdate(float obj)
        {
            var target = Game.GetPlayers()[0];
            var bot = Game.GetPlayers()[1];
            var attackRadius = 75;

            Game.DrawCircle(target.GetWorldPosition(), attackRadius, Color.Red);

            if (Vector2.Distance(bot.GetWorldPosition(), target.GetWorldPosition()) < attackRadius)
            {
                if (!m_attacking && Game.IsEditorTest)
                    Game.CreateDialogue("Attack", bot);
                bot.SetGuardTarget(null);
                m_attacking = true;
            }
            else
            {
                if (m_attacking && Game.IsEditorTest)
                    Game.CreateDialogue("Move", bot);
                bot.SetGuardTarget(target);
                m_attacking = false;
            }
        }
    }
}

But it will not quite chase the the target when it is in a crowd. Wonder if there are any better ways?

Re: How to make a bot target a player via script?

Posted: Thu Aug 08, 2019 2:44 am
by JakSparro98
Only by setting the bot and the player in different teams and the crowd in the same team of the bot will prevent him from attacking and chasing other entities

Re: How to make a bot target a player via script?

Posted: Sat Aug 10, 2019 10:29 am
by Gurt
There is a IPlayer.SetValidBotEliminateTarget(bool) function you can use to disable a specific player to be targeted by bots too.
But as JakSparro98 said, you will have to have the bots in different teams. I never got around adding support to set a specific target through the ScriptAPI as we never needed it ourselves.