• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML [SOLVED] The same code in a different place behaves differently

C

Cupid Stunt

Guest
I have a small bit of code:
Code:
if(!global.round_running) {
    global.round_running = true;
    global.board = ds_list_create();
    for(x_index = 0; x_index < size_x; x_index += 1) {
        column = ds_list_create()
        for(y_index = 0; y_index < size_y; y_index += 1) {
            y_pos = global.bottom - (size * y_index) - 1;
            x_pos = left + (size * x_index);
            ds_list_add(column, instance_create_layer(x_pos, y_pos, layer, obj_tile));
        }
        ds_list_add(global.board, column);
    }
}
If I place it in the Game Start event of an object it works as expected.


However, if I place it in Step event in the same object, it behaves slightly differently and leaves a gap above the bottom row.


I can't for the life of me figure out why.
 

TheouAegis

Member
That would suggest global.bottom or size got changed in one of the steps. It's actually moving the other rows up, not the last row down.
 

devKathy

Member
First question, does global.bottom get set by any other object(s) at any time? That is, might we expect it to change in the intervening time, since it affects the computation of the y value?

That's my first suspect since other objects have permission to touch it, as a global variable.
 
C

Cupid Stunt

Guest
That would suggest global.bottom or size got changed in one of the steps. It's actually moving the other rows up, not the last row down.
First question, does global.bottom get set by any other object(s) at any time? That is, might we expect it to change in the intervening time, since it affects the computation of the y value?

That's my first suspect since other objects have permission to touch it, as a global variable.
Great question, but no. The only reason that global.bottom is global is to obj_tile instances can know when they've hit bottom if they have to fall.

From the obj_tile Step event:
Code:
// Above the bottom and not touching
if(y < global.bottom) && (!touching) {
    y += 8;
}
That would suggest global.bottom or size got changed in one of the steps. It's actually moving the other rows up, not the last row down.
Size is set in the Game Start event:
Code:
size = 64 * global.scale;
It never changes, just gets referenced.
 
Last edited by a moderator:

devKathy

Member
Okay so we can think of your global variables as constants for our purposes.

I'd be interested to see what happened if you printed out the y values in each case. I realize they probably wouldn't change if those two numbers are really constant, but we should rule it out first.

Next thing to look at is layers? But that's GMS2 so it's all new to me...
 
C

Cupid Stunt

Guest
I'd be interested to see what happened if you printed out the y values in each case. I realize they probably wouldn't change if those two numbers are really constant, but we should rule it out first.
Good idea. I will test that. Thank You.
I am also going to comment out sections of code, one at a time, to see if I can isolate any offensive code.
 

TheouAegis

Member
Put var before x_index and y_index at the beginning of those for() loops. And put var before column=ds_list_create(). It shouldn't make a difference, but it's bugging me, especially since this is a one-off code. lol

Is that stuff on the left affecting it? In your "good" arrangement, there's stuff on the left. In the "bad" arrangement, there isn't.
 
C

Cupid Stunt

Guest
Put var before x_index and y_index at the beginning of those for() loops. And put var before column=ds_list_create(). It shouldn't make a difference, but it's bugging me, especially since this is a one-off code. lol

Is that stuff on the left affecting it? In your "good" arrangement, there's stuff on the left. In the "bad" arrangement, there isn't.
The stuff is in both, just a quick crop job makes the difference.

Code:
if(!global.round_running) {
    global.round_running = true;
    global.board = ds_list_create();
    for(var x_index = 0; x_index < size_x; x_index += 1) {
        var column = ds_list_create()
        for(var y_index = 0; y_index < size_y; y_index += 1) {
            var y_pos = global.bottom - (size * y_index) - 1;
            var x_pos = left + (size * x_index);
            ds_list_add(column, instance_create_layer(x_pos, y_pos, layer, obj_tile));
        }
        ds_list_add(global.board, column);
    }
}
Same result.

I'm off to bed. In the morning I will print out the y_pos values as devKathy wisely suggests. Also, I will comment out blocks of code to see if I can eliminate the problem and thus isolate it.

Thanks so far. Results to follow.

@TheouAegis
So the problem is in my obj_tile's step event, specifically:
Code:
// Above the bottom and not touching
if(y < global.bottom) && (!touching) {
    y += 8;
}
If I comment it out the problem goes away.
So I moved global.round_running = true; to the bottom line and added a check for it
Code:
// Above the bottom and not touching
if(global.round_running) {
    if(y < global.bottom) && (!touching) {
        y += 8;
    }
}
but even with that, the problem persists.
The tiles can't help but to move.

Think I have a bigger issue. Back to the drawing board in the AM.

Nope, that's the issue, and it's darned frustrating.
 
Last edited by a moderator:

devKathy

Member
If there are any other places you changed y, watch out haha. We had a sneaky one right there that was outside of the initial given context. :)
 
C

Cupid Stunt

Guest
I commented out all code except the code necessary to create the tiles, and the problem persisted.

Then I commented out this in the obj_tile Step event: (Code Block 1)
Code:
if(touching) { // If touching a tile, see if the tile is below
    //
    touching = instance_place(x, y+(global.size/2)+1, obj_tile);
}
This is the code that sets global.size: (Code Block 2)
Code:
global.scale = 1.5;
global.size = 64 * global.scale;
This is is in the collision_tile event of obj_tile: (Code Block 1)
Code:
touching = true;
So I uncommented everything. Now If I comment out only Code Block 1 the problem goes away.
I believe the issue is right here in Code Block 1.
The tiles are 64 pixel squared with auto hitboxes and an origin at 32, 32.


****Update****
@FrostyCat pointed out that I should print out touching.
When I did I found that I was storing instance IDs in touching, not true or false.
So a quick adjustment fixed it.

Code:
if(touching) { // If touching a tile, see if the tile is below
    if(instance_place(x, y+(global.size/2)+1, obj_tile) != -1) {
        touching = true;
    }
}
Thanks everyone for looking and advising.
 
Last edited by a moderator:

FrostyCat

Redemption Seeker
When I did I found that I was storing instance IDs in touching, not true or false.
So a quick adjustment fixed it.

Code:
if(touching) { // If touching a tile, see if the tile is below
if(instance_place(x, y+(global.size/2)+1, obj_tile) != -1) {
touching = true;
}
}
instance_place() never returns -1, it returns noone (-4) for no collision. You're not home yet.

Please, learn the difference between collision functions, particularly the big 4: place_meeting(), position_meeting(), instance_place(), instance_position()
 
C

Cupid Stunt

Guest
instance_place() never returns -1, it returns noone (-4) for no collision. You're not home yet.

Please, learn the difference between collision functions, particularly the big 4: place_meeting(), position_meeting(), instance_place(), instance_position()
Code:
if(touching) { // If touching a tile, see if the tile is below
    if(instance_place(x, y+2, obj_tile) == noone) {
        touching = false;
    } else {
        touching = true;
    }
    //show_debug_message(string(touching));
    //image_index = touching;
}
That finished the fix. Thanks a million.
Also edited my previous message to give proper credit.
 
Top