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

GM:S 1.4 Spawn random

Discussion in 'Programming' started by ofekgani, Sep 27, 2016.

  1. ofekgani

    ofekgani Guest

    In the game when I hit some object so it replaces random position properly.
    This is the code I used:
    =================================================================
    spawn_id= instance_create(0,0,obj_hero);
    do
    {
    spawn_id.x = random(room_width);
    spawn_id.y = random(room_height);

    with(spawn_id)
    {
    collision = place_meeting(x,y,obj_wall)
    }
    }
    until spawn_id.collision = 0
    =================================================================

    My problem it's settled on a particular object.

    A section of code:
    ==============================
    with(spawn_id)
    {
    collision = place_meeting(x,y,obj_wall)
    }
    }
    until spawn_id.collision = 0
    =============================
    This prevents the object to the specified object get settled into a place
    But I want to prevent the object to other objects and get settled into a place only for one object


    How do I do it with code?
     
  2. Roderick

    Roderick Member

    Joined:
    Jun 22, 2016
    Posts:
    580
  3. ofekgani

    ofekgani Guest

    spawn_id= instance_create(0,0,obj_emotionHigh);
    do
    {
    spawn_id.x = random(room_width);
    spawn_id.y = random(room_height);

    with(spawn_id)
    {
    collision= place_empty(obj_wall.x,obj_wall.y)
    }
    }
    until spawn_id.collision = 0

    I did right? Not working
     
  4. Roderick

    Roderick Member

    Joined:
    Jun 22, 2016
    Posts:
    580
    place_empty should be using spawn_id, not obj_wall.
     
  5. ofekgani

    ofekgani Guest

    I don't quite understand.... Can you show me the code
     
  6. ofekgani

    ofekgani Guest

    I don't quite understand. Can you show me the code
     
  7. Roderick

    Roderick Member

    Joined:
    Jun 22, 2016
    Posts:
    580
    collision= place_empty(spawn_id.x,spawn_id.y)
     
  8. ofekgani

    ofekgani Guest

    That's what I try. It gave an error..
    The code also defines where it won't do spawn
     
  9. Roderick

    Roderick Member

    Joined:
    Jun 22, 2016
    Posts:
    580
    I'm baffled. I can't figure this one out.

    I created a project with a sprite that had a full image mask, an object of that sprite named obj_thing and a controller object.

    I put the following code in the controller's create code:
    Code:
    repeat (100)
    {
        var thing = instance_create(0, 0, obj_thing);
        do
        {
            thing.x = random(room_width);
            thing.y = random(room_height);
        }
        until (place_empty(thing.x, thing.y))
    }
    
    I also tried place_free, !place_meeting (with both obj_thing and all), and all three conditions in a while loop. In every case, the objects were happily dropped on top of each other.

    Thinking that maybe the issue was that the objects hadn't yet been drawn, I changed the code so that it was in a step event and drew only one instance per frame. It STILL dropped them on top of each other.

    Thinking that the collisions weren't occurring, I put it back to the way it is above, and added an instance_destroy() collision event to obj_thing - and it created the objects with some overlapping, and then deleted all the overlapping ones.

    I even tried checking Solid on obj_thing, to see if that would make a difference, even though I was certain it shouldn't matter for any except place_free. It didn't work for any of them.

    I don't know what else to suggest, but hopefully someone else can point out what I'm doing wrong.

    Edit: Got it.

    I still don't know why the above code works, but what's below DOES:

    In the creation code for the object (in my case, obj_thing), add the following:
    Code:
     while (place_meeting(x, y, all))
    {
     x = random(room_width);
     y = random(room_height);
    }
    Everything scatters across the room, but nothing overlaps.
     

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