Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: dbuske on Tue 31/07/2012 13:00:26

Title: If else-- solved
Post by: dbuske on Tue 31/07/2012 13:00:26



if(cMarvin.ActiveInventory==iBook) {
    Display(".");
}
    else {
        Display(".");
    }
}

The above does not work.  Each character has an inventory item he recieves to calm down. A wrong item gets a different response.
They should give the else response. They always respond with the if response.
Title: Re: If else
Post by: Crimson Wizard on Tue 31/07/2012 13:11:32
But you have SAME command both under "if" and under "else"...
Title: Re: If else
Post by: dbuske on Tue 31/07/2012 13:17:00
Quote from: Crimson Wizard on Tue 31/07/2012 13:11:32
But you have SAME command both under "if" and under "else"...
???
Title: Re: If else
Post by: Crimson Wizard on Tue 31/07/2012 13:19:58
Here's the code you posted:
Code (ags) Select

if(cMarvin.ActiveInventory==iBook) {
    Display(".");
}
    else {
        Display(".");
    }

This means:
IF Marvin's active inventory is Book, then Display(".");
ELSE Display(".");

It will do Display(".") always.
Is this really what you have in your code, or you just removed the actual text?
Title: Re: If else
Post by: dbuske on Tue 31/07/2012 13:23:44
Yes I removed the text.  The else response is a negative statement that tells the player they gave the wrong item.
Preceding this I made the iBook the activeinventory item.
Title: Re: If else
Post by: Crimson Wizard on Tue 31/07/2012 13:32:19
I do not see how this can work wrong from the code you posted.

Maybe you can show all the code from this function? By the way, what function is this - "use inventory on character" event handler?

EDIT: Also, is cMarvin a player character?
Title: Re: If else
Post by: dbuske on Tue 31/07/2012 13:45:28
function cMarvin_UseInv()
{
cMarvin.AddInventory(iBook);
cMarvin.ActiveInventory = (iBook);
cNano.LoseInventory(iBook);
  if(cMarvin.ActiveInventory==iBook)
      {
      Display("Finally, someone who appreciates how truly awful life really is.  I think I'll shut myself down now, or stick my head in a bucket of water for a while.");
      }
        else
    { 
      Display("That's even more depressing.");
    }
}

This is in the global script.
Good point about the Use inventory on character function.  I will check that out. I just used Useinv.
Title: Re: If else
Post by: Crimson Wizard on Tue 31/07/2012 13:51:19
Well, it is all clear now.
Look at your code. What do you do?
1. Set Marvin's active inventory to iBook.
2. Check if Marvin's active inventory is iBook.
Ofcourse it will always be iBook, and it will always display the text under "if", and never under "else".

Try this instead:
Code (ags) Select

function cMarvin_UseInv()
{
  if(cNano.ActiveInventory==iBook)
  {
    Display("Finally, someone who appreciates how truly awful life really is.  I think I'll shut myself down now, or stick my head in a bucket of water for a while.");
    cMarvin.AddInventory(iBook);
    cNano.LoseInventory(iBook);
  }
  else
  { 
    Display("That's even more depressing.");
  }
}


Quote from: dbuske
Good point about the Use inventory on character function.  I will check that out. I just used Useinv.
Well, that seems to be exactly the same.
Title: Re: If else
Post by: dbuske on Tue 31/07/2012 14:04:28
Thank You, I will try it out right now.
Title: Re: If else
Post by: NickyNyce on Tue 31/07/2012 14:16:02
you were attempting to have the character without the book USE the book on himself. Crimson Wizard should have made things right for you now.

You don't need the ...cMartin.ActiveInventory=(iBook);

From the looks of it, you were doing this so that your code would work, and the only reason it kind of did was because you were setting cMartins ActiveInventory to the book before the IF statement. It should be Nano giving the book to cMartin like Crimson Wizard drew up for you. Also the reason why it kind of worked was because you gave cMartin the Book before the IF statement ran.

Title: Re: If else
Post by: dbuske on Wed 01/08/2012 15:08:08
Thanks, I will work on it again today.
Title: Re: If else
Post by: dbuske on Wed 01/08/2012 18:49:50
The code for Nano giving the inv item is in the room script.
Title: Re: If else
Post by: Crimson Wizard on Wed 01/08/2012 19:41:51
Quote from: dbuske on Wed 01/08/2012 18:49:50
The code for Nano giving the inv item is in the room script.
Do you mean you just wrote a function with "Useinv" postfix without actually binding it to event? That's not correct then.
Also, it really depends on what actually Marvin is. If it is a room object or hotspot, then event function should stay in the room. If it is a character, it must be put into GlobalScript.
Title: Re: If else
Post by: dbuske on Wed 01/08/2012 21:21:35
Quote from: Crimson Wizard on Wed 01/08/2012 19:41:51
Quote from: dbuske on Wed 01/08/2012 18:49:50
The code for Nano giving the inv item is in the room script.
Do you mean you just wrote a function with "Useinv" postfix without actually binding it to event? That's not correct then.
Also, it really depends on what actually Marvin is. If it is a room object or hotspot, then event function should stay in the room. If it is a character, it must be put into GlobalScript.
ing  room script
Nano is the main character. So, I should put Nano's giving the inv item with the if else in the global script?  The giving the item is scripted as an event in the room script.
Title: Re: If else
Post by: NickyNyce on Wed 01/08/2012 21:36:18
Like Crimson Wizard said, all this should be in cMartins UseInv function. Which will be located in the global script when you make the function.

if(cNano.ActiveInventory==iBook)
  {
     Display("Finally, someone who appreciates how truly awful life really is.  I think I'll shut myself down now, or stick my head in a bucket of water for a while.");
     cMarvin.AddInventory(iBook);
     cNano.LoseInventory(iBook);
  }
  else
  { 
     Display("That's even more depressing.");
  }   
}
Title: Re: If else
Post by: Crimson Wizard on Wed 01/08/2012 21:47:36
Quote from: dbuske on Wed 01/08/2012 21:21:35
Nano is the main character. So, I should put Nano's giving the inv item with the if else in the global script?  The giving the item is scripted as an event in the room script.
I think you are missing the point a little. The question is not about Nano, but about the one to WHOM it gives an item.
To put it simply: it depends on what object does player interact with.

When player interacts with Characters (for example: gives inventory) and Inventory Items (for example: looks at them), the event should be scripted in the GlobalScript.
When player interacts with room Objects and Hotspots, the event should be scripted in the Room script.

So, yes, you should put the event with giving an item to the GlobalScript.

Also - that's very important! - do not forget to actually bind your function with event. Simply naming it with "Useinv" won't work.
You should set the name of your function for "Use inventory on character" event in character properties. Not player character (Nano), but NPC character (Marvin).