Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
  • 0
Nevander

Noob ACS problem

Question

Posted (edited)

I don't know what's going on here. Can someone explain what is going on with this and why it doesn't care about both conditions?

 

int monstersDead = 0;

script 1 (void)

{

// theres a bunch of crap here, but eventually...

monstersDead = 1;

}

 

int switchUses = 0;

script 2 OPEN

{

While(switchUses < 2 && monstersDead == 0) Delay(10);

Delay(35);

// do some thing

}

 

For some reason, script 2 finishes without the other condition being true, it passes anyway even if one is still false. Logically it should not be happening, right?

 

If I put separate the two conditions into one while loop of their own, it works as intended.

 

I feel like a noob but this is confusing and frustrating me why a simple condition combination is failing.

 

EDIT: I just tried it from a different approach which is probably better anyway but I still want to know why the first way fails. Here's what I did instead:

 

script 2 OPEN

{

While(TRUE)

{

if(monstersDead == 1 && switchesUsed >= 2)

{

// stuff

terminate;

}

Delay(10);

}

}

Edited by Nevander

Share this post


Link to post

5 answers to this question

Recommended Posts

  • 0
Posted (edited)

Use code tags for code.

 

Anyway, you problem is entirely logic based, let's look at this condition specifically:

While(switchUses < 2 && monstersDead == 0) Delay(10);

A while loop requires the condition to evaluate to true to stay inside its block, if it goes false the loop terminates.

Meanwhile, you are using logical AND (&&), this means that the condition is true as long as both conditions are true, otherwise it's false. If switchUses is equal or greater than two, than its condition is false, so it doesn't matter what monstersDead is, the logical state of the condition is now false, and the loop terminates.

You want logical OR (||), which checks if either condition is true first and returns false only if both evaluate as false. That way, both monstersDead and switchUses must both be false for the condition to evaluate as false.

 

The English class version of this is; "Sally and Mary go to the store" is only true if both Sally and Mary are going to the store (Sally && Mary), while "Sally or Mary go to the store" is true if either or both go to the store, and false if neither of them go (Sally || Mary).

Edited by Edward850

Share this post


Link to post
  • 0
Posted (edited)

Try changing

While(switchUses < 2 && monstersDead == 0) Delay(10);

to

While((switchUses < 2) || (monstersDead == 0)) Delay(10);

Share this post


Link to post
  • 0
23 minutes ago, Nevander said:

While(switchUses < 2 && monstersDead == 0) Delay(10);

Delay(35);

I mildly confused by these lines.

I suggest using an until loop, this is what I do when I want something to execute when multiple variables have to be true.

int variable1=0;
int variable2=0;

script "My Cool Script" (void)
{
 until (variable1==1 && variable2==2)
 {
  Delay (35);
 }
 {
  //Do things
 }
}

This is basically the same as the following while loop:

int variable1=0;
int variable2=0;

script "My Cool Script" (void)
{
 while (variable1==0 && variable2!=2)
 {
  Delay (35);
 }
 {
  //Do stuff
 }
}

And both Empyre and Edward850 beat me to the punch, but they're both correct.

Share this post


Link to post
  • 0
6 minutes ago, Edward850 said:

Use code tags for code.

 

Anyway, you problem is entirely logic based, let's look at this condition specifically:


While(switchUses < 2 && monstersDead == 0) Delay(10);

A while loop requires the condition to evaluate to true to stay inside its block, if it goes false the loop terminates.

Meanwhile, you are using logical AND (&&), this means that the condition is true as long as both conditions are true, otherwise it's false. If switchUses is equal or greater than two, than its condition is false, so it doesn't matter what monstersDead is, the logical state of the condition is now false, and the loop terminates.

You want logical OR (||), which checks if either condition is true first and returns false only if both evaluate as false. That way, both monstersDead and switchUses must both be false for the condition to evaluate as false.

I don't know how to use code tags on mobile, there's no code button.

 

I'm still a bit confused but to be fair I've always misunderstood how while and until loops work, at least when there are multiple comparisons involved. I'm gonna try to make sense of this, thanks.

Share this post


Link to post
  • 0
5 minutes ago, Nevander said:

I don't know how to use code tags on mobile, there's no code button.

I'm pretty sure you can use them on mobile by typing them out like you can spoilers and quotes. Below will either be a pass or fail of this test.

Like this: [code]Your code here

[/code]

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×