Using an item on a character

Started by Shaneaphone, Wed 28/06/2023 17:45:19

Previous topic - Next topic

Shaneaphone

Ok So I've spent a good hour searching and reading comments. Found some code and commands that I thought should make sense... but it doesn't work.

I'm trying to use an inventory item ON a character to generate a bit of speech and give back an item etc.

This is what I have:

Code: ags
function cHonkey_UseInv()
{
if (player.ActiveInventory == iCnana)
}
{
  player.Say("Have a cursed nana my friend");
  cHonkey.Say("Ah thanks..."); 
  cHonkey.Say ("Take this I was saving it for my master...");
}
else
{
  cHonkey.Say("No, I don't want that.");
}
{

Seems to have issue with the 'if' but alas I cannot figure out why :(

Nahuel

#1
I think it seems to be that you are closing the function  :undecided:

original:
Code: ags
function cHonkey_UseInv()
{ // open
  if (player.ActiveInventory == iCnana)
} // close
  { // open?
    player.Say("Have a cursed nana my friend");
    cHonkey.Say("Ah thanks..."); 
    cHonkey.Say ("Take this I was saving it for my master...");
  } // close
  else
  { // open
    cHonkey.Say("No, I don't want that.");
  } //close
{ // open?

new code:

Code: ags
function cHonkey_UseInv()
{
  if (player.ActiveInventory == iCnana)
  {
    player.Say("Have a cursed nana my friend");
    cHonkey.Say("Ah thanks..."); 
    cHonkey.Say ("Take this I was saving it for my master...");
    player.LoseInventory(iCnana);  // This was added to remove the inventoryItem
  }
  else
  {
    cHonkey.Say("No, I don't want that.");
  }
}

I would also recommend you to remove "lose" the item if your character is giving it to some-one or using it.

EDIT: Added the original code.
Life isn't a game. Let's develop a life-like-game.

Shaneaphone

Thank you so much. As you can tell I am pretty rough on the coding.

Yes I had intention to make it disappear and add more dialogue/movement.

The iteration I posted was after several deletes and just wanted to communicate the issue itself.

Thanks again!

Khris

#3
The original code should've thrown a bunch of compiler errors.

{ opens a block, } closes it again (a block is a bunch of statements / commands).

This is true for functions and for the commands that run conditionally based on if conditions or else cases.
When you put if blocks inside functions, you're going to open a 2nd block using a 2nd { inside the outer one, then as you approach the end of the function you close them again, in order. It basically works exactly like the nested parens in mathematical expressions.

You can keep track of the "level" by indenting each block (AGS even does this for you whenever you press enter to end a line). Nested example using 4 spaces to make it more prominent:

Code: ags
function CommentOnWeather()
{
    if (itsRaining)
    {
        player.Say("I'm going to need an umbrella.");
        if (player.HasInventory(iUmbrella))
        {
            player.Say("Luckily I found one earlier.");
        }
        else
        {
            player.Say("But where am I going to get one?");
        }
    }
    else // not raining
    {
        player.Say("I love the sun.");
    }
}


Once you can do this in your sleep, you can try different styles. Many people for instance do not put the opening brace in the next line, and if there's only one command inside an inner block, you can omit the braces. I'd write the above code like this:
Spoiler
Code: ags
function CommentOnWeather() {
    if (itsRaining) {
        player.Say("I'm going to need an umbrella.");
        if (player.HasInventory(iUmbrella)) player.Say("Luckily I found one earlier.");
        else player.Say("But where am I going to get one?");
    }
    // not raining
    else player.Say("I love the sun.");
}
[close]

SMF spam blocked by CleanTalk