___________________________________________
############################################################################################
ERROR in action number 1
of  Step Event0 for object objBox:
Variable <unknown_object>.xspeed(100041, -2147483648) not set before reading it.
 at gml_Object_objBox_Step_0 (line 12) -     xspeed = Object1.xspeed;
############################################################################################
gml_Object_objBox_Step_0 (line 12)
gml_Object_objBurn_Step_0 (line 1)


// --- Define gravity and other constants first ---
grav = 0.5;
gravWater = 0.2;

// --- Box Physics Variables ---
isCarried = false;
xspeed = 0;
yspeed = 0;
ground = false;
inWater = false;
bubbleTimer = 0;
canGravity = true;
currentGrav = grav;            // Now grav is initialized

// --- Define max vertical speed ---
maxVspeed = 15;

// Step Event of objBox

// If the box is being carried, position it relative to the player and skip physics
if (isCarried && instance_exists(Object1)) {
    // Define offset positions as needed
    var carry_offset_x = Object1.x + 20; // Example offset
    var carry_offset_y = Object1.y;      // Example offset
    x = carry_offset_x;
    y = carry_offset_y;
    
    // Optionally, set xspeed and yspeed to match player's movement
    xspeed = Object1.xspeed;
    yspeed = Object1.yspeed;
    
    // Skip further physics calculations while carried
    return;
}

// --- Floor Collision ---
var mySolid = instance_place(x, y + yspeed, objSolid);
if (mySolid != noone && yspeed >= 0) {
    var spriteOffsetY = sprite_get_yoffset(sprite_index);
    var spriteHeight = sprite_get_height(sprite_index);
    y = mySolid.bbox_top - (spriteHeight - spriteOffsetY);
    ground = true;
    yspeed = 0;
    
    // Ensure the box stays grounded
    while (!place_meeting(x, y + 1, objSolid)) {
        y += 1;
    }
}

// --- Wall Collision ---
mySolid = instance_place(x + xspeed, y, objSolid);
if (mySolid != noone && xspeed != 0) {
    var spriteOffsetX = sprite_get_xoffset(sprite_index);
    var spriteWidth = sprite_get_width(sprite_index);
    
    if (xspeed < 0) {
        x = mySolid.bbox_right + spriteOffsetX;
    } else {
        x = mySolid.bbox_left - (spriteWidth - spriteOffsetX);
    }
    
    // Force box out of the wall if clipping occurs
    while (place_meeting(x, y, objSolid)) {
        x += sign(xspeed) * -1;
    }
    
    xspeed = 0;
}

// --- Ceiling Collision ---
mySolid = instance_place(x, y + yspeed, objSolid);
if (mySolid != noone && yspeed < 0) {
    var spriteOffsetY = sprite_get_yoffset(sprite_index);
    y = mySolid.bbox_bottom + spriteOffsetY;
    
    // Force box out of the ceiling if clipping occurs
    while (place_meeting(x, y, mySolid)) {
        y += 1;
    }
    
    yspeed = 0;
}

// --- Platform (TopSolid) Collision ---
mySolid = instance_place(x, y + yspeed, objTopSolid);
if (mySolid != noone && yspeed >= 0) {
    if (bbox_bottom <= mySolid.bbox_top + yspeed) {  // Check if actually above the platform
        var spriteOffsetY = sprite_get_yoffset(sprite_index);
        var spriteHeight = sprite_get_height(sprite_index);
        y = mySolid.bbox_top - (spriteHeight - spriteOffsetY);
        ground = true;
        yspeed = 0;
    }
}

// --- Ensure Ground State and Yspeed Are Consistent ---
if (ground) {
    yspeed = 0;
    // Double-check ground state
    if (!place_meeting(x, y + 1, objSolid) && !place_meeting(x, y + 1, objTopSolid)) {
        ground = false;
    }
}

// --- Water Interaction ---
if (place_meeting(x, y, objWater) && !inWater) {
    inWater = true;
    var currentWater = instance_place(x, y, objWater);
    if (currentWater != noone) {
        instance_create_layer(x, currentWater.bbox_top + 1, "Instances", objSplash);
    }
}

if (inWater) {
    currentGrav = gravWater;
    
    bubbleTimer += 1;
    if (bubbleTimer >= 10) {
        bubbleTimer = 0;
        if (!instance_exists(objAirBubble)) {
            instance_create_layer(x, y, "Instances", objAirBubble);
        }
    }
} else {
    currentGrav = grav;
    bubbleTimer = 0;
}

// --- Leaving the Water ---
if (inWater) {
    var wtr = instance_place(x, y - yspeed, objWater);
    if (wtr != noone) {
        if (bbox_bottom < wtr.bbox_top) {
            instance_create_layer(x, wtr.bbox_top + 1, "Instances", objSplash);
            inWater = false;
        }
    }
}

// --- Gravity Application ---
if (!ground && canGravity) {
    yspeed += currentGrav;
    if (yspeed > maxVspeed) {
        yspeed = maxVspeed;
    }
}

// --- Apply Movement ---
x += xspeed;
y += yspeed;