### [Solved] Need help in making bot AI

#### 1MrPaul1

• Posts: 1285
I'm trying to make a bot that will shoot to enemies, and have no idea how to set up direction for bullets... Any advices will help...

« Last Edit: September 16, 2011, 10:31:38 pm by 1MrPaul1 »

#### Alexin

• Posts: 3130
Download a game from Forge that does what you want (Super Noni's Adventure?) and investigate how enemies shoot at the player.
"Find the fun"
alexin@stencyl.com

#### 1MrPaul1

• Posts: 1285
I'm using mobile internet, so working offline and can't download this game right now for checking it, tell me please, in those game enemies shoot at player x and y like in "run and gun" or set player as target like in "follow actor of type" behavior? I already understand how make enemy shoot the player, it's easy, but don't know how to make AI that will pick one or two enemies that placed most closer to player, from group of enemies on screen, some times it's 20 monsters in one time. I think I need to use block "for each actor of group" but I don't know how proper use this block

#### Alexin

• Posts: 3130
[...] but don't know how to make AI that will pick one or two enemies that placed most closer to player, from group of enemies on screen, some times it's 20 monsters in one time.
1) Create a Number attribute named Distance. Right before the loop, set it to a big number.

2) Create an Actor attribute named ClosestEnemy.

3) Use a loop block to iterate over a group or type of actors (the enemies).

4) For each enemy, compute its distance to the player. If this distance is lesser than Distance, then set ClosestEnemy to the enemy.

5) When the loop is over, ClosestEnemy contains the closest enemy to the player. Use it to shoot.

« Last Edit: September 13, 2011, 07:17:03 am by Alexin »
"Find the fun"
alexin@stencyl.com

#### 1MrPaul1

• Posts: 1285
Some moments still hard for me, but it looks how very good advise, thank you very much Alexin!

#### AnachroNia

• Posts: 72
[...] but don't know how to make AI that will pick one or two enemies that placed most closer to player, from group of enemies on screen, some times it's 20 monsters in one time.
1) Create a Number attribute named Distance. Right before the loop, set it to a big number.

2) Create an Actor attribute named ClosestEnemy.

3) Use a loop block to iterate over a group or type of actors (the enemies).

4) For each enemy, compute its distance to the player. If this distance is lesser than Distance, then set ClosestEnemy to the enemy.

5) When the loop is over, ClosestEnemy contains the closest enemy to the player. Use it to shoot.

Don't set it to 'a big number'... set it to the distance to the first Enemy in the group and set it as the closest enemy. What will happen if this 'big number' is smaller than the distance to all the enemies?

#### Alexin

• Posts: 3130
I was explaining in layman's term. The outcome is the same but it requires one more block.

You can either use the suggestion above or Number.MAX_VALUE.
"Find the fun"
alexin@stencyl.com

#### 1MrPaul1

• Posts: 1285
Alexin, I tried to undertand yours solution of this problem, but undertand just that I undertand nothing... of cause I know how to create attributes and where to find loop block, but it seems that my knowledge is still too poor for other details that you advice me... also, tried to create region around the bot and make as target fists enemy in this region, but everything is unsuccessfully... it's always easy for me to use default values of actors in blocks, as "self","last collided actor","last created actor" but I don't know how to assign actor with needed values to the "actor attribute" for next working with it, so always problem in this. I learn many things in stencyl by examples from other behaviors, but can't find behaviors with those things that you adviced me

#### Alexin

• Posts: 3130
Closest Enemy - Hidden Actor atttribute
Shortest Distance - Hiddent Number att
Distance - Hidden Number att
DX - Hidden Number att
DY - Hidden Number att
Player - Visible actor attribute you can chose from the scene
"Find the fun"
alexin@stencyl.com

#### 1MrPaul1

• Posts: 1285
Big thanx to you Alexin!!! Today finished this behavior, problem was in picking target for shooting, I combined your behavior and behavior "enemy fire projectile" from run and gun. And I don't know how, but it's working again thank you!
Here is the ready behavior, if some body need it. It can be attached to friendly bot that will help to shoot an enemies.

#### Alexin

• Posts: 3130
Moving to Resolved Questions.
"Find the fun"
alexin@stencyl.com

#### JKTKops

• Posts: 31
I'm trying to do the same thing for a Danmaku game I'm working on for school. I know this topic is super old, and that's actually kind of the problem. When I go to iterate over all the enemies, that "the actor" block no longer exists and I can't seem to find a replacement for it. What do I do to get this to work? Thanks in advance.

~JKTKops

#### letmethink

• Posts: 2545
It is instead 'actor of type' and you can drag it from the for each actor of type block ( it is next to it)
~Letmethink