This loop keeps setting objects to same x coordinate or it does not even create them all expect the first one pls help.

D

DNgames

Guest
-I want to make save system for my buildings but it wont work so here are the codes.

name = obj_const_wall;

for(var i = 0; i < obj_stats.maxstructures; i++)
{
if(obj_stats.structure[i, 0] == -1 and obj_stats.structure[i, 1] == -1)
{
obj_stats.structure[i, 0] = name;
obj_stats.structure[i, 1] = x;
}
}

-Code above is set in objects create event. When created i want to place it in 2D array and store its x coordinate and object name so later i can place it again in same place.



if(room == rm_test)
{
for(var i = 0; i < maxstructures; i++)
{
if(structure[i, 0] != -1)
{
instance_create(structure[i, 1], 288, structure[i, 0]);
}
}
}

-This code above when room starts if its that specific room it creates all those structures that are stored in array and place them in their x coordinate but it seems like it wont work. Idk what is the problem saving or placing objects.

-And in create event of that obj_stats i set all of 2D array to -1.

for(var i = 0; i < maxstructures; i++)
{
for(var j = 0; j < maxstructures; j++)
{
structure[i, j] = -1;
}
}
 

Nidoking

Member
EDIT: I've been misreading this. I see what you're doing, but I don't think you've quite thought this through. When you do get to rm_test, you're creating a bunch of obj_const_wall. Aren't they going to add themselves to the obj_stats.structure array?

On second thought, no, they're not, because the first one that's created puts itself into ALL of the open spots. You forgot to break out of the for loop. But even if you fix that, creating instances will fill the array with copies of the same instance if you don't set something to prevent that. Also, you don't need to pre-fill the second axis up to maxstructures if you're only using two values.
 
Last edited:

chamaeleon

Member
GML:
name = obj_const_wall;

for(var i = 0; i < obj_stats.maxstructures; i++)
{
    if(obj_stats.structure[i, 0] == -1 and obj_stats.structure[i, 1] == -1)
    {
        obj_stats.structure[i, 0] = name;
        obj_stats.structure[i, 1] = x;
    }
}
You're using the same x coordinate over and over (the x coordinate of the instance executing this code, a single non-changing value unless you modify it in the loop) for all the array positions, unless you have some other code that modify it. If you don't want the x coordinate to be the same for all instances created later, what exactly do you want their distribution to look like?
 
D

DNgames

Guest
EDIT: I've been misreading this. I see what you're doing, but I don't think you've quite thought this through. When you do get to rm_test, you're creating a bunch of obj_const_wall. Aren't they going to add themselves to the obj_stats.structure array?

On second thought, no, they're not, because the first one that's created puts itself into ALL of the open spots. You forgot to break out of the for loop. But even if you fix that, creating instances will fill the array with copies of the same instance if you don't set something to prevent that. Also, you don't need to pre-fill the second axis up to maxstructures if you're only using two values.
Actually i 💩💩💩💩ing did not thought about. Thanks. Omg im stupid. Just realized how to fix it lol.
 
Top