SOLVED AI using a node based pattern

Iain

Member
Hi all,

I am trying to make a AI for the enemy objects using a node. The nodes is placed at the intersection within a maze. Within each node a boolean variable is set for each of the available directions the intersection has, true being the direction open. When the enemy object hits the node the enemy then checks through the 4 boolean varibles, (East, North, West, South). puts the true ones into a ds_list then a new direction is randomly chosen from the ds_list_size.
My problem is that once the enemy hits the node and sorts out a new direction is sorted and the ds_list is destroyed the enemy is still inside the collision detection of the node so the code runs again. How can I stop this from happening....
Cheers for any help
 

kraifpatrik

(edited)
GameMaker Dev.
One way to solve this could be to make a variable that remembers the id of the node that the enemy has collided with and initialize it with undefined for example. When an enemy collides with a node, execute your code only if its id is different to the last remembered id. In code:

GML:
/// @desc Create event
nodeLast = undefined;

/// @desc Collision with Node event
if (other.id != nodeLast)
{
    // Choose next direction etc.
    nodeLast = other.id;
}
 

Iain

Member
Hi I have tried it and i have posted my code snippet below. Once its hit a node it doesnt seems to want to run the code again.
GML:
speed = 2;
direction = dir;
if (instance_position(x, y, o_node))
{
    var node_last = undefined;
    var inst = instance_position(x, y, o_node);
    
    if (x == inst.x) and (y == inst.y)
    {
        if (inst.id != node_last)
        {
            var new_list = ds_list_create();
            if( inst.go_east == true)
            {
                ds_list_add(new_list, "EAST");   
            }
            if( inst.go_north == true)
            {
                ds_list_add(new_list, "NORTH");   
            }
            if( inst.go_west == true)
            {
                ds_list_add(new_list, "WEST");   
            }
            if( inst.go_south == true)
            {
                ds_list_add(new_list, "SOUTH");   
            }
            var new_dir = choose(0, ds_list_size(new_list) - 1);
            dir = new_dir * 90;   
            ds_list_destroy(new_list);   
            node_last = inst.id;
        }
    }
}
 
Top