Windows (Solved) Using for loop to create/place objects (problem)

M

Merrick

Guest
Hi folks
I've been having a problem with the for loop. I'm probably just not understanding completely how it works yet, but...

Here's my goal/problem: an object that, when created, instantly places objects around itself in a grid pattern.forloop_object_placement2.png

Here is the code that I am using to try and make this work in obj_grey's Create Event:

//Loop Variable
yrow = -32;
//Loop for placing attack squares
for (i=0; i<2; i+=1)
{
var xcolumn;
xcolumn =(32*i)-32;
instance_create(x+xcolumn,y+yrow,obj_red);
if (xcolumn >= 32)
{
i = 0;
yrow +=32;
if (yrow > 32)
{
break;
}
}
}

Here is a screenshot of the results:
forloop_object_placement.JPG

As you can see, it seems to only place the first two squares and then stops. I'm perplexed as to why.
I noticed that if I change the for loop to read "for(i=0;1<3; i+=1)" instead, I get this:
forloop_object_placement3.JPG
Close... but I'm not sure why that changed anything...

I can't seem to get my head around this problem. Can anyone enlighten me?
 
Last edited by a moderator:

TheouAegis

Member
I could try to figure out why you original code does what it does, but instead I will suggest you just do what most people do.

your iterator is i but you don't use it anywhere except to calculate xcolumn. You may as well just use two loops, one that loops through all three columns and one that loops through all three rows. And you don't have to do any of those funky calculations and you can be certain that all 8 or 9 squares get drawn.
 

Bingdom

Googledom
You should be using 2 'for' loops. 1 For the x, the other for the y.

Code:
for(var i=0;i<3;i++) {
    for(var j=0; j<3;j++) {
    //Run checks here
    }
}
Use i and j for the coordinate checking and spawning.
 
T

t0tallyKy1e

Guest
I could be way off, but you could try using one for loop for just the xColumn variable with another for loop for the yRow variable inside it. Then you would create the instance inside the yRow for loop as long as the current spot isn't where the object your surrounding is. If you're new to for loops, just make sure the variable you're incrementing in the inner loop is different from the outer loop. So something like this...

for(i = 0; i < 3; i++){
yrow = -32;
xcolumn = (32 * i) - 32;

for(j = 0; j < 3; j++){
if(yrow != y && xcolumn != x){ //something to check if the current spot is where object_grey is
instance_create(x+xcolumn, y+yrow, obj_red);
}
}
}

Efficiency-wise this kind of sucks, but if you're just starting out and learning how things work it's fine, just be aware that if you want to do this for a million tiles for some reason, your computer is going to slow way down.

...I did bad on a test today and felt the need to redeem myself, hopefully this helps you!
 

TheouAegis

Member
Ok now that I'm home....

Code:
for(i=0; i<9; i++)
if i !=4 instance_create(x+32*(i mod 3 - 1), y+32*(i div 3 - 1), obj_red);
 
Last edited:
M

Merrick

Guest
Thanks everyone, you've all definitely helped!
I used the following code and it looks like it worked (obj_grey placed all 8 objects around itself successfully). I used the following code:

for (i=0; i<3; i+=1)
{
for (j=0; j<3; j+=1)
{
xcolumn =(32*i)-32;
yrow = (32*j)-32;
if(yrow != y && xcolumn != x)
{
instance_create(x+xcolumn,y+yrow,obj_red);
}
}
}

hopefully it's not eating up too much memory :)
 
M

Merrick

Guest
Ok now that I'm home....

Code:
for(i=0; i<9; i++)
if !=4 instance_create(x+32*(i mod 3 - 1), y+32*(i div 3 - 1), obj_red);
Hi TheouAegis, what did you mean by !=4 ?
Gamemaker says that there's an unexpected symbol in there.
Thanks for your help!
 
Top