Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: BrightBulb on Fri 10/04/2020 22:13:01

Title: [Solved] Error: "Line too long (max line length = 500)
Post by: BrightBulb on Fri 10/04/2020 22:13:01
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.
Title: Re: Error: "Line too long (max line length = 500)
Post by: Crimson Wizard on Fri 10/04/2020 22:36:07
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) Select

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

===>
Code (ags) Select

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) Select

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

if (total == z)
{
}
Title: Re: Error: "Line too long (max line length = 500)
Post by: 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.

Does splitting work with && / || operators?
Title: Re: Error: "Line too long (max line length = 500)
Post by: Snarky on Fri 10/04/2020 23:38:10
There may be something wrong with the expression. You could post it here for us to see.
Title: Re: Error: "Line too long (max line length = 500)
Post by: Crimson Wizard on Fri 10/04/2020 23:38:58
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 :).
Title: Re: Error: "Line too long (max line length = 500)
Post by: BrightBulb on Fri 10/04/2020 23:45:58
Code (ags) Select

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)))){
Title: Re: Error: "Line too long (max line length = 500)
Post by: Crimson Wizard on Sat 11/04/2020 00:25:10
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) Select

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) Select

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

Code (ags) Select

// 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.
Title: Re: Error: "Line too long (max line length = 500)
Post by: BrightBulb on Sat 11/04/2020 13:51:42
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.