# How do I Set Up Proper Artificial Intelligence?

Get help using Construct 2

### » Sat Apr 05, 2014 2:00 pm

What I want to happen is for the enemy to chase the player.

I also need him to randomly set an attack. Which I planned on doing by set wep to random(1,3) Then if wep = blah blah use attack.

I was hopping for some help on how to do this because my distance formula doesn't seem to work as he will either infinity use his weapon (if set to greater than) or never use it at all (if set to less than).

I also need him to do things on the Y-axis by jumping and such but I figured I could figure that out later.

Any and all help is appreciated,
thanks
You do not have the required permissions to view the files attached to this post.
B
8
S
1
Posts: 76
Reputation: 949

### » Sat Apr 05, 2014 2:27 pm

The problem is that random(1,3) is almost always greater than 1 and never smaller than 1.

Random returns a float between either 0 and a-1, if you use
Code: Select all
`random(a)`
or a and b if
Code: Select all
`random(a,b)`
This means that it can return 1.024566 or 2.478962. You could set wep to
Code: Select all
`floor(random(3)+1)`
and comparing if it is 1. That way it has an equal chance to return 1.564585, 2.024566 or 3.478962, which will round down to the nearest integer (1, 2 and 3, respectively). I recommend
Code: Select all
`choose(1,0,0)`
which will randomly choose between those three, so 1 has a 1/3 chance, and the two 0's have a total chance of 2/3.

Distance can be replaced by
Code: Select all
`abs(player.x-testenemy.x)`
Borka223 from RecGames
B
14
S
4
G
5
Posts: 39
Reputation: 2,987

### » Sat Apr 05, 2014 2:49 pm

Thank you very much on both aspects. I knew there was something I had to add to the random part and I just couldn't remember.
B
8
S
1
Posts: 76
Reputation: 949

### » Sat Apr 05, 2014 3:22 pm

izicial wrote:Thank you very much on both aspects. I knew there was something I had to add to the random part and I just couldn't remember.

It is very important to remember what kind of output you get and convert appropriately. I hope you know what int(), string() and float() do and how to use them. I've failed many times because I didn't know random returns a float. Good luck in your project!
Borka223 from RecGames
B
14
S
4
G
5
Posts: 39
Reputation: 2,987