Using a non-constant expression as a constant expression? -
BullseyeHawk - 06.02.2014
Is using a non-constant expression, a changing integer as a constant expression possible?
pawn Код:
stock createStringSize(size=80)
{
new stringSize[size];
return sizeof(stringSize);
}
By default, this is impossible. Would return:
Код:
error 008: must be a constant expression; assumed zero
My question is using the size a NON-constant expression as a constant expression. Possible? If yes, how?
I am quite aware that it's mostly impossible, cause it'll get the string size declared over and over, but why it won't be possible?
AW: Using a non-constant expression as a constant expression? -
Nero_3D - 06.02.2014
You just can't in pawn, why would you want to do so ?
Also is your function supposed to do ?
Re: Using a non-constant expression as a constant expression? -
BullseyeHawk - 06.02.2014
I'm attempting to create a custom function upon sendLongMessage,
pawn Код:
stock SendLongMessage( playerid, color, string[], max_chars=80)
{
new MAX_CHARS_PER_LINE = 80;
if(max_chars)
{
MAX_CHARS_PER_LINE = max_chars;
}
new length = strlen(string),
lines = length / MAX_CHARS_PER_LINE;
if(( length % MAX_CHARS_PER_LINE ))
++lines;
new line[MAX_CHARS_PER_LINE + 5];
new _:index;
while( index < lines )
{
if(index == 0)
strmid(line, string, ( index * MAX_CHARS_PER_LINE ), ( index * MAX_CHARS_PER_LINE ) + MAX_CHARS_PER_LINE);
else
strmid(line, string, ( index * MAX_CHARS_PER_LINE ), ( index * MAX_CHARS_PER_LINE ) + MAX_CHARS_PER_LINE);
if(lines > 1)
{
if(index == 0)
{
format(line, sizeof line, "%s ...", line);
}
else if(index > 0 && (index + 1) < lines)
{
format(line, sizeof line, "... %s ...", line);
}
else
{
format(line, sizeof line, "... %s", line);
}
}
SendClientMessage( playerid, color, line );
index++;
}
return 1;
}
It would have been nice if it's being able to be edited by a declared number.
Guess I'd need to move into the actual functions that are using it.
Thanks anyway!
AW: Re: Using a non-constant expression as a constant expression? -
Nero_3D - 06.02.2014
Well its not impossible, you could use y_malloc or an selfmade emit version
But the easiest way would be to use an array with 144 cells (max length of SendClientMessage)
Also it should be [MAX_CHARS_PER_LINE + 9] because we got "... %s ..." 8 extra chars + EOS
Re: Using a non-constant expression as a constant expression? -
ColeMiner - 06.02.2014
A "constant" is by definition always the same. A "variable" by definition changes.
Re: Using a non-constant expression as a constant expression? -
PowerPC603 - 07.02.2014
Setting the size of a string or array during runtime cannot be done by pawn, unless you use plugins or something that allows it.
Pawn needs to know the size of the strings/arrays during compiling. Variables can hold any value so they cannot be used to set the size.
Also resizing arrays after they have been declared isn't possible.
You have to make your string/array as large as you possibly ever need.