GameMaker Saving multiple instances of an object x and y position

G

gamer_essence

Guest
I'm trying to figure out the most efficient way to save all instances x and y position of a certain object in a room.
Then I would like to load those x and y positions and create them in a separate room. To help visualize what i'm talking about, kinda think of the user creating a multiple object creation and being able to save and load it into a room where their creation would be put to the test.
 

samspade

Member
Is this cross game sessions or within the same session? In other words do you mean save to a file or just put it in some variables? For the second this would work (maybe with some tweaking):

Code:
var i = 0;
with (object) {
    saved_x_array[i] = x;
    saved_y_array[i] = y;
    i += 1;
}
There are other ways as well. You could save both x and y into an array and put the array inside of another array or a list, you could use a ds_grid or 2D array. And of course how you iterate through the objects and increment the value is up to you as w
 
G

gamer_essence

Guest
I attempted something like this and it would put all the objects into the top right hand side of the screen but that could've been an error I made?
 
G

gamer_essence

Guest
yeah im still getting the same problem where it puts all the objects in the top left of the next room
 

FrostyCat

Redemption Seeker
I attempted something like this and it would put all the objects into the top right hand side of the screen but that could've been an error I made?
samspade's code is perfectly fine. What you described usually happens when you don't respect the difference between objects and instances.
NEVER access a single instance by object ID if multiple instances of the object exist. This includes attempts to reference or set object.variable (which is inconsistent across exports) and using with (object) to apply actions to it (this encompasses all instances of the object instead of just the one you want). Verbally, "Dog's colour" makes sense with one dog, but not with multiple dogs.
 
G

gamer_essence

Guest
Well first i have a collision event. This will run through an object i have to scan the room more or less just for proof of concept. The object moves from left to right and down 64 when it reaches the right of the room. It continues this until it ends up at the bottom right hand side of the room.

Collision Event: with obj_orbiter1 // just one of the objects i want it to check for but i wanted to see if i could at least get one to work before i move on to dozens.
var i = 0;
with (other) {
saved_x_array = other.x;
saved_y_array = other.y;
i += 1;
}

that saves the x and y coords of the specific object it collides with.

For the create event i had tried many things and this was one of the only ways i could create this array without errors. I am almost 100% sure that this is a very ghetto way of doing this and i doubt it even works to the extent that i need but as i said this was one of the only ways i didnt get an error.

Create event:
saved_y_array = array_create(100,)
saved_x_array = array_create(100,)

Now with this object set to persistent when i go into another room i do the following.

Step Event:
if (room = rm_test)
{
var i = 1;
if (i < 100)
{
place_x = saved_x_array; // Random variables i set because i was getting an error regarding numbers not working for x and y coords
place_y = saved_y_array;
instance_create_layer(place_x,place_y,"instances",obj_orbiter1)
i += 1;
}
}


Any help deciphering the mess that i have created would be a great help! And the whole scanning thing that i put in is not needed so if theres a way i can make this the most efficient im open to it! Thank you all for the assistance so far
 
Ok, it's immediately clear what the problem is now. You're not using an array. You're CREATING an array at the start, but then you're changing it into a normal variable the instant the scanner hits the object. Here's a way to do what you want:

Create Event
Code:
inst_no = instance_number(obj_orbiter1); // Get how many instances of the object there are
for (var i=0;i<inst_no;i++) { // Loop through that number
   var inst = instance_find(obj_orbiter1,i); // Get the instance ID of each instance as we go through the for loop
   xpos[i] = inst.x; // Store the coordinates of the current instance in two arrays
   ypos[i] = inst.y;
}
Then, in the Room Start event, you can put this (we only need to check if it's rm_test at the start of the room, not every step, hence why we are using the Room Start event):
Code:
if (room == rm_test) {
   for (var i=0;i<inst_no;i++) { // Loop through how many instances we counted at the start.
      instance_create_layer(xpos[i],ypos[i],"instances",obj_orbiter1); // Create an instance at the correct positions
   }
}
I'm not entirely sure why you thought you could save an array without an index (the index is the position within the array you are saving or reading from). In the above example if you tried to read xpos[0] you would get the x position of the first instance the code found in the for loop, if you tried to read xpos[1] you would get the x position of the second instance and so on. You should read the manual on arrays.

As a sidenote, when you start trying to get this working on more than one object, you should look into the concept of Parenting within GM.
 
Last edited:
G

gamer_essence

Guest
Thank you for that! You wouldn't believe how long i've been working on this!
 
Top