Legacy GM Spawn random

O

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?
 
O

ofekgani

Guest
Instead of place_meeting, try place_empty.
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
 

Roderick

Member
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
place_empty should be using spawn_id, not obj_wall.
 

Roderick

Member
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.
 
Top