[Solved] Error: "Line too long (max line length = 500)

Started by BrightBulb, Fri 10/04/2020 22:13:01

Previous topic - Next topic

BrightBulb

Hi everyone,

I got the above error message with a very complex if expression with a lot of long variable names. There is no problem with the brackets. The line is just too long.

Is there any solution to this?

Thanks.

Crimson Wizard

#1
Simply split the line in two or more.

In AGS Script, as well as most other programming languages, expressions may contain linebreaks in between separate operators/names, and whitespaces (spaces, tabs) mean nothing and ignored by compiler.

Example:
Code: ags

if (a + b + c + d +.... y == z)
{
}

===>
Code: ags

if (a + b +
       c + d +
       ... +y
       == z)
{
}



Alternatively, sometimes it may be convenient to split complex expression into separate variables. For instance, this may make debugging easier, since you can test intermediate parts of expression separately.

For example:
Code: ags

int v1 = a + b + c;
int v2 = d + e + f;
....
int total = v1 + v2 + v3 ...;

if (total == z)
{
}

BrightBulb

I already tried to split the line, but then I get a "end of input reached in the middle of expression" error.

Does splitting work with && / || operators?

Snarky

There may be something wrong with the expression. You could post it here for us to see.

Crimson Wizard

Quote from: BrightBulb on Fri 10/04/2020 23:23:48
I already tried to split the line, but then I get a "end of input reached in the middle of expression" error.

Maybe you actually have an extra/missing bracket there? Coud you post this line?

Quote from: BrightBulb on Fri 10/04/2020 23:23:48
Does splitting work with && / || operators?

Definitely, if you split between them and not in the middle of && for example :).

BrightBulb

Code: ags

if ( (iMode==1 && Item[Party[iMemberClicked].iContainerOpen].iSize > Item[iActiveItem].iSize && (Item[Party[iMemberClicked].iContainerOpen].iContainerType==1 || Item[Party[iMemberClicked].iContainerOpen].iContainerType==Item[iActiveItem].iFitType)) || (iMode==0 && ( (iButtonClicked>11 || iButtonClicked == Item[iActiveItem].iSlotType) || (iButtonClicked==11 && Item[iActiveItem].iSlotType==9) || ((iButtonClicked==3 || iButtonClicked ==5) && Item[iActiveItem].iSlotType==12) || (iButtonClicked==3  &&  Item[iActiveItem].iSlotType==13 && Party[iMemberClicked].iSlot[5]==0)))){

Crimson Wizard

#6
Well, I tested putting this in a random game, and it compiled fine after splitting in 3 average lines.
(of course I had to declare some dummy structs there first)

Code: ags

if ( (iMode==1 && Item[Party[iMemberClicked].iContainerOpen].iSize > Item[iActiveItem].iSize &&
	(Item[Party[iMemberClicked].iContainerOpen].iContainerType==1 || Item[Party[iMemberClicked].iContainerOpen].iContainerType==Item[iActiveItem].iFitType))
	|| (iMode==0 && ( (iButtonClicked>11 || iButtonClicked == Item[iActiveItem].iSlotType) || (iButtonClicked==11 && Item[iActiveItem].iSlotType==9) || ((iButtonClicked==3 || iButtonClicked ==5) && Item[iActiveItem].iSlotType==12) || (iButtonClicked==3  &&  Item[iActiveItem].iSlotType==13 && Party[iMemberClicked].iSlot[5]==0)))){
}


Maybe you forgot a bracket somewhere else?




BTW, regardless, I'd definitely advise to break this giant condition into multiple expressions, store results in variables, and then use these variables in a shorter "if". That might save alot of time later when expanding logic or searching for mistakes.

Another approach is to instead use functions that test some common conditions. I don't know your system very well, but to give a random example:
Code: ags

bool CanItemFit(int member, int item) {
    return Item[Party[member].iContainerOpen].iContainerType == Item[item].iFitType;
}

Code: ags

// used as
if ( CanItemFit(iMemberClicked, iActiveItem) ) {
}

Above also helps reading the code, as you read function name that describes what's going on, rather than a long expression full of brackets and stuff.

BrightBulb

I split the line in the an suddenly it worked. I don't know why. Didn't change any brackets.

You are absolutely right about restructering the whole script. It always starts small and gets bigger and bigger...


Thank for taking your time to look through this mess.

SMF spam blocked by CleanTalk