How to make a character carry an object around a room (Solved!)

Started by pietakio, Mon 10/09/2012 04:33:10

Previous topic - Next topic

pietakio

Say we have a room object. I know how to make my character bend down to "pick" it up and put it into inventory. But what if I want her to visually pick up the object and have it appear as if she's holding it as she strolls around the room? In other words, the object is following a point on the character as the character moves and the object is moving with the character. I've played with this for hours but can't find a way to make it work in script. Will I just have to make new alternative character views with her holding each object that I'd like to use this way? I'd like to do it in script if possible. Would so appreciate some help. Thanks!!!

Igor Hardy

You need to create the object as a character in the script. Then you can simply use the follow function which is native to character game objects. In general using characters for room objects allows much more flexibility in the script.

Khris

Not really, since the character is supposed to carry the object. Though one could switch the character object's view to one where the sprites are offset so it matches the player's hand's position.

The other way:
At the top of the room script, add
Code: ags
bool object_carried;

void repeatedly_execute_always() {
  if (!object_carried) return;

  // set object here
  Object* o = oPackage;

  int x, y, l = player.Loop;
  // down
  if (l == 0) { x = -7; y = -30; o.Baseline = player.Baseline + 1; }
  // left
  if (l == 1) { x = -14; y = -35; }
  ...

  o.SetPosition(player.x + x, player.y + y);


The x and y in the code refer to the object's lower left corner in relation to the character's pivot (bottom center).

As soon as the object is picked up, call
Code: ags
  object_carried = true;

pietakio

Hey!

Thanks for the super comprehensive help. The code worked like a charm, with a few minor modifications due to my newbie-ness (I got confused and dumbed it down, hopefully in an OK way). I'll share it here in case another newbie ever wants to pick up an object and carry it around:

Say your object is called oObject. I set it up so that the object is picked up when the player interacts with it and put down if the player interacts with it while it's being carrried.   

In the room script header define four variables:

**************
 
  bool carry_object=false;   //tells the program whether or not to carry the object
  int x;                    //the x-coordinate of the player character's hand
  int y;                    //the y-coordinate of the player character's hand
  int L;                    //the number of the loop -- hand position changes with the walk loop so we need to set up x and y dependent on the loop

************

When the player interacts with the object:

****************

//if the object is on the ground...and neglecting to write in animations that make it look somewhat realistic

if (carry_object==false)
   
    {
       carry_object=true;

//if you have scaling in the room you want the object to scale with the character:

       oObject.IgnoreScaling=false;
     
    }
   
//But if you're already carrying the object, put it down

    else if (carry_object==true)
   
    {
      oObject.SetPosition(player.x, player.y);
      carry_object=false;
     
    }

******************
In room's repeatedly execute function try (Note: I found setting the object baseline to +1 the player baseline as Kris wrote made the object behave funny with respect to other room objects, having it -1 the player baseline worked..who knows!):

  L=player.Loop;

  if (carry_object==true)
{
// set hand position for down loop...

   if (L == 0) { x = 35; y = -80; oWateringCan.Baseline = player.Baseline -1; }

// for left loop...

   else if (L == 1) { x = 0; y = -105; oWateringCan.Baseline = player.Baseline -1;}

// for right loop...

    else if (L==2) {x=0; y=-105; oWateringCan.Baseline = player.Baseline -1;}

//up...

   else if (L==3) {x=-55; y=-80; oWateringCan.Baseline = player.Baseline -1;}

// keep going if you have more loops  ....
   
oWateringCan.SetPosition(player.x + x, player.y + y);

}

***************

Probably some mistakes in there but it works for me.

Thanks again for the help!


SMF spam blocked by CleanTalk