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

GML Kindly, need some help with basic AI behaviour (ecosystem)

J

Joao Cruz Malhao

Guest
Im still fairly new to gml and I've been messing around with basic AI hunger systems (want to create somekind of simulation with a basic ecosystem, pig eats plant, wolf eats pig and from those source code can create variety of animals), currently i have a hunger variable value that decreases each second, and once it hits a certain value, pig_obj moves towards grass_obj. but i hit a stop when tried to get to the instance_nearest and continue off to the next nearest instance and so on. instead pig_obj just doesnt move.

here's the STEP EVENT code:
Code:
grass.x = instance_nearest(x,y,grass_obj).x
grass.y = instance_nearest(x,y,grass_obj).y

if (pig_hunger < 50) {    
    move_towards_point(grass.x, grass.y, 5);
}
        else {
            pig_speed = 0;
        }

rest of the code seems to be working fine but ill insert here just in case.
CREATE EVENT:
Code:
pig_health = 100
pig_hunger = 100
pig_stamina = 100
pig_thirst = 100
pig_speed = 5
alarm[0] = room_speed;
ALARM[0]:
Code:
pig_hunger -= 10;
pig_thirst -= 10;
alarm[0] = room_speed;
DRAW:
Code:
draw_text(20, 20, pig_hunger);
draw_text(20, 50, pig_thirst);
draw_self();
COLLISION >< WITH GRASS_OBJ:
Code:
pig_hunger += 50
instance_deactivate_object(grass_obj);
any help or tips would be much appreciated.
 
Last edited by a moderator:

mar_cuz

Member
Replace your step event code with this

Code:
Code:
STEP EVENT
var grass = instance_nearest(x,y,grass_obj);

//pig goes to grass
if (pig_hunger < 50) {
    pig_speed = 5;
    move_towards_point(grass.x, grass.y, pig_speed);
}

//pig stops at grass
If (distance_to_point(grass.x, grass.y) < 2) {
      pig_speed = 0;
        }
Change your collision with grass code to this
Code:
pig_hunger += 25;
instance_destroy(other);
 
Last edited:
J

Joao Cruz Malhao

Guest
Tried it, got this error. running on GMS 2


Object: pig_obj Event: Step at line 10 : unknown function or script If

line10 :
Code:
  If (distance_to_point(grass.x, grass.y) < 1) {
 

mar_cuz

Member
Tried it, got this error. running on GMS 2


Object: pig_obj Event: Step at line 10 : unknown function or script If

line10 :
Code:
  If (distance_to_point(grass.x, grass.y) < 1) {
Try

Code:
if (point_distance( x, y, grass.x, grass.y) <2) {
I thought gm2 still had distance to point.
 

TheouAegis

Member
Personally I'd avoid distance_to* functions, as they're typically going to be slower. Whereas point_distance does require 2 extra arguments, the formulation is restricted to those 4 points, not some spatial partitioning algorithm between the point and the bounding box. Also if you ever decide to not give the pig an actual sprite for any reason, point_distance will still work whereas distance_to_point would probably crap out.

Besides, if you're doing distance_to_point, you may as well just do distance_to_object instead if it's still around, since essentially that's what you're doing anyway and it doesn't require the origin to be centered.
 
J

Joao Cruz Malhao

Guest
Although when there's no more grass_obj 's in the room, game crashes.
crash report:
Code:
FATAL ERROR in
action number 1
of  Step Event0
for object pig_obj:

Variable <unknown_object>.y(1, -2147483648) not set before reading it.
 at gml_Object_pig_obj_Step_0 (line 14) - if (distance_to_point(grass.x, grass.y) < 1) {
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_pig_obj_Step_0 (line 14)
Thank you for the help
 
J

Joao Cruz Malhao

Guest
Personally I'd avoid distance_to* functions, as they're typically going to be slower. Whereas point_distance does require 2 extra arguments, the formulation is restricted to those 4 points, not some spatial partitioning algorithm between the point and the bounding box. Also if you ever decide to not give the pig an actual sprite for any reason, point_distance will still work whereas distance_to_point would probably crap out.

Besides, if you're doing distance_to_point, you may as well just do distance_to_object instead if it's still around, since essentially that's what you're doing anyway and it doesn't require the origin to be centered.
I'll look into it, thank you
 

mar_cuz

Member
Although when there's no more grass_obj 's in the room, game crashes.
crash report:
Code:
FATAL ERROR in
action number 1
of  Step Event0
for object pig_obj:

Variable <unknown_object>.y(1, -2147483648) not set before reading it.
 at gml_Object_pig_obj_Step_0 (line 14) - if (distance_to_point(grass.x, grass.y) < 1) {
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_pig_obj_Step_0 (line 14)
Thank you for the help
Just add a check if instance_exists(grass) { before if distance_to_point
 
Top