Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Sign in to follow this  
MasterofJKD

Why is there an error?

Recommended Posts

There is an error near the "else" statement when I try to compile the script it simply says "invalid statement" and for the life of me i cant figure out why. Also I am still fairly new at acs script. Any help would be appreciated.


Script 3 (void)
//2 key door
{
if(CheckInventory("YellowCard") && CheckInventory("BlueCard"))
Door_Open(16, 16);
delay(100);
Door_Close(16, 16);


else
print(s:"You need both the yellow & blue keys.");

Share this post


Link to post

Shouldn't it be...

if(CheckInventory("YellowCard") && CheckInventory("BlueCard"))
{
Door_Open(16, 16);
delay(100);
Door_Close(16, 16);
}

?

Share this post


Link to post

well I tried adding the bracket, and now it says "invalid declarator" near the else statement but I still would like the else statement to tell the player they need both the yellow and blue keys.

Share this post


Link to post

What does your script look like now? It should be something like

Script 3 (void)
//2 key door
{
	if(CheckInventory("YellowCard") && CheckInventory("BlueCard"))
	{
		Door_Open(16, 16);
		delay(100);
		Door_Close(16, 16);
	}
	else
	{
		print(s:"You need both the yellow & blue keys.");
	}
}
(the curly braces around the else block isn't really needed since there's only one statement)

Share this post


Link to post

Now it looks like this, I changed it to what boris showed and it still says "invalid declarator" near the "else" statement.

Script 3 (void)
//2 key door
{
if(CheckInventory("YellowCard") && CheckInventory("BlueCard"))
Door_Open(16, 16);
delay(100);
Door_Close(16, 16);
}
else
{
print(s:"You need both the yellow & blue keys.");
}

Share this post


Link to post

You did not change it to what Boris typed. Count the number of curly braces in his post versus yours. He has 2 more than you (6 versus 4).

The way you have it, the else part is outside the script.

Share this post


Link to post

Write a man a script and he'll have one linedef. Teach a man to script and he'll code for a lifetime.

(I am suggesting it'd be better to explain the principles behind bracketed scopes in a language like ACS and why they have to always be balanced than to just paste a correct script :P )

Share this post


Link to post

Script 3 (void)
//2 key door
{
if(CheckInventory("YellowCard") && CheckInventory("BlueCard"))
<==You're missing a bracket here!
Door_Open(16, 16);
delay(100);
Door_Close(16, 16);
}
else
{
print(s:"You need both the yellow & blue keys.");
}
<== And you need another one here to close the script!

Share this post


Link to post

Thank you for actually showing where I missed the brackets, but yeah I do need to be shown how to really get into the more advanced scripting stuff, I'm good with basic commands but the "check inventory" and other things, also the if/else statement is still iffy to me.

Share this post


Link to post

If statements are control structures that alter the flow of the script. They can be in two forms: block, and inline.

An inline If statement looks like this:

If (Condition) Do something;
If "condition" evaluates to true (equals non-zero), "do something" is executed. If it just has one code statement needs to execute, you don't have to worry about using brackets.. You can also write it like the following, because the compiler doesn't really care about whitespace so much:
If (Condition)
  Do something;
Now if you want to do more than one thing if the condition is met, you need to use the block form:
If (Condition)
{
  Do something;
  Do something else;
}
Additionally, you can use Else If and Else clauses to an If block to check for even more conditions (however If must always come first, and Else must be last, if used at all):
If (Condition)
{
  Do something;
}
Else If (Some other condition)
{
  Do something else;
}
Else
{
  Otherwise do this;
}
And of course, you can mix up block and inline clauses:
If (!Condition) Do this if not true;
Else
{
  Do this;
  And that;
}
Basically, any time you start a block of code using a bracket {, you must close that block of code with another bracket }. Think of them as related instructions that get executed at once and you need to tell it how to group them together.

Share this post


Link to post

It's a good idea to always use bracketed blocks, that way there's no surprise.

Look at this code:

If (some condition)
    Do this;
    And that;
    And also this;
Else
    Do something different;
It won't compile, because the "if" will stop at "Do this" and when the "else" is encountered the compiler won't know what it's attached to.
If (some condition)
{
    Do this;
    And that;
    And also this;
}
Else
    Do something different;
Now, this works. But imagine another approach:
If (some condition)
    Do something;
Else
    Do this;
    And that;
    And also this;
It will compile, but it won't work as you'd expect. If the condition in the "if" is not true, it'll do "this", and "that" and "also this", yay it works. And if the condition is true? Then it'll do "something", and follow it with "that" and "also this"! Because, without brackets to delimit the block, it'll be compiled as if it were as such:
If (some condition)
{
    Do something;
}
Else
{
    Do this;
}
And that;
And also this;
Which is not what was intended.

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
Sign in to follow this  
×