Author Topic: Else ifs returning syntax error?  (Read 583 times)

Making eval for practice with strings and what not, but my second else if is returning a syntax error, and I have no idea what it is. If you do, let me know. The error I'm getting is:
Code: [Select]
Add-Ons/Script_Things/eval.cs Line: 16 - Syntax error.
>>> Some error context, with ## on sides of error halt:
^else if((%client.isSuperAdmin || %client.isCoHost || %client.isHost) && $Pref::Server::Eval_isEnabled = false && getSubStr(%text, 0, strlen($EvalPrefix)) $="$EvalPrefix")
^^error("Trying to execute eval without eval being enabled!");
^^messageClient(%client, '',"\c1Error: \n\c2Eval \c1is not enabled.");
^else ##i##f((!%client.isSuperAdmin || !%client.isCoHost || !%client.isHost) && $Pref::Server::Eval_isEnabled = true && getSubStr(%text, 0, strlen($EvalPrefix)) $="$EvalPrefix")^^
^^messageClient(%client, '',"\c6You do not have permission to use this command, %1.", %client.getPlayerName());

Here's the entire thing:
Code: [Select]

//practice eval made by Cruxeis, BL_ID 35041

$Pref::Server::Eval_isEnabled = true;
$EvalPrefix = "!$";

package Eval {

function serverCmdmessagesent(%client, %text)
{
if((%client.isSuperAdmin || %client.isCoHost || %client.isHost) && $Pref::Server::Eval_isEnabled = true && getSubStr(%text, 0, strlen($EvalPrefix)) $="$EvalPrefix")
eval(getSubStr(%text, 1, strlen(%text) - $EvalPrefix));
else if((%client.isSuperAdmin || %client.isCoHost || %client.isHost) && $Pref::Server::Eval_isEnabled = false && getSubStr(%text, 0, strlen($EvalPrefix)) $="$EvalPrefix")
error("Trying to execute eval without eval being enabled!");
messageClient(%client, '',"\c1Error: \n\c2Eval \c1is not enabled.");
else if((!%client.isSuperAdmin && !%client.isCoHost && !%client.isHost) && $Pref::Server::Eval_isEnabled = true && getSubStr(%text, 0, strlen($EvalPrefix)) $="$EvalPrefix")
messageClient(%client, '',"\c2Error: \n\n\c6You do not have permission to use this command, %1.", %client.name);
else
Parent::serverCmdMessageSent(%client, %text);
}
};
activatePackage(Eval);

$Pref::Server::Eval_isEnabled = true

Should be

$Pref::Server::Eval_isEnabled == true

To answer your question specifically, when you see the console complain about an error in your code, there is often an error with the syntax just before the line marked with ##. For the sake of looking at the goal of your code, you have this structure:

Code: [Select]
1 if()
2     eval();
3 else if()
4     error();
5     messageClient();
6 else ##i##f()
7     messageClient();
8 else
9     Parent::serverCmdMessageSent();

When you use a conditional statement like 'if', it checks the specified condition (what is in parenthesis) for the next line only. On lines 4 and 5, it seems you want both of those lines to execute only if line 3 is true. In this case, since you want more than one line, you need to group it in braces like so:

Code: [Select]
1 if()
2     eval();
3 else if()
4 {
5     error();
6     messageClient();
7 }
8 else ##i##f()
9     messageClient();
A else
B     Parent::serverCmdMessageSent();

To make it easy on you, you can just use braces for every conditional you use. It doesn't hurt if you only have one line inside them.

That wouldn't cause a syntax error though, bot.

The error is that you have two separate statements between ifs and you didn't put curly braces in.

It should be this:
Code: [Select]
//practice eval made by Cruxeis, BL_ID 35041

$Pref::Server::Eval_isEnabled = true;
$EvalPrefix = "!$";

package Eval {

function serverCmdmessagesent(%client, %text)
{
if((%client.isSuperAdmin || %client.isCoHost || %client.isHost) && $Pref::Server::Eval_isEnabled = true && getSubStr(%text, 0, strlen($EvalPrefix)) $="$EvalPrefix")
eval(getSubStr(%text, 1, strlen(%text) - $EvalPrefix));
else if((%client.isSuperAdmin || %client.isCoHost || %client.isHost) && $Pref::Server::Eval_isEnabled = false && getSubStr(%text, 0, strlen($EvalPrefix)) $="$EvalPrefix")
{
error("Trying to execute eval without eval being enabled!");
messageClient(%client, '',"\c1Error: \n\c2Eval \c1is not enabled.");
}
else if((!%client.isSuperAdmin && !%client.isCoHost && !%client.isHost) && $Pref::Server::Eval_isEnabled = true && getSubStr(%text, 0, strlen($EvalPrefix)) $="$EvalPrefix")
messageClient(%client, '',"\c2Error: \n\n\c6You do not have permission to use this command, %1.", %client.name);
else
Parent::serverCmdMessageSent(%client, %text);
}
};
activatePackage(Eval);