ElCheTibo
Member
I'm currently working on a game project that let the player carry two objects at once in his hands. It's a 2D platformer and I've already figured out most of the code by myself.
Everything is working pretty well but when I try to pick up two objects, the first object disappeared and is replaced with the second one...
A Reddit user suggested me to put the following lines in my code :
The majority of the code is running correctly but the "moving the objects to hands" is where the issue is.
When I try to run the project, the debug pop-up says :
I cannot think of another solution to place them in the player's hands, because that may be too advanced for my beginner level
Can you help me to fix these pesky bugs ? It's been bothering me for days now...
Everything is working pretty well but when I try to pick up two objects, the first object disappeared and is replaced with the second one...
A Reddit user suggested me to put the following lines in my code :
GML:
// CREATE EVENT
swords = [2];
nbOfSwords = 0;
// STEP EVENT
// Picking up swords
var _len = array_length(swords);
if pickUp // 'E' is pressed and the object is picked up
{
if _len < 2
{
var _pick_up_dist = 32;
var _nearest_dist = _pick_up_dist;
var _nearest_bottle = noone;
// Look for nearest sword to pick up
for(var i = 0; i < instance_number(oSword); i++) {
var _sword = instance_find(oSword, i);
show_debug_message("_sword typeof: " + typeof(_sword)); // These lines are just
show_debug_message("_sword: " + string(_sword)) // for debugging purposes
if _sword.pickedUp
{
continue;
}
var _dist = point_distance(x, y, _sword.x, _sword.y);
if _dist < _nearest_dist
{
_nearest_dist = _dist;
_nearest_sword = _sword;
}
}
// Found the nearest sword, add to swords[]
if _nearest_sword
{
swords[_len] = _nearest_sword;
_nearest_sword.pickedUp = true; // a bool in the Create event of oSword
}
}
}
// Moving picked up swords to hand positions
var _dist = 15;
for(var i = 0; i < _len; i++) {
_sword.x = x + _dist - (_dist * 2 * i); // x position
_sword.y = y + _dist; // y position
}
When I try to run the project, the debug pop-up says :
After some troubleshooting, I've noticed that the problem is the ".x" and ".y" that follows the "_sword".local variable <unknown built-in variable>(-1610512697, -2147483648) not set before reading it.
at gml_Object_oPlayer_Step_0 (line 81) - _sword.x = x + _dist - (_dist * 2 * i); // x position
I cannot think of another solution to place them in the player's hands, because that may be too advanced for my beginner level
Can you help me to fix these pesky bugs ? It's been bothering me for days now...
Last edited: