MasterofJKD Posted July 30, 2012 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."); 0 Share this post Link to post
Memfis Posted July 30, 2012 Shouldn't it be... if(CheckInventory("YellowCard") && CheckInventory("BlueCard")) { Door_Open(16, 16); delay(100); Door_Close(16, 16); } ? 0 Share this post Link to post
MasterofJKD Posted July 30, 2012 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. 0 Share this post Link to post
boris Posted July 30, 2012 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) 0 Share this post Link to post
MasterofJKD Posted July 30, 2012 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."); } 0 Share this post Link to post
Enjay Posted July 31, 2012 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. 0 Share this post Link to post
MasterofJKD Posted August 2, 2012 I added the other brace to the script and it works now, thanks everyone for helping me out. 0 Share this post Link to post
Quasar Posted August 2, 2012 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 ) 0 Share this post Link to post
Nomad Posted August 2, 2012 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! 0 Share this post Link to post
MasterofJKD Posted August 6, 2012 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. 0 Share this post Link to post
EarthQuake Posted August 6, 2012 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. 0 Share this post Link to post
Gez Posted August 6, 2012 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. 0 Share this post Link to post