SA-MP Forums Archive
Setting string size via function parameter - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Setting string size via function parameter (/showthread.php?tid=484363)



Setting string size via function parameter - GiS - 30.12.2013

Why doesn't this work? Can I set the size of a string with a variable somehow? It can be useful in a function.

pawn Код:
someFunction(size) {
    new string[size];
}
Код:
error 008: must be a constant expression; assumed zero
error 009: invalid array size (negative, zero or out of bounds)
error 036: empty statement
fatal error 107: too many error messages on one line



Re : Setting string size via function parameter - [HRD]Mar1 - 30.12.2013

pawn Код:
int size;
function(size) {
    new string[size];
}



Re: Setting string size via function parameter - Patrick - 30.12.2013

Never tried creating a little function using a stock I usually use macro for one line function, the code below compiled.

pawn Код:
#define Create.%0(%1) \
    new %0[%1]
Compilation
pawn Код:
Pawn compiler 3.2.3664          Copyright (c) 1997-2006, ITB CompuPhase
Usage
pawn Код:
Create.string(128);
   
format(string, sizeof(string), "aa");
Parameter
pawn Код:
Parameter : Create_StringName(Size);



Re: Setting string size via function parameter - Ada32 - 30.12.2013

Quote:
Originally Posted by pds2k12
Посмотреть сообщение
Never tried creating a little function using a stock I usually use macro for one line function, the code below compiled.

pawn Код:
#define Create.%0(%1) \
    new %0[%1]
Compilation
pawn Код:
Pawn compiler 3.2.3664          Copyright (c) 1997-2006, ITB CompuPhase
Usage
pawn Код:
Create.string(128);
   
format(string, sizeof(string), "aa");
Parameter
pawn Код:
Parameter : Create_StringName(Size);
that's NOT how you use macros!


Re: Setting string size via function parameter - Patrick - 30.12.2013

Quote:
Originally Posted by Ada32
Посмотреть сообщение
that's NOT how you use macros!
Zir can u sho me how 2 use macro? Zorry me beginner in scripting, please show me zir, i want to know hau also zorry me no speak english veri will.


Re: Setting string size via function parameter - Ada32 - 30.12.2013

Quote:
Originally Posted by pds2k12
Посмотреть сообщение
Zir can u sho me how 2 use macro? Zorry me beginner in scripting, please show me zir, i want to know hau also zorry me no speak english veri will.
i'll do you better -> https://sampforum.blast.hk/showthread.php?tid=166680


AW: Setting string size via function parameter - GiS - 30.12.2013

So there's no way I can use this without a macro?


Re: Setting string size via function parameter - Patrick - 30.12.2013

Quote:
Originally Posted by Ada32
Посмотреть сообщение
I won't argue with you, you don't even show me what I did wrong in there, there's alot of different style creating a macro, I apologize for being unprofessional *(Above, because your comment made me lol)

Pawn code
pawn Код:
#include <a_samp>

#define Create.%0(%1) \
    new %0[%1]
   
main()
{
    Create.string(128);
   
    new pds2k12 = 60 * 10 / 5;
    format(string, sizeof(string), "60 x 10 ч 5 = %i", pds2k12);
    print(string);
}
Result
pawn Код:
60 x 10 ч 5 = 120
Try the code here: http://slice-vps.nl:7070/

EDIT: lets go hard if you don't believe.

Pawn Code
pawn Код:
#include <a_samp>

#define Create.%0(%1) \
    new %0[%1]
   
main()
{
    new StartTick = GetTickCount();
    Create.string(128);
    printf("Took %i ms to create a string", GetTickCount() - StartTick);
   
    new StartTick1 = GetTickCount();
    new pds2k12 = 60 * 10 / 5;
    format(string, sizeof(string), "60 x 10 ч 5 = %i", pds2k12);
    print(string);
    printf("Took %i ms to execute the string & variable", GetTickCount() - StartTick1);
}
Result
Код:
Took 0 ms to create a string
60 x 10 ч 5 = 120
Took 1 ms to execute the string & variable



AW: Setting string size via function parameter - GiS - 30.12.2013

Well, thanks, but is there a way to do it with a function as shown above?


Re: Setting string size via function parameter - PowerPC603 - 30.12.2013

AFAIK, pawn requires strings with a fixed size.
You also cannot resize an existing string or array in pawn during runtime.