SA-MP Forums Archive
#define VALUE VARIABLE - 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: #define VALUE VARIABLE (/showthread.php?tid=614825)



#define VALUE VARIABLE - Shinja - 13.08.2016

I made a dialog to change some defines
PHP Code:
//OnDialogResponse...case dialogid:
#undef VALUE
#define VALUE strval(inputtext) 
But when i use it example
PHP Code:
printf("VALUE = %d"VALUE); 
In this line i get error "Undefined Symbol inputtext"

and example
PHP Code:
for(new i=0i<VALUEi++) 
i get error "Undefined Symbol inputtext"

How to do it correctly?


Re: #define VALUE VARIABLE - PrO.GameR - 13.08.2016

Defines are constants that get changed compile time, you need to use variables for that.


Re: #define VALUE VARIABLE - Misiur - 13.08.2016

Well, are you doing those things inside OnDialogResponse? Otherwise you don't have inputtext.


Re: #define VALUE VARIABLE - Shinja - 13.08.2016

Quote:
Originally Posted by Misiur
View Post
Well, are you doing those things inside OnDialogResponse? Otherwise you don't have inputtext.
Yes

PHP Code:
//OnDialogResponse...case dialogid: 
#undef VALUE 
#define VALUE strval(inputtext) 
Quote:
Originally Posted by PrO.GameR
View Post
Defines are constants that get changed compile time, you need to use variables for that.
I tried it..
PHP Code:
new VALUE 25
complier crashes in this line
PHP Code:
new Info[MAX_PLAYERS][VALUE][ENUM]; 



Re: #define VALUE VARIABLE - PrO.GameR - 13.08.2016

Because compiler needs constants when initializing arrays, there is no memory allocation in pawn or dynamic array sizes.


Re: #define VALUE VARIABLE - Shinja - 13.08.2016

Quote:
Originally Posted by PrO.GameR
View Post
Because compiler needs constants when initializing arrays, there is no memory allocation in pawn or dynamic array sizes.
Noway?


Re: #define VALUE VARIABLE - SickAttack - 13.08.2016

printf("VALUE = %d", VALUE);

pawn Code:
// ** INCLUDES

#include <a_samp>

// ** DEFINES

#define VALUE strval(inputtext)

// ** MAIN

main()
{
    print("Loaded \"blank.amx\".");

    OnDialogResponse(0, 0, 0, 0, "50");
}

// ** CALLBACKS

public OnGameModeInit()
{
    return 1;
}

public OnGameModeExit()
{
    return 1;
}

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    printf("%d", VALUE);
    return 1;
}
for(new i = 0; i < VALUE; i ++)

pawn Code:
// ** INCLUDES

#include <a_samp>

// ** DEFINES

#define VALUE strval(inputtext)

// ** MAIN

main()
{
    print("Loaded \"blank.amx\".");

    OnDialogResponse(0, 0, 0, 0, "50");
}

// ** CALLBACKS

public OnGameModeInit()
{
    return 1;
}

public OnGameModeExit()
{
    return 1;
}

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    for(new i = 0; i < VALUE; i ++)
    {
        printf("%d", i);
    }
    return 1;
}
All work.

new Info[MAX_PLAYERS][VALUE][ENUM];

Set it to the highest value being used.


Re: #define VALUE VARIABLE - Gammix - 13.08.2016

Quote:
Originally Posted by Shinja
View Post
Yes

PHP Code:
//OnDialogResponse...case dialogid: 
#undef VALUE 
#define VALUE strval(inputtext) 

I tried it..
PHP Code:
new VALUE 25
complier crashes in this line
PHP Code:
new Info[MAX_PLAYERS][VALUE][ENUM]; 
Declaring dynamic arrays isn't supported by PAWN, you have to have a constant value. In this case you are relying on "strval", not a constant.

Defines are just text replacements, so you are actually doing this: (not valid)
pawn Code:
new Info[MAX_PLAYERS][strval(inputtext)][ENUM];
And in case you are using "VALUE" inside of OnDialogResponse, the error should not occur.
Also i guess its meaningless to declare a macro instead of a variable here because basically you want a better readable code and don't want to process "strval" again and again. In that case, variables are helpful not macros.
pawn Code:
new VALUE = strval(inpittext);



Re: #define VALUE VARIABLE - Shinja - 13.08.2016

Quote:
Originally Posted by SickAttack
View Post
printf("VALUE = %d", VALUE);

pawn Code:
// ** INCLUDES

#include <a_samp>

// ** DEFINES

#define VALUE strval(inputtext)

// ** MAIN

main()
{
    print("Loaded \"blank.amx\".");

    OnDialogResponse(0, 0, 0, 0, "50");
}

// ** CALLBACKS

public OnGameModeInit()
{
    return 1;
}

public OnGameModeExit()
{
    return 1;
}

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    printf("%d", VALUE);
    return 1;
}
for(new i = 0; i < VALUE; i ++)

pawn Code:
// ** INCLUDES

#include <a_samp>

// ** DEFINES

#define VALUE strval(inputtext)

// ** MAIN

main()
{
    print("Loaded \"blank.amx\".");

    OnDialogResponse(0, 0, 0, 0, "50");
}

// ** CALLBACKS

public OnGameModeInit()
{
    return 1;
}

public OnGameModeExit()
{
    return 1;
}

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    for(new i = 0; i < VALUE; i ++)
    {
        printf("%d", i);
    }
    return 1;
}
All work.

new Info[MAX_PLAYERS][VALUE][ENUM];

Set it to the highest value being used.
That's not what i mean't, but thanks!

Thanks Gammix for explanations!


Re: #define VALUE VARIABLE - SickAttack - 13.08.2016

Quote:
Originally Posted by Shinja
View Post
That's not what i mean't, but thanks!
Yeah, I see. The definition doesn't store values, so who knows what you were thinking. And the main post lacks of what you were trying to do.