Colobot Wiki
Register
Advertisement

Syntax:[]

int grab(int position)

Description:[]

The grab function when executed on a Grabber or Subber will attempt to pick up an object in the specified position relative to the bot. Predefined position's are:

  • 'InFront' Directly ahead (default)
  • 'Behind' Directly behind
  • 'EnergyCell' The bot's own power cell

This function does not require exact positioning of the executing bot when calling grab(InFront), as long as there is a valid item nearby, the bot will maneuver in order to pick it up (see bugs).

When grab(InFront) is executed on a grabber facing the back of a bot or building with an installed energy cell it will attempt to pick up the installed cell.

The Subber can only use the default 'InFront' position and can only pick up items lying on the ground.

Dispite causing animation, grab() can be performed without energy as long as it does not cause the bot to turn or move forwards or backwards.

Return Value:[]

With errmode(0) grab() should return 0 for success and non-zero for failure (see bugs). With errmode(1) grab() should return 0 for success and error out on failure (see bugs).

Bugs:[]

Occasionally, usually when the bot must turn or move in order to execute a grab(), the grab() function will appear to succeed, but not actually grab anything.

If two different items are located quite near each other, it is also possible that grab() will pick up the wrong item.

The workaround is to wrap grab() in a custom function that does error checking:

int gwrap( object item )
{
    errmode(0);
    float d = distance( position, item.position );
    if( d > 3 )
    {
        if( goto( item.position ) != 0 )
        {
            return 1;

        }
    }
    else if( d > 1.5 )
    {
        turn( direction( item.position ));
        move(d - 1.5);
    }
    if( grab() != 0 or load == null or load != item )
    {
        return 2;
    }
    return 0;
}

This function attempts to maneuver to the item, avoiding goto for nearby items (which can trigger a bug). It returns 1 if it cannot reach the item, 2 if it failed to grab the item or 0 upon success.

Example:[]

Swap the bot's current energy cell for an energy cell located directly in front of the bot:

    grab();
    drop(Behind);
    grab(EnergyCell);
    drop();
    grab(Behind);
    drop(EnergyCell);

See also.[]

CBOT Language, Drop

Advertisement