Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: cipberbloom on Thu 24/06/2021 00:38:38

Title: [SOLVED] Sequence keeps repeating even though incrementing should prevent it
Post by: cipberbloom on Thu 24/06/2021 00:38:38
Hullo, everyone!  ;-D

Ok, so here's what I'm trying to do:

In room 2 (a shelf with dolls), when you use an inventory item on a specific object, the character is meant to switch to room 1 (crypt), where the character animates for a few seconds and a sound is played. No problems there, that works great.

All of the counters in the bit of code from room 1 (crypt) below are global variables with the initial value of 0.


Code (ags) Select

  function room_AfterFadeIn()
{
    if (aliceCount >= 1 || lilaCount >=1 || tedCount >= 1 && brushCount == 0 && cMalcolm.ActiveInventory == iDinceyDollBrush)
  {
    cMalcolm.LoseInventory(iDinceyDollBrush);
    cMalcolm.Walk(705, 590, eBlock, eWalkableAreas);
    cMalcolm.SayBubble("bla bla bla");
    cMalcolm.LockView(21);
    cMalcolm.Animate(0, 5, eRepeat, eNoBlock, eForwards);
    aDollBrush.Play();
    Wait(160);
    cMalcolm.UnlockView(1);
    brushCount += 1;
    }
   sackOverlay = Overlay.CreateGraphical(1472, 743, 213, false);
}



But, every time he re-enters room 1 from any other room, the animation, sound, etc all happen again, even though the counter should no longer be set to 0. Is this something that ought to be in the global script rather than room 1 or is my code totally weird and wrong? If you have any other recommendations they are most welcome. I'm really enjoying this as a learning project, but sometimes find myself feeling more insane than usual.  8-0

Many thanks in advance!

PS -  I've also tried it with:
Code (ags) Select
brushCount += 1;
within it's own brackets/printers' braces/curly braces thusly:
Code (ags) Select

{
brushCount += 1;
}


Again, thanks.  :)



Title: Re: Sequence keeps repeating even though incrementing of counter should prevent it
Post by: Matti on Thu 24/06/2021 00:51:47
Hi there  ;-D

You need some extra brackets:

Code (ags) Select
if ( (aliceCount >= 1 || lilaCount >=1 || tedCount >= 1) && brushCount == 0 && cMalcolm.ActiveInventory == iDinceyDollBrush)

Because in your code the condition would be true even if brushCount would be higher than zero as long as aliceCount or lilaCount would be >= 1.
Title: Re: Sequence keeps repeating even though incrementing of counter should prevent it
Post by: Crimson Wizard on Thu 24/06/2021 01:01:31
First of all I'd double check that your condition sais what you mean it say. I won't be able to tell if it's correct unless you describe what logic you want there in plain language.

But logical conditions are easy place of error, similar to math btw, because of how logical operations interact with each other.
In short:
1) they only apply to nearest neighbours,
2) AND (&&) operator have precedence over OR (||)


For example, if you have:
Code (ags) Select

if (condition1 || condition2 && condition3)


Such condition will result in success if:
1) condition1 is true, OR
2) both condition2 and condition3 are true.

On another hand, if you have:
Code (ags) Select

if ( (condition1 || condition2) && condition3)


Such condition will result in success if:
1) Either condition1 OR condition2 is true, AND
2) condition3 are true.



Let's look on your condition:
Code (ags) Select

if (aliceCount >= 1 || lilaCount >=1 || tedCount >= 1 && brushCount == 0 && cMalcolm.ActiveInventory == iDinceyDollBrush)


Such condition will result in success in THREE cases:
1) aliceCount >= 1, OR
2) lilaCount >=1, OR
3) tedCount >= 1 && brushCount == 0 && cMalcolm.ActiveInventory == iDinceyDollBrush
So, if any of these is true - the condition will result in true.

Brackets create "groups", which are calculated first, and then involve into any external operation as if they were one condition. This helps to change the way different conditions are applied to each other. For example:

if ( (aliceCount >= 1 || lilaCount >=1 || tedCount >= 1) && brushCount == 0 && cMalcolm.ActiveInventory == iDinceyDollBrush)


Such condition will result in success if:
1) at least one of the other counters is >=1, AND
2) brushCount == 0 && cMalcolm.ActiveInventory == iDinceyDollBrush
Title: Re: Sequence keeps repeating even though incrementing of counter should prevent it
Post by: cipberbloom on Thu 24/06/2021 01:04:50
Quote from: Matti on Thu 24/06/2021 00:51:47
Hi there  ;-D

You need some extra brackets:

Code (ags) Select
if ( (aliceCount >= 1 || lilaCount >=1 || tedCount >= 1) && brushCount == 0 && cMalcolm.ActiveInventory == iDinceyDollBrush)

Because in your code the condition would be true even if brushCount would be higher than zero as long as aliceCount or lilaCount would be >= 1.

Oh wow! Thank you so much--you are a lifesaver! So it's just like maths, then! Seriously, you're awesome. Vielen dank!  ;-D
Title: Re: Sequence keeps repeating even though incrementing of counter should prevent it
Post by: cipberbloom on Thu 24/06/2021 01:10:40
Quote from: Crimson Wizard on Thu 24/06/2021 01:01:31
First of all I'd double check that your condition sais what you mean it say. I won't be able to tell if it's correct unless you describe what logic you want there in plain language.

But logical conditions are easy place of error, similar to math btw, because of how logical operations interact with each other. In short - they only apply to nearest neighbours.

And a thousand thanks to you, too, Crimson Wizard! Any thorough explanations regarding maths and programming concepts are always very helpful, as both subjects have been pretty challenging to me.  You're great--thank you!  ;-D
Title: Re: Sequence keeps repeating even though incrementing of counter should prevent it
Post by: Matti on Thu 24/06/2021 01:17:36
Gerne  :)

And yes, CW did the explanation I had in mind but was too lazy to write down  ;)