drop an enemy after picking up?

ChrisC

Member
I made this to pickup an enemy but i cant figure out how to get him to put the enemy down.

create event of player
GML:
grabenemy = false;
grabbed_id = -1
step event of the player
GML:
nearestEnemy = instance_nearest(x, y + z, oParEnemy);

if (kGrab and onGround)
and distance_to_object(nearestOpponent) < GrabRange
and (state = RUN or state = IDLE)
and nearestEnemy.weight <= max_pickup_weight
and grabenemy = false
{
grabenemy = true;
nearestEnemy.pickedup = true;
hit = true;  //hit only true to keep player from moving.
alarm[0] = 30;
}
GrabScript();
grab script
GML:
if grabenemy == true
{
    if( grabbed_id == -1 )
    {
    grabbed_id = instance_nearest(x, y + z, oParEnemy);
    }
}
else
{
    if( grabbed_id != -1 ){
        instance_destroy(grabbed_id);
        grabbed_id = -1;
    }
}
if( grabbed_id != -1 ) and instance_exists(grabbed_id)
{
    grabbed_id.x = x+25;
    grabbed_id.y = y-10;
    grabbed_id.z = z-15;
    depth = other.depth - 1;
}
else
{grabenemy = false
}
enemies create event
GML:
pickedup = false;
enemies step event
GML:
if pickedup == true and state != DEAD
{state = PICKEDUP;}
else
{pickedup = false;}

 case PICKEDUP:
{}
break;
 

ChrisC

Member
this is what i was trying to do but it doesnt work
GML:
if kGrab and grabenemy == true
{
grabenemy = false;
other.pickedup = false;
other.state = IDLE;
grabbed_id = -1;
}
 
If your enemy drop code is running before your enemy pick up code, it looks like the moment you drop the enemy, it will be picked up again, because kGrab will be true and grabenemy will be false.
 

ChrisC

Member
If your enemy drop code is running before your enemy pick up code, it looks like the moment you drop the enemy, it will be picked up again, because kGrab will be true and grabenemy will be false.
I tried this to make sure, so it half way works now.

The enemy still gets stuck like they are being grabbed but they no longer stick to my players x and y values they just hover in one spot.

edit: the player never carries them in the first place either, so instead of going to player they just hover to clarify, so it broke one part that did work but now neither work i guess, hard to explain.
 
Last edited:

Mk.2

Member
Where are you changing its state back to whatever it was before "PICKEDUP"?

EDIT: Nevermind, I missed that in the last bit of code you posted. Can you post the IDLE state?
 
I would suggest drawing the values of all the relevant variables on screen (grabenemy etc...) so you can see exactly what is going on. Check that they are all being set/unset correctly. Also look into using the debugger and stepping through the code if you haven't already.
 

ChrisC

Member
I would suggest drawing the values of all the relevant variables on screen (grabenemy etc...) so you can see exactly what is going on. Check that they are all being set/unset correctly. Also look into using the debugger and stepping through the code if you haven't already.
I agree, but i dont know how to do any of those things.
 
Add a Draw GUI Event to your player. In the event, use the draw_text() function to draw the variables on screen. In the draw GUI event, the top left corner of the screen is simply 0,0 position-wise, so it's easy to fix the position of the text, and it draws on top of everything else, so its easy to see.
 

ChrisC

Member
Add a Draw GUI Event to your player. In the event, use the draw_text() function to draw the variables on screen. In the draw GUI event, the top left corner of the screen is simply 0,0 position-wise, so it's easy to fix the position of the text, and it draws on top of everything else, so its easy to see.
ok thanks, i have the values show up, but now i still have the same problem. How do i press the button again to drop the enemy?
 

ChrisC

Member
i tried to narrow it down so i changed the put down code to
GML:
if kGrab and grabenemy == true
{
grabenemy = false;

}
but becasue of this code the enemy just destroys
GML:
if grabenemy == true
{
    if( grabbed_id == -1 )
    { 
    grabbed_id = instance_nearest(x, y + z, oParEnemy);
    }
}
else
{
    if( grabbed_id != -1 ){
        instance_destroy(grabbed_id);
        grabbed_id = -1;
    }
}
 

ChrisC

Member
seems like its looping somehow, i put other ones in and the vaiables flash when i press button but dont change
 

ChrisC

Member
OKay so if i changed the button i release the grab with it does reset most of variables. the enemy stops following the player x and y but they get stuck floating in the air and their pickup variable still stays true
 

ChrisC

Member
i narrowed it down to 2 problems.

first the looping of the 1 button press,

and two

other.pickedup = false;
other.state = IDLE;

those dont seem to do anything to the enemies variables.
 

ChrisC

Member
Add a Draw GUI Event to your player. In the event, use the draw_text() function to draw the variables on screen. In the draw GUI event, the top left corner of the screen is simply 0,0 position-wise, so it's easy to fix the position of the text, and it draws on top of everything else, so its easy to see.
ok this code works but the button is looping so it only works if i use a different button that kGrab
GML:
if kGrab and grabenemy == true
{
with (oParEnemy)
{
pickedup = false;
state = IDLE;
}
grabenemy = false;
grabbed_id = -1;
}
 
Top