GML How do I make an object move randomly?

S

sharpman5k

Guest
Hello, I'm fairly new to this whole game development thing, have mainly done pixel art in the past but no programming. So even the simplest things are difficult to comprehend for me, though I am more than willing to learn.

What I'm wanting to do is make an enemy move in a random direction, for a random amount of time until it changes to another random direction. An example would be the Stalfos or Darknut enemies from the first Zelda. A quick google search did not really help me, although I did find this under motion_set in the Help System.

if irandom(9) = 1 motion_set(random(360), 1 + random(3));

I first attempted to change (360) into (0, 90, 180, 270) thinking it would chose between those directions, but it instead caused a crash.

I'm not even sure if I'm on the right track.
 

chamaeleon

Member
Not sure what you mean with "it caused a crash".. But in this particular case maybe you'd like to use choose(0, 90, 180, 270) instead of random(360).
 

TheouAegis

Member
Make an alarm[0] and set it to 1 in the create event. Set speed to however fast you want the enemy to walk.

Make an alarm 0 event. In it put

Code:
direction=choose(0,90,180,270):
alarm[0]=choose(30,60);
When it collides with a wall set
speed=-speed
 
S

sharpman5k

Guest
Not sure what you mean with "it caused a crash".. But in this particular case maybe you'd like to use choose(0, 90, 180, 270) instead of random(360).
Make an alarm[0] and set it to 1 in the create event. Set speed to however fast you want the enemy to walk.

Make an alarm 0 event. In it put

Code:
direction=choose(0,90,180,270):
alarm[0]=choose(30,60);
When it collides with a wall set
speed=-speed
Thanks you guys, this makes a lot more sense! I'll try this out now.
 

woods

Member
heres one i been working on lately... with a fair bit of help from the kind people here in the forums..
i have everything in my game moving on a 32x32 grid...(think final fantasy 1 overworld map style)

my cow here goes 2 or 3 "cells" then chooses another direction..


create event
Code:
/// initialize variables

image_speed = 0.1
isWalking = true
counter = 0
move_snap(32,32)
snd_cow_delay = 0
and step event
Code:
/// move random


counter = counter+1

if isWalking = true and counter = 60



{
//pick direction
var number=irandom(8)

//move and change sprite
if (number>3)
    {
        speed=0; image_speed=0.1; //idle
    }
if (number==3)
{
    if(!place_meeting(x+32,y,obj_block))
    {
    motion_set(0,0.6);image_speed=0.2; sprite_index=spr_cow_right;  //east
    }
    else
    {
    speed=0;image_speed=0.1; //idle
    }
}
       
if (number==2)
{
    if (!place_meeting(x-32,y,obj_block))
    {
    motion_set(180,0.6);image_speed=0.2; sprite_index=spr_cow_left; //west
    }
    else
    {
    speed=0;image_speed=0.1; //idle
    }
}
     
if (number==1)
{
    if (!place_meeting(x,y-32,obj_block))
    {
    motion_set(90,0.6);image_speed=0.2; sprite_index=spr_cow_up;  //north
    }
    else
    {
    speed=0;image_speed=0.1; //idle
    }  
}  
if (number==0)
{
    if (!place_meeting(x,y+32,obj_block))
    {
    motion_set(270,0.6);image_speed=0.2; sprite_index=spr_cow_down; //south
    }
    else
    {
    speed=0;image_speed=0.1; //idle
    }
}
       
       
move_snap(32,32);

   
//reset move counter
counter = 0;

}

bonus tidbit.. in step event i have the cow moo one of three random sounds when the player gets close enough ;o)

Code:
/// play sound when near player

if snd_cow_delay > 0
{
    snd_cow_delay -=1;
}
else
{
    num1=irandom(2)
    if num1==0
    {
        if distance_to_object(obj_player) < 32 or distance_to_object(obj_werewolf) < 32
        {
            audio_play_sound(snd_cow_1,10,false)
            snd_cow_delay = 500
        }
    }
    if num1==1
    {
        if distance_to_object(obj_player) < 32 or distance_to_object(obj_werewolf) < 32
        {
            audio_play_sound(snd_cow_2,10,false)
            snd_cow_delay = 500
        }
    }
    if num1==2
    {
        if distance_to_object(obj_player) < 32 or distance_to_object(obj_werewolf) < 32
        {
            audio_play_sound(snd_cow_3,10,false)
            snd_cow_delay = 500
        }
    }
}
 
Top