Problem with nested logic (SOLVED)

Started by That Guy, Mon 12/09/2005 10:21:50

Previous topic - Next topic

That Guy

I have an inventory object with an interaction defined for "Look at object" and nothing else.

However, calling IsInteractionAvailable() on that object with a parameter of eInteract is returning 1, as though an interaction was defined.

Is this a bug, or am I doing something wrong?

EDIT:  I noticed that if my IF/THEN logic got too complex, the final conditional didn't get evaluated no matter what, whether it used IsActionAvailable() or even GetProperty().

the logic went something like this;

if ((something) || ((one thing) && (another thing))

then (another thing) always returned true, no matter what the condition should have been.  did I get too complicated and break something in the parser?

strazer

No, AGS can handle much more complex things. It might help if you posted the actual code you use.

That Guy

It's the section of the SCUMM tutorial that handled running Use/Use with conditions.Ã,  Here's the code;

Code: ags

if ((StrComp(verb, vGive) != 0) || ((StrComp(verb, vUse) == 0) && (item.IsInteractionAvailable(eModeInteract) == 1)))
{
Ã,  Ã, item.runInteraction(mode);
}


Using that code, the runInteraction method always got called for "Use" verbs, whether the item had an Interact event defined or not, so I have to assume it was either getting thrown out of the parser or unconditionally returning true.  Just to make sure I wasn't going crazy, I replaced it with a custom property on the object, and tested that, like so:

Code: ags

if ((StrComp(verb, vGive) != 0) || ((StrComp(verb, vUse) == 0) && (item.GetProperty("shouldUse") == 1)))


And again, it always returned true regardless of the value of the "shouldUse" property.Ã,  It's like the parser never even tested the last part of the && condition.

I broke the condition up into multiple lines and it all works fine now, but I'm still a little confused.Ã,  ???

Kweepa

Quote from: That Guy on Mon 12/09/2005 19:28:20
Code: ags

if ((StrComp(verb, vGive) != 0) || ((StrComp(verb, vUse) == 0) && (item.IsInteractionAvailable(eModeInteract) == 1)))
{
   item.runInteraction(mode);
}

The first StrComp will return non-zero if verb != vGive, so the if statement will be true.
I think you probably meant
Code: ags

if ((StrComp(verb, vGive) == 0) || ((StrComp(verb, vUse) == 0) && (item.IsInteractionAvailable(eModeInteract) == 1)))
{
   item.runInteraction(mode);
}


God knows how many bugs strcmp has been the cause of. Starting a new C (or AGS v2.62) project, always write a StrEqual() function which returns true when two char[] strings are equal.
Still waiting for Purity of the Surf II

Pumaman

As Steve says, if verb is NOT vGive then the second half of the test won't be done and the interaction will be run.

With 2.71 beta, you can compare strings with the == operator to avoid this nastiness, so you could do:

if ((verb == vGive) || ((verb == vUse) && (item.IsInteractionAvailable(eModeInteract) == 1)))
{
   item.runInteraction(mode);
}

That Guy

QuoteAs Steve says, if verb is NOT vGive then the second half of the test won't be done and the interaction will be run.

True.Ã,  I see that now.Ã,  I really need to stop coding at 2am.Ã,  ;DÃ,  The condition made sense logically, but not *logically*.

The condition did in fact need to be fired for any verb other than vGive (so the NOT vGive was correct), but it shouldn't fire for vUse UNLESS the item had an Interact event.  So that seems like it should require an OR condition, because an AND condition would have thrown out any of the other verb combos other than vUse with an event.  I think.  Geesh.

Anyway, in case anyone was curious, I restructured the conditions to the following, which works just dandy;

Code: ags

// depending on the verb, we may want to run the interaction on this item
					
if ((StrComp(verb, vGive) != 0) && (StrComp(verb, vUse) != 0))
{
Ã,  Ã,  if (item != null)
Ã,  Ã,  Ã,  Ã, item.RunInteraction(mouse.Mode);
}
else
{
Ã,  Ã, if (StrComp(verb, vUse) == 0)
Ã,  Ã,  Ã,  if (item.IsInteractionAvailable(eModeInteract) == 1)
Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã, item.RunInteraction(mouse.Mode);
						
Ã,  Ã,  Ã,  Ã,  Ã, return;
Ã,  Ã,  Ã,  }
					
Ã,  Ã, cJustin.ActiveInventory = item;
				
Ã,  Ã, mouse.Mode = eModeUseinv;
}

SMF spam blocked by CleanTalk