GML Instance_place checks only 1 object ?

Hi, i am making a save script for my game using instance_place.
Code:
var inst = instance_place(xx,yy,all);
    if(inst)
    {
        var instIndex = inst.object_index;
        ini_write_string("Map0", string(ntp(xx))+","+string(ntp(yy)), instIndex);
    }
Everything was okay till time that i have noticed that if 2 objects are at exacly this same place only one is saved. I made object to check if that's true and results was positive. So my question is if instance_place returns only 1 object id ? If not, how can i make it to return 2 objects id ?
 

Simon Gust

Member
Alternatively, you can reverse the logic.
Code:
// assuming xx and yy are var scoped
with (all)
{
  if (point_in_rectangle(xx, yy, bbox_left, bbox_top, bbox_right, bbox_bottom))
  {
    ini_write_string("Map0", string(ntp(xx))+","+string(ntp(yy)), object_index);
  }
}
I think this will work.
 
Alternatively, you can reverse the logic.
Code:
// assuming xx and yy are var scoped
with (all)
{
  if (point_in_rectangle(xx, yy, bbox_left, bbox_top, bbox_right, bbox_bottom))
  {
    ini_write_string("Map0", string(ntp(xx))+","+string(ntp(yy)), object_index);
  }
}
I think this will work.

Thanks, all ideas are great and works but still i have this same problem. Maybe i made a mistake somewhere else.
 
Create event:
Code:
x=0;
y=0;

ini_open(working_directory + "SaveMap0.ini");
var xx=0, yy=0;
var BlockAmount = ntp(room_height) * ntp(room_width);
    
for(var i=0; i<BlockAmount ;i++)
{
    
    instance_activate_region(
        xx-ptn(1),
        yy-ptn(1),
        ptn(2),
        ptn(2),
        true
        );

    if(yy>=room_height)
    {
        xx+=ptn(1);
        yy = 0;   
    }
    else yy+=ptn(1);

    var inst = instance_place(xx,yy,all);
    if(inst)
    {
        var instIndex = inst.object_index;
        ini_write_string("Map0", string(ntp(xx))+","+string(ntp(yy)), instIndex);
    }

}

instance_destroy();
Destroy event:
Code:
ini_close();
show_debug_message("Save end");
ptn() and ntp() are just simple scripts to for example change 32 to 1, 64 to 2 or reverse. And yes, i know that i could use grid.
 

Simon Gust

Member
Create event:
Code:
x=0;
y=0;

ini_open(working_directory + "SaveMap0.ini");
var xx=0, yy=0;
var BlockAmount = ntp(room_height) * ntp(room_width);
   
for(var i=0; i<BlockAmount ;i++)
{
   
    instance_activate_region(
        xx-ptn(1),
        yy-ptn(1),
        ptn(2),
        ptn(2),
        true
        );

    if(yy>=room_height)
    {
        xx+=ptn(1);
        yy = 0;  
    }
    else yy+=ptn(1);

    var inst = instance_place(xx,yy,all);
    if(inst)
    {
        var instIndex = inst.object_index;
        ini_write_string("Map0", string(ntp(xx))+","+string(ntp(yy)), instIndex);
    }

}

instance_destroy();
Destroy event:
Code:
ini_close();
show_debug_message("Save end");
ptn() and ntp() are just simple scripts to for example change 32 to 1, 64 to 2 or reverse. And yes, i know that i could use grid.
This doesn't look like the updated code.
Also be aware the instance activation takes place next frame, not this frame. So any instances that are being called to activate before saving aren't probably saved.
 
As i said before, that code works but only one problem i have noticed is that when there are 2 objects (blocks) at this same x and y only one of them is being saved (assigned to variable "inst" ). I made another one object to cheks if tahts true.
Code:
//"Right Down" event
var inst = instance_place(x,y,all)

if(inst)
{
    var instIndex = inst.object_index;
    var instName = object_get_name(instIndex);
}

else var instIndex = "NONE"

show_message("obj_instance_place_check: " + string(instIndex) )
And when i aim to that 2 blocks that are in themselves i get message with id of that one which is visible over the other one. When i destroy that one on top and then click right mouse button again this time i get message with id of the second one. So my question is is it possible to get 2 IDs at the same time from instance_place or run code only for one layer/depth ?
 
Okay, i solved it. I have created a script that is creating objects instead of instance_create and saves their x and y to array. Then after some time there is a object which reads that array and write their values to file.
But, there is still a question "Is instance_place returning id for only one object if colliding to two?"
 
Last edited:

FrostyCat

Member
But, there is still a question "Is instance_place returning id for only one object if colliding to two?"
It will return the ID for one of the two instances. If you want all the instances, you need to use instance_place_list() as I described.
Code:
var inst_list = ds_list_create(),
    inst_list_length = instance_place_list(xx, yy, all, inst_list, false);
for (var i = 0; i < inst_list_length; i++)
{
    ini_write_string("Map0", string(ntp(xx))+","+string(ntp(yy)), inst_list[| i].object_index);
}
ds_list_destroy(inst_list);
 
Top