Trouble making object = visible when using inventory item on character

Started by owain86, Fri 08/07/2011 17:31:46

Previous topic - Next topic

owain86

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

Matti

You can't use object names in the global script, instead you have to use their number.

object[1].Visible = true;

monkey0506

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:

Code: ags
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.

SMF spam blocked by CleanTalk