• 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!

How do you manage enemy AI?

  • Thread starter Andrea Dominante
  • Start date
A

Andrea Dominante

Guest
I'll make my question a little more specific: which functions do you use for specific AI behaviors?

For example, I'm about to make a standing enemy which turns and shoot towards my character, using
distance_to_object and in case collision_line...

How do you usually do this stuff? I'm curious to learn more efficient and smarter ways :)
 
A

Aura

Guest
There's no use of making things over-complicated. Usage of functions depends on what I'm going to code. For instance, if I want to code pathfinding, I'd use the Motion Planning functions. Since you have not asked about the most efficient method of implementing a certain type of AI, it would be really difficult to help. ^^"

A suggestion though: You should use Finite State Machines (FSMs) to determine the behaviour of an instance. It's the most efficient method of handling nested AI IMO. ^^
 
I've found you can do practically anything you want with these functions:
Code:
mp_potential_step();
move_towards_point();
collision_line();
distance_to_point();
distance_to_object();
Along with a few variables to keep track of speed and whether it's chasing the player or not, etc.

I agree with the others as well, check out FSMs. I've been doing coding like that for a while, but I actually didn't know there was a term for that until I read this post!
 

TheouAegis

Member
It also depends on what kind of game. What the perspective on it is. If it's a top-down game, those functions are good. If it's a side scrolling game, many of those functions are Overkill. Like, I often see people use point_distance or distance_to_object, when what they want is just abs(player.x-x). I don't use amy of the functions listed.

I do use state FSM in one way or another every time, though.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
I do use state FSM in one way or another every time, though.
Yep, same. Ai is (generally) done in two steps: decision making, and response. For the decision making part you use the FSM, and for response, well that could be anything (shoot, move, throw, etc...). So you want to decide what actions your AI needs to perform and set up an FSM to select those repsonses, and then fill in the parts with the response code. A typical (simple) FSM would simply be a "state" variable and a "switch" in the step event, for example:
Code:
// CREATE
state = "idle";

// STEP
switch (state)
{
case "idle":
    // find a target or move randomly or something
    // If there is a target set state to "attack"
    break;
case "attack":
    // If the target is in range attack, if not move.
    // if moving set state to "chase"
    break;
case "chase":
    // Check player is in range, if in range attack
    // if not in range then go idle
    break;
}
That's the idea.... you have to make sure that no matter what the situation there is a state to cover it, and that no states loop between themselves endlessly, but other than that it's actually quite easy to set up (when it's basic).
 
I often see people use point_distance or distance_to_object, when what they want is just abs(player.x-x).
Ha ha, wow. I've never even considered doing that the math way. I actually started programming AI programs like that before I even took a geometry class, and I never bothered changing any of the ways I did things after I knew enough math to do so.

So then, my follow up question is if the distance_to_point(); function is any different than the simple calculation, and if so how. Also, are either of the solutions more or less efficient than the other?
 

Yal

šŸ§ *penguin noises*
GMC Elder
I really like this approach... you might call it infinite state machine, I guess :p It simply won't get more flexible than this, in either case.

upload_2016-7-7_21-30-59.png
 
Top