Random spawning makes objects end up on top of other objects

B

Butterfly6

Guest
Hello everyone,
I don't have a lot of experience in using game maker yet, so I hope you can help me out with the following problem. I am making a game in which a person in walking around in a supermarket and gains points by colliding with healthy foods and loses points by colliding with junk food. I want a new piece of food to appear randomly in the room every time the other piece gets eaten. My problem is that the new piece of food sometimes appears on top of another object in the room (for example on top of a craddle or a check out desk). The person in the game is than is not able to eat that piece of food anymore.

The step event of obj_controller:
Code:
if !instance_exists(obj_pizza)
{
    instance_create(random(room_width), random(room_height),obj_pizza)
    x = random(room_width);
    y = random(room_height);
}

if !instance_exists(obj_pancakes)
{
    instance_create(random(room_width), random(room_height),obj_pancakes)
    x = random(room_width);
    y = random(room_height);
}
The create event of the parent of all the junk foods:
Code:
while (distance_to_object(obj_krataubergines) <100)
{
    x=random(room_width);
    y=random(room_height);
}

while (distance_to_object(obj_krattomaat) <100)
{
    x=random(room_width);
    y=random(room_height);
}

while (distance_to_object(obj_krataardpl) <100)
{
    x=random(room_width);
    y=random(room_height);
}

while (distance_to_object(obj_krat) <100)
{
    x=random(room_width);
    y=random(room_height);
}

while (distance_to_object(obj_kratmais) <100)
{
    x=random(room_width);
    y=random(room_height);
}

while (distance_to_object(obj_bankerella) <100)
{
    x=random(room_width);
    y=random(room_height);
}

while (distance_to_object(obj_kassa) <100)
{
    x=random(room_width);
    y=random(room_height);
}

while (distance_to_object(obj_kast) <100)
{
    x=random(room_width);
    y=random(room_height);
}

while (distance_to_object(obj_wijn) <100)
{
    x=random(room_width);
    y=random(room_height);
}

while (distance_to_object(obj_brood) <100)
{
    x=random(room_width);
    y=random(room_height);
}
I tried this code above so that the food would not appear closer than 100 to that object, but it still does. I have no idea how to fix this.

I really hope that someone can help me out with this!
Thank you in advance!
 
Last edited by a moderator:

Simon Gust

Member
Hi,
Child objects don't automatically run the code of their parent's create event and for that there is a function called "event_inherited()" which is put in every child-object to run their parent's creation code.
 
B

Butterfly6

Guest
Hi,
Child objects don't automatically run the code of their parent's create event and for that there is a function called "event_inherited()" which is put in every child-object to run their parent's creation code.
Hello,
I have tried what you said, but the food still appears on top of other objects. I now have the same 2 codes as in my earlier message, but I have now added the following code to the create event of all the child food objects:
Code:
event_inherited();
Is there information missing in my code or why is is not working with me?

Thanks in advance!
 

Simon Gust

Member
So I've made a project with 2 parent objects (obj_healthy_food and obj_chunk_food). 1 child object for each (obj_salad and obj_burger).
In the obj_burger-create event I put:
Code:
while (distance_to_object(obj_healthy_food) < 100)
{
    x = random_range(0, 300);
    y = random_range(0, 300);
}
So instead of checking for each child of obj_health_food I checked just for the parent and it worked for me. They were next to each other in the room editor, I started the game and obj_burger was not next to obj_salad anymore.
And for some reason I didn't need the event_inherited() either. I've never used it anyway.
 
D

DarthTenebris

Guest
Try this out:
Code:
randomize();
var xx = irandom_range(0, room_width);
while(!place_meeting(xx - (sprite_width / 2))) {
     var yy = irandom_range(0, room_height);
     while(!place_meeting(yy - (sprite_height / 2))) {
          x = xx;
          y = yy;
     }
}
I didn't test it out, so I'm asking you to :p
 
B

Butterfly6

Guest
So I've made a project with 2 parent objects (obj_healthy_food and obj_chunk_food). 1 child object for each (obj_salad and obj_burger).
In the obj_burger-create event I put:
Code:
while (distance_to_object(obj_healthy_food) < 100)
{
    x = random_range(0, 300);
    y = random_range(0, 300);
}
So instead of checking for each child of obj_health_food I checked just for the parent and it worked for me. They were next to each other in the room editor, I started the game and obj_burger was not next to obj_salad anymore.
And for some reason I didn't need the event_inherited() either. I've never used it anyway.
I tried what you said, but the foods still appear on top of other objects sometimes. Did you leave the codes which I mentioned in my first post the way they are or did you change/delete these? And what exectly does to (0,300) refer to? Because I also tried changing these numbers, but it still didn't work out for me.

I really wonder why it does work with you and not with me...?
 
B

Butterfly6

Guest
Try this out:
Code:
randomize();
var xx = irandom_range(0, room_width);
while(!place_meeting(xx - (sprite_width / 2))) {
     var yy = irandom_range(0, room_height);
     while(!place_meeting(yy - (sprite_height / 2))) {
          x = xx;
          y = yy;
     }
}
I didn't test it out, so I'm asking you to :p
And to what object should I add this code and should I add it as a create-event or what?
I hope it works!
 
J

JFitch

Guest
The problem with your code is that you check each object one at a time, so one piece of code can undo a piece of code before it. You first pick a spot that's not near the first object, then after that you pick a spot that's not near the second object, but it can now be near the first object.

I don't know if parents work here. If they do, you can do this, with "PARENT" replaced with the parent object.
Code:
while (distance_to_object(PARENT) <100)
{
   x=random(room_width);
   y=random(room_height);
}
If parents don't work here, you can use an "or" statement.

Code:
 while (distance_to_object(obj_krataubergines) <100) || (distance_to_object(obj_krattomaat) <100) || (distance_to_object(obj_krataardpl) <100) || (distance_to_object(obj_krat) <100) ||
(distance_to_object(obj_kratmais) <100) || (distance_to_object(obj_bankerella) <100) || (distance_to_object(obj_kassa) <100) || (distance_to_object(obj_kast) <100) || (distance_to_object(obj_wijn) <100) || (distance_to_object(obj_brood) <100)
{
   x=random(room_width);
   y=random(room_height);
}
You can also use the "min" function.
Code:
 while (min(distance_to_object(obj_krataubergines),(distance_to_object(obj_krattomaat),(distance_to_object(obj_krataardpl),(distance_to_object(obj_krat),(distance_to_object(obj_kratmais),(distance_to_object(obj_bankerella),(distance_to_object(obj_kassa),(distance_to_object(obj_kast),(distance_to_object(obj_wijn),(distance_to_object(obj_brood))<100)
{
   x=random(room_width);
   y=random(room_height);
}
 
B

Butterfly6

Guest
The problem with your code is that you check each object one at a time, so one piece of code can undo a piece of code before it. You first pick a spot that's not near the first object, then after that you pick a spot that's not near the second object, but it can now be near the first object.

I don't know if parents work here. If they do, you can do this, with "PARENT" replaced with the parent object.
Code:
while (distance_to_object(PARENT) <100)
{
   x=random(room_width);
   y=random(room_height);
}
If parents don't work here, you can use an "or" statement.

Code:
 while (distance_to_object(obj_krataubergines) <100) || (distance_to_object(obj_krattomaat) <100) || (distance_to_object(obj_krataardpl) <100) || (distance_to_object(obj_krat) <100) ||
(distance_to_object(obj_kratmais) <100) || (distance_to_object(obj_bankerella) <100) || (distance_to_object(obj_kassa) <100) || (distance_to_object(obj_kast) <100) || (distance_to_object(obj_wijn) <100) || (distance_to_object(obj_brood) <100)
{
   x=random(room_width);
   y=random(room_height);
}
You can also use the "min" function.
Code:
 while (min(distance_to_object(obj_krataubergines),(distance_to_object(obj_krattomaat),(distance_to_object(obj_krataardpl),(distance_to_object(obj_krat),(distance_to_object(obj_kratmais),(distance_to_object(obj_bankerella),(distance_to_object(obj_kassa),(distance_to_object(obj_kast),(distance_to_object(obj_wijn),(distance_to_object(obj_brood))<100)
{
   x=random(room_width);
   y=random(room_height);
}
The parent thing does not work in my case. And I tried the 'or' and 'min' statement, but they both don't work :( The foods still appear on top of craddles and things like that.
But thanks anyway! I hope you can figure out why it's not working with me.
 
B

Butterfly6

Guest
Try this out:
Code:
randomize();
var xx = irandom_range(0, room_width);
while(!place_meeting(xx - (sprite_width / 2))) {
     var yy = irandom_range(0, room_height);
     while(!place_meeting(yy - (sprite_height / 2))) {
          x = xx;
          y = yy;
     }
}
I didn't test it out, so I'm asking you to :p
It gives an error at line 2 position 9 '=' when I add this code...so I can't test it now.
 

Yal

šŸ§ *penguin noises*
GMC Elder
Something like this would probably work better... (you'd run this in the food object whenever they move: if you destroy the food on collision and create a new food object, you'd put this in their Create event)
Code:
randomize()//You actually only need to call this once, when the game starts, so I'd move it there instead.
while(place_meeting(x,y,parent_solidobject)){
  x = irandom_range(0,room_width)
  y = irandom_range(0,room_height)
}
Then you create a new object called parent_solidobject and make all objects the food can't be on top on have that set as their parent.
 
B

Butterfly6

Guest
Something like this would probably work better... (you'd run this in the food object whenever they move: if you destroy the food on collision and create a new food object, you'd put this in their Create event)
Code:
randomize()//You actually only need to call this once, when the game starts, so I'd move it there instead.
while(place_meeting(x,y,parent_solidobject)){
  x = irandom_range(0,room_width)
  y = irandom_range(0,room_height)
}
Then you create a new object called parent_solidobject and make all objects the food can't be on top on have that set as their parent.
It works!! Thank you so much for your help! Now I can finally move on with my game :)
 
Top