Define a VALUE not variable.
#1

Ok, Its all well and easy to do something like
pawn Код:
#define hello 5
....but I want something more dynamic for example:

pawn Код:
new var = CreateVehicle(...);
#define VEHICLE var
This works, but only if "var" is made into a global variable. Is there anyway to assign the VALUE of "var" to the define, rather than making the define point to "var" which equals the value?

Any advice would be greatly appreciated!

Thanks

David
Reply
#2

If we're on the same page here. The reason why you can't change the value of what is defined is because when you define something globally it is stored in the memory when the script is initialized. Changing the value of the variable afterwards will make no difference.

I've been thinking and honestly... I have NO idea why you would want to do this?! Why not just use a variable? The only real reason why people use the define is to make scripting a large script simpler or avoiding repeating calculations or values (like with the DCMD definition). Instead of remembering the integer, you define it to an alpha numeric value which allows global use of it easier, and allows to change one value and altering all of the values instead of changing it everytime its used.

Eg. #define MAX_PLAYERS 200

The only reason it's there is because its simpler, and easier to change/edit. Instead of:

pawn Код:
for(new i = 0; i<200; i++)
// you do...
for(new i = 0; i<MAX_PLAYERS; i++)
Perhaps if you elaborated on what you want to do exactly, I could understand what it is exactly you want.
Reply
#3

pawn Код:
new max_players;
SetMaxPlayers()
{
    max_players = GetMaxPlayers();
     #undef MAX_PLAYERS
    #define MAX_PLAYERS max_players
    return 1;
}
I have this.....I know I COULD just change everything in my script to max_players, rather than MAX_PLAYERS, or just change the define in the includes....but I'm awkward!

As far as I know, the define calling for the value in a variable is far slower than the define itself defining the number....however I am not sure that the difference in speed is capable of making a difference as im sure its still very fast...

Thanks for your reply.

David
Reply
#4

Alternatively, you could simply undefine MAX_PLAYERS, then create a global variable with the same name and set it to that.
pawn Код:
#include <a_samp>
#undef MAX_PLAYERS

new MAX_PLAYERS = 200; // The default, in case you use it before you call SetMaxPlayers().

#pragma unused MAX_PLAYERS

#define SetMaxPlayers() \
  MAX_PLAYERS = GetMaxPlayers()

public OnFilterScriptInit()
{
  SetMaxPlayers();

  return 1;
}
That will work as you imagine it would. The definition of the function is becase it is a lot simpler and more efficient to create the simple function that way, and the pragma is because if you don't use MAX_PLAYERS, it will issue a compiler warning. The pragma pre-empts that.

And the only way I can think to do what you are asking is to use defines once again to set a definition to another value. It's pretty advanced and you won't need it when you can use the method above.

~Cueball~
Reply
#5

Ok, I'll have a play around with that idea, thanks for your help!

David
Reply
#6

You can't set the value of a #define with a variable: #define are precompiler directives.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)