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.

How to make a bot target a player via script?

Here you can find answered ScriptAPI topics.
Forum rules
By using the forum you agree to the following rules.
Post Reply
NearHuscarl
Superfighter
Superfighter
Posts: 97
Joined: Thu Feb 07, 2019 4:36 am

How to make a bot target a player via script?

Post by NearHuscarl » Wed Aug 07, 2019 10:21 pm

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?
Last edited by NearHuscarl on Sun Mar 01, 2020 9:44 am, edited 1 time in total.
0 x
Image

User avatar
JakSparro98
Superfighter
Superfighter
Posts: 530
Joined: Fri Jul 15, 2016 7:56 pm
Started SFD: PreAlpha 1.0.5
Location: Rome, Italy
Gender:
Age: 26

Post by JakSparro98 » Thu Aug 08, 2019 2:44 am

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
0 x

User avatar
Gurt
Lead Programmer
Lead Programmer
Posts: 1884
Joined: Sun Feb 28, 2016 3:22 pm
Title: Lead programmer
Started SFD: Made it!
Location: Sweden
Gender:
Age: 34

Post by Gurt » Sat Aug 10, 2019 10:29 am

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.
0 x
Gurt

Post Reply