(Solved) Checking Object States in If/else statements.

Started by Dannymac247, Wed 27/05/2015 23:51:58

Previous topic - Next topic

Dannymac247

So I am working on a puzzle where multiple objects in a room are used to create a rat trap. Since these objects could theoretically be found/placed in any order, I figured a simple if/else statement could do the trick, and I used the following code to do it. By my understanding of the code, the following script should check if room objects 5 and 6 are visible, and then, if they are, make them invisible and activate object 7 along with removing the trap (a stick and a box, hence isnb) and displaying a message about the trap being set. If they are not both activated, then it should return a message saying that the trap needs a trigger and a bait.

It is always returning a negative. Any obvious thoughts on what I am doing wrong here? I was worried that I was using the wrong function, but I can't find another one in the manual.

Code: ags
if ((object[5].Visible == true) && (object[6].Visible == true))
{
object[7].Visible= true;
object[5].Visible= false;
object[6].Visible= false;
cEgo.LoseInventory(isnb);
		Display ("You set the trap.");
}
else {
		Display ("The trap will need a trigger and bait, first.");



Khris

In which function did you put that code? Are you using inv items with a "floor" hotspot or something along those lines?

Also, the first line will work like this:  if (object[5].Visible && object[6].Visible)  because .Visible is already a boolean value.
And you can use object names in rooms, the object[] array is intended for global scripts.

Dannymac247

Thanks for the quick reply! There is a hot spot set on the floor of the room, and this script is triggered when a certain inventory item is used on it.  (Interestingly, now I am continuing to play with it, it is ALSO displaying the negative message when other items are being used on the floor, even though it is under a conditional item.)

So, the "true" values are my problem, you think?

Khris

#3
Here's how I'd script this:

Code: ags
function CheckTrap(String message) {
  if (oBait.Visible && !oTrigger.Visible) {
    Display ("%s but the trap still needs a trigger.", message);
    return; // not done yet
  }
  if (oTrigger.Visible && !oBait.Visible) {
    Display ("%s but the trap still needs some bait.", message);
    return; // not done yet
  }
  // at this point, both are visible
  oBait.Visible = false;
  oTrigger.Visible = false;
  oTrap.Visible = true;
  Display ("%s finishing the trap.", message);
}

function hFloor_UseInv() {
  if (player.ActiveInventory == iBait) {
    player.LoseInventory(iBait);
    oBait.Visible = true;
    CheckTrap("You place the bait,");
  }
  else if (player.ActiveInventory == iTrigger) {
    player.LoseInventory(iTrigger);
    oTrigger.Visible = true;
    CheckTrap("You place the trigger,");
  }
  else Display("That's not going to work.");
}


Edit: changed Useinv to UseInv

Dannymac247

Still not working. Same problem... even with the Bait and Trigger visible, it is still returning the negative message.

NickyNyce

#5
The code you posted doesn't show that you actually turned either of those objects to true before you checked them. I'm assuming somewhere above the IF statement you're doing this, right?

Showing everything that you're doing would make it easier to see what's going wrong.

Khris

I tested my code as-is and it worked perfectly fine.

Dannymac247

The objects in question are activated earlier when they are placed on the floor. So they ARE active. Khris' script did one better than mine and at least recognized when the bait was placed.

Sigh. I've worked around the problem by working it solely on the inventory screen, but that is clunky and less intuitive for the player. Could version be a problem here? I'm using AGS 2.72.

Khris

Could you post all relevant code, the pieces that failed?

While using 2.72 shouldn't have anything to do with this issue, it is recommended to use the current version.

Dannymac247

Most of what went before was done through 2.72's drop menus, so I'm not dead sure how to show the code.

I also copied your code nearly verbatim off of the page, except I corrected the exclamation points before o.Bait and o.Trigger. Do they belong there? I put in the parenthesis the manual said would be needed for that phrasing.

Gilbert

If you are using the Interaction Editor you may post a screenshot here.

Snarky

Quote from: Dannymac247 on Fri 29/05/2015 06:03:35
I also copied your code nearly verbatim off of the page, except I corrected the exclamation points before o.Bait and o.Trigger. Do they belong there?

Changing symbols in a code snippet because you don't understand them isn't exactly what I would call "correcting" them. Yes, they belong there! (And I have to ask: Why didn't you test whether the code worked before changing anything in it?)

In AGS script (like many other programming languages), "!" is the negation symbol: it checks that the bit that follows is NOT true. (And similarly, "!=" means "not equal".) So if (oBait.Visible && !oTrigger.Visible) should be read as "IF oBait visible AND NOT oTrigger visible" (or in more natural English: "If oBait is visible and oTrigger is not visible [then do the following...]"). By deleting the NOT, you change the whole meaning of the test, and of course it won't work.

QuoteI put in the parenthesis the manual said would be needed for that phrasing.

Khris's code was fine as-is, it didn't need any more parentheses.

Dannymac247

Because it looked like a typo. That code looks nothing like what the manual was saying to use. Sigh. That's also why the one worked and the other didn't... I only saw the one exclamation point. Oh well, dummy moment over. Thanks, guys... it seems to work now.

Khris

You "only saw the one exclamation point", yet earlier you "corrected the exclamation points before o.Bait and o.Trigger"? :-\


SMF spam blocked by CleanTalk