• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Where to begin with AI

boi513

Member
I need to begin programming artificial intelligence for my project. It is a fighting game in the style of streets of rage. Can anyone give me clues on how to begin as far as coding is concerned?
Will switch statements be most effective?
What are some things to know as far as cpu positioning is concerned?
advice?
 

samspade

Member
I need to begin programming artificial intelligence for my project. It is a fighting game in the style of streets of rage. Can anyone give me clues on how to begin as far as coding is concerned?
Will switch statements be most effective?
What are some things to know as far as cpu positioning is concerned?
advice?
Start with learning about state machines if you aren't familiar with them. You can implement the state machines in a variety of different ways (switch statements, functions, methods, etc.) but the core idea carries across all of them.

If you're willing to spend money, there's this asset on the marketplace: https://marketplace.yoyogames.com/assets/7720/beat-em-up-engine.

I have used it myself but it seems to be inline with what you want.

I don't know what cpu position means.
 

boi513

Member
By "positioning" I meant as far as what distance the cpu chooses to use an ability, like ryu's hadouken from "street fighter".
 

angelwire

Member
My best piece of advice is to write the AI object with similar "inputs" like you would with the player object. When I first started writing AI my tendency was to take shortcuts and set values directly instead of using ai "inputs".
For example, if the AI character is standing in a fire and needs to run to the left to get out of the fire, instead of writing:
GML:
while(is_in_fire)
{
x += 2;
}
You should write:
GML:
if (is_in_fire)
{
x_go_to = //x position where there is no fire
}

if (x < x_go_to)
{
move_right();
}

if (x > x_go_to)
{
move_left();
}
And then move_left() and move_right() should have the same code you've written in the player's keyboard left/keyboard right events. This way you can handle collisions and the AI won't accidentally cheat by going through walls. And you can also do the same thing with attacks. Copy the attack code that you've written for when the player presses the "attack" button and use it in a script that the AI should call. This will make sure the AI is playing by the same rules and won't spam a fireball every step or something like that. The closer your AI object is to your player object the better things will be. Your AI won't accidentally cheat if it can only do actions based off of the same inputs that the player can do.

Now this doesn't tell you how to determine what the AI should do at each step. Like @samspade said, Finite State machines will be your friend. You create a bunch of states that tell the AI what it's currently doing, and then switch between them when things happen. You can have an "Attack Close" state where the AI tries to move close to the player and use a punch/kick or a "Attack Far" state where the ai tries to get some distance from the player and use a fireball or throw something. Here's an example for the AI trying to not get hit by the player:
GML:
//If the AI is just sitting around
    //If the player is kicking
        //If I'm close enough to the player to get kicked
        //See if I can get out of the way before getting hit
            //If I can, move in the direction to get out of the way
        //If not, see if I can block
            //If I can block, then block and enter the "block" state
Another quick tip, add some randomness to keep the AI from being perfect. If the AI reacts instantly as soon as the player kicks, then it will be nearly impossible to win.

Hopefully this is helpful. AI is very tough, but it's also incredibly rewarding. Good luck!
 

Joe Ellis

Member
States are the best way to build ai. Otherwise you're just adding if, else, plus many other ifs and elses to determine what "state" the instance is in. States are basically a cleaner way to handle this, cus instead of having a switch statement or the ifs, you split up whats inside each if into different functions, then just tell the instance to run the current state function.
In the older versions you use script_execute(state). If you're using 2.3, you assign the function to the variable "state" and simply do: state().

Performance related: Assigning a function to a variable and then calling it using the variable is incredibly slow, about 4 times slower than script_execute. Cus it has to bind a temporary method for that once call. So this is why you should create a method, which basically binds that function to the instance for it to use. ei. create a method variable. Then it ends up almost as fast as calling the function directly.
This doesn't matter too much while you're just setting stuff up though. I'd just assign the function itself to the state variable for now. But if performance becomes an issue if you've got a lot of ai instances, it'll have a big impact, so def something to bare in mind before releasing the finished game.
 
Top