SOLVED Basic position switch mechanic not working

marbar

Member
Hi everybody,

In my game, I need to switch the position of two objects when a certain condition is met. The code below (from the step event of the object being switched) works most of the time, but sometimes doesn't. I can't figure out the reason for this inconsistency, but I think it has something to do with accessing the position value of the other object, as when I set the position values to arbitrary numbers it works every time. pos4 and pos5 are variables storing the id's of the objects I'm trying to switch with. Anyone have any ideas where my mistake could be?

GML:
if (abs(pos7.x-x) >= gapw/2){
                //set temporary switch varaibles
                tempx = initialx;
                tempy = initialy;
                if pos7.x-x >= 0{
                    //switch positions with one object
                    initialx = pos5.initialx;
                    initialy = pos5.initialy;
                    pos5.initialx = tempx;
                    pos5.initialy = tempy;
                 
                } else {
                    //switch positions with other object
                    initialx = pos4.initialx;
                    initialy = pos4.initialy;
                    pos4.initialx = tempx;
                    pos4.initialy = tempy;
                }

        }
 
Last edited:

chirpy

Member
When are you applying swapped (initialx, initialy) to (x,y) ?
It seems that there may be something between them, as you compared pos7.x with x, but then only swapped (initialx, initialy) with some other thing.
 

marbar

Member
When are you applying swapped (initialx, initialy) to (x,y) ?
It seems that there may be something between them, as you compared pos7.x with x, but then only swapped (initialx, initialy) with some other thing.
I actually swap the positions when the player stops dragging the object. This code is at the top of the objects step event:

GML:
if global.drag == false{
    //if player stops dragging object, switch position
    x = initialx;
    y = initialy;

}
The drag part works correctly, and the object goes back to the initial positions, but the initial positions aren't always swapped as they should be
 

marbar

Member
Is it possibly swapping them every step, and it's a coin flip whether it happens an odd or even number of times?
You got it thanks so much! Added a quick code update to check for that and now it works like a charm.
GML:
if (abs(pos7.x-x) >= gapw/2){
                if hasSwitched = false{ //check if already switched
                    //set temporary switch varaibles
                    tempx = initialx;
                    tempy = initialy;
                    if pos7.x-x >= 0{
                        //switch positions
                        initialx = pos5.initialx;
                        initialy = pos5.initialy;
                        pos5.initialx = tempx;
                        pos5.initialy = tempy;
                    
                    } else {
                        //switch positions
                        initialx = pos4.initialx;
                        initialy = pos4.initialy;
                        pos4.initialx = tempx;
                        pos4.initialy = tempy;
                    }
                }
                hasSwitched = true;
        }
 
Top