############################################################################################
ERROR in action number 1
of Step Event0 for object objBox:
Variable <unknown_object>.sprite_get_height(100102, -2147483648) not set before reading it.
at gml_Object_objBox_Step_0 (line 6) - var offsetY = -player.sprite_get_height(player.sprite_index) / 2; // Adjust vertically
############################################################################################
gml_Object_objBox_Step_0 (line 6)
gml_Object_objBurn_Step_0 (line 1)
//Create Event of objBox
isHeld = false; // Indicates if the box is being carried
heldBy = noone; // Reference to the player instance holding the box
//Step Event of objBox
if (isHeld && instance_exists(heldBy)) {
var player = heldBy;
// Define offsets based on player's direction
var offsetX = player.image_xscale * 40; // 40 pixels to the side
var offsetY = -player.sprite_get_height(player.sprite_index) / 2; // Adjust vertically
// Smoothly follow the player (optional)
x = lerp(x, player.x + offsetX, 0.5);
y = lerp(y, player.y + offsetY, 0.5);
}
// part of Create Event of player object
carryingBox = false;
heldBox = noone;
//part of playerStep
if (keyboard_check_pressed(ord("E"))) {
if (!carryingBox) {
// Attempt to pick up a box
var box = instance_nearest(x, y, objBox);
var pickupRange = 32;
code
if (box != noone && point_distance(x, y, box.x, box.y) <= pickupRange && !box.isHeld) {
carryingBox = true;
heldBox = box;
box.isHeld = true;
box.heldBy = id;
}
} else {
// Attempt to put down the box
if (heldBox != noone) {
var dropX = x + image_xscale * 40;
var dropY = y - sprite_get_height(sprite_index) / 2;
// Check for collision at drop position
if (!place_meeting(dropX, dropY, objSolid) && !place_meeting(dropX, dropY, objTopSolid)) {
// Place the box
heldBox.x = dropX;
heldBox.y = dropY;
heldBox.isHeld = false;
heldBox.heldBy = noone;
// Update carrying state
carryingBox = false;
heldBox = noone;
}
}
}
Assistant
}
The error you're encountering stems from an incorrect usage of the sprite_get_height function in your objBox object's Step Event. Specifically, you're attempting to call sprite_get_height as if it were a method of the player object, which it isn't. Let's break down the issue and provide a solution.
Error Explanation
Error Message:
code
ERROR in action number 1
of Step Event0 for object objBox:
Variable <unknown_object>.sprite_get_height(100102, -2147483648) not set before reading it.
at gml_Object_objBox_Step_0 (line 6) - var offsetY = -player.sprite_get_height(player.sprite_index) / 2; // Adjust vertically
Problematic Line:
gml
var offsetY = -player.sprite_get_height(player.sprite_index) / 2; // Adjust vertically
Here, sprite_get_height is being incorrectly called as a method of the player object (player.sprite_get_height). In GameMaker Language (GML), sprite_get_height is a global function that should be called directly, not through an instance or object.
Solution
To fix the error, you should call sprite_get_height as a standalone function, passing the sprite index of the player as its argument. Here's how you can modify your code:
Corrected objBox Step Event:
gml
// Step Event of objBox
if (isHeld && instance_exists(heldBy)) {
var player = heldBy;
// Define offsets based on player's direction
var offsetX = player.image_xscale * 40; // 40 pixels to the side
var offsetY = -sprite_get_height(player.sprite_index) / 2; // Adjust vertically
// Smoothly follow the player (optional)
x = lerp(x, player.x + offsetX, 0.5);
y = lerp(y, player.y + offsetY, 0.5);
}
By removing player. and calling sprite_get_height directly, you ensure that GML correctly interprets the function call, avoiding the error.
Additional Recommendations
Consistency in Function Calls:
Ensure that all instances where you retrieve sprite properties use the correct global functions. For example, if you need the width of a sprite, use sprite_get_width(sprite_index) instead of trying to call it through an object.
Error Handling:
Consider adding checks to ensure that player.sprite_index is valid before calling sprite_get_height. This can prevent potential runtime errors if the player lacks a sprite.
Code Optimization:
If you're frequently accessing the player's sprite height, you might want to store it in a variable within the player object to reduce repeated function calls.
Final Revised Code Snippets
Create Event of objBox:
gml
isHeld = false; // Indicates if the box is being carried
heldBy = noone; // Reference to the player instance holding the box
Step Event of objBox:
gml
if (isHeld && instance_exists(heldBy)) {
var player = heldBy;
// Define offsets based on player's direction
var offsetX = player.image_xscale * 40; // 40 pixels to the side
var offsetY = -sprite_get_height(player.sprite_index) / 2; // Adjust vertically
// Smoothly follow the player (optional)
x = lerp(x, player.x + offsetX, 0.5);
y = lerp(y, player.y + offsetY, 0.5);
}
Create Event of Player Object:
gml
carryingBox = false;
heldBox = noone;
Step Event of Player Object (playerStep):
gml
if (keyboard_check_pressed(ord("E"))) {
if (!carryingBox) {
// Attempt to pick up a box
var box = instance_nearest(x, y, objBox);
var pickupRange = 32;
if (box != noone && point_distance(x, y, box.x, box.y) <= pickupRange && !box.isHeld) {
carryingBox = true;
heldBox = box;
box.isHeld = true;
box.heldBy = id;
}
} else {
// Attempt to put down the box
if (heldBox != noone) {
var dropX = x + image_xscale * 40;
var dropY = y - sprite_get_height(sprite_index) / 2;
// Check for collision at drop position
if (!place_meeting(dropX, dropY, objSolid) && !place_meeting(dropX, dropY, objTopSolid)) {
// Place the box
heldBox.x = dropX;
heldBox.y = dropY;
heldBox.isHeld = false;
heldBox.heldBy = noone;
// Update carrying state
carryingBox = false;
heldBox = noone;
}
}
}
}
By implementing these changes, your game should handle the box-picking mechanics without encountering the previously mentioned error. Ensure you test the interactions thoroughly to confirm everything works as expected.
Summary:
Replace player.sprite_get_height(player.sprite_index) with sprite_get_height(player.sprite_index) since sprite_get_height is a global function, not a method of the player object.