1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

Random spawning makes objects end up on top of other objects

Discussion in 'Programming' started by Butterfly6, May 5, 2017.

  1. Butterfly6

    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: May 6, 2017
  2. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,199
    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.
     
  3. Butterfly6

    Butterfly6 Guest

    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!
     
  4. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,199
    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.
     
  5. Fat_Man3468800

    Fat_Man3468800 Member

    Joined:
    Jun 22, 2016
    Posts:
    487
    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
     
  6. Butterfly6

    Butterfly6 Guest

    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...?
     
  7. Butterfly6

    Butterfly6 Guest

    And to what object should I add this code and should I add it as a create-event or what?
    I hope it works!
     
  8. JFitch

    JFitch Member

    Joined:
    Sep 28, 2016
    Posts:
    428
    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);
    }
     
  9. Butterfly6

    Butterfly6 Guest

    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.
     
  10. Fat_Man3468800

    Fat_Man3468800 Member

    Joined:
    Jun 22, 2016
    Posts:
    487
    Add it to the child food object create event. (I think?)
     
  11. Butterfly6

    Butterfly6 Guest

    It gives an error at line 2 position 9 '=' when I add this code...so I can't test it now.
     
  12. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,874
    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.
     
  13. Butterfly6

    Butterfly6 Guest

    It works!! Thank you so much for your help! Now I can finally move on with my game :)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice