Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: owain86 on Fri 08/07/2011 17:31:46

Title: Trouble making object = visible when using inventory item on character
Post by: owain86 on Fri 08/07/2011 17:31:46
Hi,

I have a non-player character (a cat). I'd like cat food to appear on the floor once I have used the box of cat food from the inventory on the cat. I think the trouble I'm having is that interactions with the other character are scripted in the 'global script' whereas so far I've only been making objects visible/not visible in room scripts? My Global script is below;
}

function cCat_UseInv()
{
  if (cCharlie.ActiveInventory == iCatFood){
    cCharlie.Say("This should distract the cat!");
    cCharlie.Walk(235, 275, eBlock, eWalkableAreas);
    cCat.Walk(96, 350, eBlock, eAnywhere);
    oFood.Visible = true;
    }
}

The error message says: Undefined token 'oFood'

My object has a script name of 'oFood' and is in room 3 of my game.
Thanks,
Owain
Title: Re: Trouble making object = visible when using inventory item on character
Post by: Matti on Fri 08/07/2011 18:21:30
You can't use object names in the global script, instead you have to use their number.

object[1].Visible = true;
Title: Re: Trouble making object = visible when using inventory item on character
Post by: monkey0506 on Fri 08/07/2011 21:49:31
As matti said, object script names can only be used in the room scripts, but you can use the global object array with the object's ID. The reason for that is that objects only exist inside of the room to which they belong. AGS does not load any memory for any of your rooms until you go into that room in the game, and when you leave, it releases that memory. So, outside of room 3, your oFood object doesn't exist at all. Because of this, the global script can't reference the object script names.

You may also want to get in the habit of checking the player's room as well. Even if you don't necessarily need it for this particular interaction, it could lead to bugs in future situations. So:

function cCat_UseInv()
{
  if (cCharlie.ActiveInventory == iCatFood){
    cCharlie.Say("This should distract the cat!");
    cCharlie.Walk(235, 275, eBlock, eWalkableAreas);
    cCat.Walk(96, 350, eBlock, eAnywhere);
    if (player.Room == 3) object[1].Visible = true; // make sure the player is in the right room, then turn the object on
    }
}


Just make sure you replace the 1 with the appropriate ID of your object.