Top Down games are almost nothing without great AI. In this article I will cover the steps to making great Top Down AI for your game! All of this comes from my upcoming game Covert Ops.
Step 1. scr_ai_init
We'll want to start our AI coding with all the variables we will need. Create a script called "scr_init_ai". You can call it whatever you want, but we're going to call it that in this article. In this script, we want to initialize some key variables such as health, ammo, and whatever action the AI is doing. Let's add the following:
ai_health=100;//Set the health to 100
ai_ammo=50;//Set the ammo to 50
action=0;//Set the action to 0
//Action Variables
//0 = Wandering
//1 = Finding the target
//2 = Shooting the target
//3 = Reloading
//4 = Finding health
Now we have our main variables set up! We have our health, ammo, and a very important action variable. The action variable is what will make the AI do stuff.
Step 2. scr_ai_step
This is the main code we will use as it happens every step. Here is where we will check our actions and do them accordingly. Let's call this code scr_ai_step and add the following:
image_angle=direction//For showing what direction the AI is going
if action=0{
scr_wander()
}
You'll notice that there are other scripts in this code. We will get to that. For now, notice how this coding is what controls the AI's every move. Whatever it does it does because of this. This makes it very easy to add more actions.
Step 2A. scr_wander
In step 2, we saw that a script scr_wander() was called. Here is where we will make it. It's not very hard to do. Basically what it does is make the AI go to certain points just like it's called, scr_wander. Before we can make it, however, we will need a few more variables called set, point1, point2, point3, and point4. So lets open up scr_ai_init again and add the following:
set=0;
point1=instance_nearest(x,y,objPoint1);
point2=instance_nearest(x,y,objPoint2);
point3=instance_nearest(x,y,objPoint3);
point4=instance_nearest(x,y,objPoint4);
Now that we have that, we can make scr_wander:
if set=0{
mp_potential_step_object(point1.x,point1.y,1.5,objAvoid)
}
if set=1{
mp_potential_step_object(point2.x,point2.y,1.5,objAvoid)
}
if set=2{
mp_potential_step_object(point3.x,point3.y,1.5,objAvoid)
}
if set=3{
mp_potential_step_object(point4.x,point4.y,1.5,objAvoid)
}
if x=point1.x and y=point1.y and set=0{
set=1
}
if x=point2.x and y=point2.y and set=1{
set=2
}
if x=point3.x and y=point3.y and set=2{
set=3
}
if x=point4.x and y=point4.y and set=3{
set=0
}
What will this script do? It will move to each of the four points accordingly in a continuous loop.
You'll notice that we have 5 objects that we have not yet created. Let's create them now. For reference, they are:
objPoint1
objPoint2
objPoint3
objPoint4
objAvoid
objAvoid is a major object in this AI, so make sure nothing bad happens to it!
Due to space issues, this concludes Part 1 of my Top Down AI article. Click here for Part 2!
.
Users logged in:
Comments
Loading comments...