roaming AI help

P

piksil_demon

Guest
ok, earlier i asked for help in an admittedly difficult AI, and after all their help i feel its close to working. however, my problem right now it the roaming AI. heres what i got-
create-
direction = irandom(8) * 45;

alarm 0-
direction = irandom(8) * 45;
speed = 2;

step-
if (alarm[0] = -1) alarm[0] = room_speed*3;
image_angle = direction;


the problem is the AI is not moving at all, nor is it changing direction.
i also want to add in an occasional stop in motion. any advice?
 
W

whale_cancer

Guest
Edit: Whoops I misread something. Please delete/ignore this post.
 

Bingdom

Googledom
This is what i did for a wandering AI

OBJ_Player
CREATE EVENT:
Code:
tar_x = random(room_width); //Random spot in map
tar_y = random(room_height);
sleep = 5; //The duration in seconds until the player can move again
move = true; //Whether the player should move or not
dir = point_direction(x,y,tar_x,tar_y);
spd = 3; //Speed of object
ALARM[0] EVENT:
Code:
tar_x = random(room_width); //Random spot in map
tar_y = random(room_height);
move = true; //Move again
dir = point_direction(x,y,tar_x,tar_y);
STEP EVENT:
Code:
if (move) {
    x+=lengthdir_x(spd,dir); //Movement
    y+=lengthdir_y(spd,dir);
    if distance_to_point(tar_x,tar_y) < 8 {
        move = false //Stop moving
        alarm[0] = room_speed*sleep; //Can move again after 5 seconds
    }
}
 
P

piksil_demon

Guest
This is what i did for a wandering AI

OBJ_Player
CREATE EVENT:
Code:
tar_x = random(room_width); //Random spot in map
tar_y = random(room_height);
sleep = 5; //The duration in seconds until the player can move again
move = true; //Whether the player should move or not
dir = point_direction(x,y,tar_x,tar_y);
spd = 3; //Speed of object
ALARM[0] EVENT:
Code:
tar_x = random(room_width); //Random spot in map
tar_y = random(room_height);
move = true; //Move again
dir = point_direction(x,y,tar_x,tar_y);
STEP EVENT:
Code:
if (move) {
    x+=lengthdir_x(spd,dir);
    y+=lengthdir_y(spd,dir);
    if distance_to_point(tar_x,tar_y) < 8 {
        move = false
        alarm[0] = room_speed*sleep;
    }
}
not quite what im looking for. i want him to roam aimlessly in the 8 cardinal directions, but on a 2-4 second time frame before changing direction. i like how simple you made it tho, i think i can modify it a bit. can you explain what tar_x/y do? i cant find that in the manual
 

Bingdom

Googledom
tar_x and tar_y are just variables, its just short for target_x and target_y. I used them to pick a random coordinate in the room and used it to move the player towards the direction. I thought you might've preferred a 360 direction AI, that's all. :)

Looking at your current code, you didn't active the alarm in the create event.
You forgot alarm[0] = room_speed * random_range(2,4);
Just noticed :p.

EDIT:
Be careful, because your code can possibly make the character run outside of the room.
 

Tthecreator

Your Creator!
Are you sure that the alarm event is being ran?
You can check this by putting show_message("I'm alarm 0") inside alarm 0.

If it doesnt, you can use alarms in the way I usually do them:

Code:
create-
direction = irandom(8) * 45;
alarm[0]=room_speed*3;

alarm 0-
direction = irandom(8) * 45;
speed = 2;
image_angle=direction;
alarm[0] = room_speed*3;

step-
//we don't even need a step event for this
Also we can now make the object stop occasionally:


Code:
create-
direction = irandom(8) * 45;
alarm[0]=room_speed*3;

alarm 0-
direction = irandom(8) * 45;
if irandom(100)=1 then{
speed= 0;
}else{
speed = 2;
}
image_angle=direction;
alarm[0] = room_speed*3;
 
S

Shariku Onikage

Guest
Your code is fine (just ran it myself to check), though it'll take 3 seconds for the objects to start moving.

Is your room speed set to 30 (open the room and click the settings tab)? If it's been changed it could stretch out the alarm to an eternity.

If not that then there may be some other code that's halting the object's speed. Press shift+ctrl+f and search for 'speed' to see everything relating to it.

Alternatively move speed=2 to the create event and it should start moving straight away.
 
P

piksil_demon

Guest
Your code is fine (just ran it myself to check), though it'll take 3 seconds for the objects to start moving.

Is your room speed set to 30 (open the room and click the settings tab)? If it's been changed it could stretch out the alarm to an eternity.

If not that then there may be some other code that's halting the object's speed. Press shift+ctrl+f and search for 'speed' to see everything relating to it.

Alternatively move speed=2 to the create event and it should start moving straight away.
the problem may be that im using a parent object for the mob. does the parent object have to be in the room, or dose the child take all properties of the parent regardless?
 

Bingdom

Googledom
The child does take the properties of the parent regardless, but can easily be replaced if you put matching events in the child.
If you have put events inside the child that matches the parent, then you can use event_inherited().
 
P

piksil_demon

Guest
Are you sure that the alarm event is being ran?
You can check this by putting show_message("I'm alarm 0") inside alarm 0.

If it doesnt, you can use alarms in the way I usually do them:

Code:
create-
direction = irandom(8) * 45;
alarm[0]=room_speed*3;

alarm 0-
direction = irandom(8) * 45;
speed = 2;
image_angle=direction;
alarm[0] = room_speed*3;

step-
//we don't even need a step event for this
Also we can now make the object stop occasionally:


Code:
create-
direction = irandom(8) * 45;
alarm[0]=room_speed*3;

alarm 0-
direction = irandom(8) * 45;
if irandom(100)=1 then{
speed= 0;
}else{
speed = 2;
}
image_angle=direction;
alarm[0] = room_speed*3;
the mob still isnt moving. :(
 
S

Shariku Onikage

Guest
Can you export/upload the project and provide a link? My guess is it's going to be something screwing up speed.
 
S

Shariku Onikage

Guest
Open up the object window and click on Show information at the bottom. Copy and paste all the data provided there. If there's anything causing the issue, we should be able to see it

upload_2016-8-24_11-16-37.png
 
P

piksil_demon

Guest
Open up the object window and click on Show information at the bottom. Copy and paste all the data provided there. If there's anything causing the issue, we should be able to see it

View attachment 2271
Information about object: obj_enemy_parent
Sprite:
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children
obj_enemy_basic
Mask:

No Physics Object
Create Event:
execute code:

direction = irandom(8) * 45;
alarm[0]=room_speed*3;
speed=2;


Alarm Event for alarm 0:
execute code:

direction = irandom(8) * 45;
if irandom(100)=1 then{
speed= 0;
}else{
speed = 2;
}
image_angle=direction;
alarm[0] = room_speed*3;

Step Event:
execute code:

if alarm[0] = -1{ alarm[0] = room_speed*3;}


Information about object: obj_arrow
Sprite: spr_arrow
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Step Event:
execute code:

hit = instance_place (x,y, obj_enemy_parent);

if (hit != noone)
{
hit.hp -= global.atk;
instance_destroy();
}

Other Event: Outside Room:
execute code:

instance_destroy();

Information about object: obj_player
Sprite: spr_player
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

hp = 10;
spd= 1
ammo = 10


Step Event:
execute code:

//variables
var up, down, left, right, arrow;
up = keyboard_check (ord('W'));
down = keyboard_check (ord('S'));
left = keyboard_check (ord('A'));
right = keyboard_check (ord('D'));


//player movemnt
image_angle = point_direction(x, y, mouse_x, mouse_y);

if right {
speed = spd; direction = 0;
}
if up {
speed = spd; direction = 90;
}
if left {
speed = spd; direction = 180;
}
if down {
speed = spd; direction = 270;
}

if up and right {
speed = spd; direction = 45;
}
if up and left {
speed = spd; direction = 135;
}
if down and left {
speed = spd; direction = 225;
}
if down and right {
speed = spd; direction = 315;
}

if! up and !down and !left and !right {speed=0};

//collision
if instance_place (x,y,obj_enemy_parent) {hp -= global.enatk;}
//ranged attack

if( mouse_check_button_pressed( mb_right))
{ if ammo >= 1{
var arrow = instance_create( x, y, obj_arrow);
arrow.direction = point_direction(x, y, mouse_x, mouse_y);
arrow.speed = 10;
arrow.image_angle = point_direction(x, y, mouse_x, mouse_y);
ammo+= -1;}}

//audio_play_sound (snd_fire_ammo, 10, false)
//ammo += -1
//else (snd_no_ammo, 10, false);}
// player damage and death
//if hp <= 0 instance_destroy();

//if keyboard_check_pressed(ord("R")) game_restart();
//if hp >= 0 game_restart();

Information about object: obj_globals
Sprite:
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

global.enatk = 1;
global. atk = 1;
 
S

Shariku Onikage

Guest
Is there any information about obj_enemy_basic, or is it just empty?
Also, it it obj_enemy_parent or obj_enemy_child that's been placed in the room.

EDIT: Ignore that, parent has no sprite, so it must be child.

Try putting both side by side (making a sprite for parent) and see what happens.
 
S

Shariku Onikage

Guest
Also, can you provide the Show information for obj_enemy_child, just in case...
 
P

piksil_demon

Guest
Also, it it obj_enemy_parent or obj_enemy_child that's been placed in the room.

EDIT: Ignore that, parent has no sprite, so it must be child.

Try putting both side by side (making a sprite for parent) and see what happens.
the parent moves, but allways to the right, then up :/
 
P

piksil_demon

Guest
Also, can you provide the Show information for obj_enemy_child, just in case...
Information about object: obj_enemy_basic
Sprite: spr_enemy_basic
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent: obj_enemy_parent
Children:
Mask:
No Physics Object
Create Event:
execute code:

hp=10

Step Event:
execute code:

if hp <= 0 instance_destroy();
 

Bingdom

Googledom
There's your problem, like i've mentioned earlier. The parent and child BOTH have the step and create event. You need to put event_inherited() in both events in the child object. :)
 
S

Shariku Onikage

Guest
Drop a whole bunch of parents down in different parts of the room and you should see them move differently. Each instance is going to run the same random number when being ran through game maker (it'll be more random when it gets converted to a solo executable file).

Though in all honesty i can't see why the children aren't moving...
 
S

Shariku Onikage

Guest
No wait. Just clicked. Remove your child's create and step events. It's overwriting the parents.

Or use event_inherited(); as suggested by Bingdom.

I should really read other people's entries...
 
P

piksil_demon

Guest
Drop a whole bunch of parents down in different parts of the room and you should see them move differently. Each instance is going to run the same random number when being ran through game maker (it'll be more random when it gets converted to a solo executable file).

Though in all honesty i can't see why the children aren't moving...
@Bingdom (#googledom FTW) figured it out. thank you tho. with his solution i can ad unique code to each child.

now all i need to do is figure out how to make it truly random. every time i restart it does the same thing lol
 
Top